Search in sources :

Example 16 with Consumer

use of com.swiftmq.amqp.v100.client.Consumer in project ddf by codice.

the class OAuthPlugin method refreshTokens.

/**
 * Attempts to refresh the user's access token and saves the new tokens in the token storage
 *
 * @param refreshToken refresh token used to refresh access token
 * @param oauthSource source being queried
 * @throws OAuthPluginException if the access token could not be renewed
 */
private void refreshTokens(String refreshToken, OAuthFederatedSource oauthSource, String sessionId, OIDCProviderMetadata metadata) throws StopProcessingException {
    if (refreshToken == null) {
        throw createNoAuthException(oauthSource, sessionId, metadata, "unable to find the user's refresh token.");
    }
    ClientAccessToken clientAccessToken;
    try {
        LOGGER.debug("Attempting to refresh the user's access token.");
        WebClient webClient = createWebclient(metadata.getTokenEndpointURI().toURL().toString());
        Consumer consumer = new Consumer(oauthSource.getOauthClientId(), oauthSource.getOauthClientSecret());
        AccessTokenGrant accessTokenGrant = new RefreshTokenGrant(refreshToken);
        clientAccessToken = OAuthClientUtils.getAccessToken(webClient, consumer, accessTokenGrant);
    } catch (OAuthServiceException e) {
        String error = e.getError() != null ? e.getError().getError() : "";
        throw createNoAuthException(oauthSource, sessionId, metadata, "failed to refresh access token " + error);
    } catch (MalformedURLException e) {
        throw createNoAuthException(oauthSource, sessionId, metadata, "malformed token endpoint URL. " + e.getMessage());
    }
    // Validate new access token
    try {
        AccessToken accessToken = convertCxfAccessTokenToNimbusdsToken(clientAccessToken);
        OidcTokenValidator.validateAccessToken(accessToken, null, resourceRetriever, metadata, null);
    } catch (OidcValidationException e) {
        throw createNoAuthException(oauthSource, sessionId, metadata, "failed to validate refreshed access token.");
    }
    // Store new tokens
    String newAccessToken = clientAccessToken.getTokenKey();
    String newRefreshToken = clientAccessToken.getRefreshToken();
    int status = tokenStorage.create(sessionId, oauthSource.getId(), newAccessToken, newRefreshToken, oauthSource.getOauthDiscoveryUrl());
    if (status != SC_OK) {
        LOGGER.warn("Error updating the token information.");
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Consumer(org.apache.cxf.rs.security.oauth2.client.Consumer) OAuthServiceException(org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException) RefreshTokenGrant(org.apache.cxf.rs.security.oauth2.grants.refresh.RefreshTokenGrant) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) AccessToken(com.nimbusds.oauth2.sdk.token.AccessToken) BearerAccessToken(com.nimbusds.oauth2.sdk.token.BearerAccessToken) TypelessAccessToken(com.nimbusds.oauth2.sdk.token.TypelessAccessToken) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) AccessTokenGrant(org.apache.cxf.rs.security.oauth2.common.AccessTokenGrant) WebClient(org.apache.cxf.jaxrs.client.WebClient) OidcValidationException(org.codice.ddf.security.oidc.validator.OidcValidationException)

Example 17 with Consumer

use of com.swiftmq.amqp.v100.client.Consumer in project activemq by apache.

the class SwiftMQClientTest method testSendReceive.

@Test
public void testSendReceive() throws Exception {
    String queue = "testqueue";
    int nMsgs = 100;
    final String dataFormat = "%01024d";
    int qos = QoS.AT_MOST_ONCE;
    AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT);
    try {
        Connection connection = new Connection(ctx, "127.0.0.1", port, false);
        connection.setContainerId("client");
        connection.setIdleTimeout(-1);
        connection.setMaxFrameSize(1024 * 4);
        connection.setExceptionListener(new ExceptionListener() {

            public void onException(Exception e) {
                e.printStackTrace();
            }
        });
        connection.connect();
        {
            Session session = connection.createSession(10, 10);
            Producer p = session.createProducer(queue, qos);
            for (int i = 0; i < nMsgs; i++) {
                AMQPMessage msg = new AMQPMessage();
                System.out.println("Sending " + i);
                msg.setAmqpValue(new AmqpValue(new AMQPString(String.format(dataFormat, i))));
                p.send(msg);
            }
            p.close();
            session.close();
        }
        System.out.println("=======================================================================================");
        System.out.println(" receiving ");
        System.out.println("=======================================================================================");
        {
            Session session = connection.createSession(10, 10);
            Consumer c = session.createConsumer(queue, 100, qos, true, null);
            // Receive messages non-transacted
            int i = 0;
            while (i < nMsgs) {
                AMQPMessage msg = c.receive();
                if (msg != null) {
                    final AMQPType value = msg.getAmqpValue().getValue();
                    if (value instanceof AMQPString) {
                        String s = ((AMQPString) value).getValue();
                        assertEquals(String.format(dataFormat, i), s);
                        System.out.println("Received: " + i);
                    }
                    if (!msg.isSettled())
                        msg.accept();
                    i++;
                }
            }
            c.close();
            session.close();
        }
        connection.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : AMQPString(com.swiftmq.amqp.v100.types.AMQPString) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage) AmqpValue(com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue) AMQPType(com.swiftmq.amqp.v100.types.AMQPType) AMQPContext(com.swiftmq.amqp.AMQPContext) AMQPString(com.swiftmq.amqp.v100.types.AMQPString) Test(org.junit.Test)

Example 18 with Consumer

use of com.swiftmq.amqp.v100.client.Consumer in project cxf by apache.

the class JAXRSOAuth2Test method testConfidentialClientIdOnly.

@Test()
public void testConfidentialClientIdOnly() throws Exception {
    String address = "https://localhost:" + port + "/oauth2/token";
    WebClient wc = createWebClient(address);
    try {
        OAuthClientUtils.getAccessToken(wc, new Consumer("fredNoPassword"), new CustomGrant(), false);
        fail("NotAuthorizedException exception is expected");
    } catch (OAuthServiceException ex) {
        assertEquals("invalid_client", ex.getError().getError());
    }
}
Also used : Consumer(org.apache.cxf.rs.security.oauth2.client.Consumer) OAuthServiceException(org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 19 with Consumer

use of com.swiftmq.amqp.v100.client.Consumer in project cxf by apache.

the class JAXRSOAuth2Test method testBasicAuthClientCred.

@Test
public void testBasicAuthClientCred() throws Exception {
    String address = "https://localhost:" + port + "/oauth2/token";
    WebClient wc = createWebClient(address);
    ClientCredentialsGrant grant = new ClientCredentialsGrant();
    // Pass client_id & client_secret as form properties
    // (instead WebClient can be initialized with username & password)
    grant.setClientId("bob");
    grant.setClientSecret("bobPassword");
    try {
        OAuthClientUtils.getAccessToken(wc, grant);
        fail("Form based authentication is not supported");
    } catch (OAuthServiceException ex) {
        assertEquals(OAuthConstants.UNAUTHORIZED_CLIENT, ex.getError().getError());
    }
    ClientAccessToken at = OAuthClientUtils.getAccessToken(wc, new Consumer("bob", "bobPassword"), new ClientCredentialsGrant(), true);
    assertNotNull(at.getTokenKey());
}
Also used : Consumer(org.apache.cxf.rs.security.oauth2.client.Consumer) OAuthServiceException(org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException) ClientCredentialsGrant(org.apache.cxf.rs.security.oauth2.grants.clientcred.ClientCredentialsGrant) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 20 with Consumer

use of com.swiftmq.amqp.v100.client.Consumer in project cxf by apache.

the class JAXRSOAuth2Test method testPublicClientIdOnly.

@Test
public void testPublicClientIdOnly() throws Exception {
    String pubPort = JCACHE_PORT_PUBLIC;
    if (JWT_JCACHE_PORT.equals(port)) {
        pubPort = JWT_JCACHE_PORT_PUBLIC;
    } else if (JPA_PORT.equals(port)) {
        pubPort = JPA_PORT_PUBLIC;
    } else if (JWT_NON_PERSIST_JCACHE_PORT.equals(port)) {
        pubPort = JWT_NON_PERSIST_JCACHE_PORT_PUBLIC;
    }
    String address = "http://localhost:" + pubPort + "/oauth2Public/token";
    WebClient wc = WebClient.create(address);
    ClientAccessToken at = OAuthClientUtils.getAccessToken(wc, new Consumer("fredPublic"), new CustomGrant(), false);
    assertNotNull(at.getTokenKey());
}
Also used : Consumer(org.apache.cxf.rs.security.oauth2.client.Consumer) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Aggregations

Consumer (org.apache.cxf.rs.security.oauth2.client.Consumer)19 WebClient (org.apache.cxf.jaxrs.client.WebClient)17 ClientAccessToken (org.apache.cxf.rs.security.oauth2.common.ClientAccessToken)12 AMQPMessage (com.swiftmq.amqp.v100.messaging.AMQPMessage)9 AMQPString (com.swiftmq.amqp.v100.types.AMQPString)9 Test (org.junit.Test)9 AMQPContext (com.swiftmq.amqp.AMQPContext)7 AmqpValue (com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue)7 JSSESocketFactory (com.swiftmq.net.JSSESocketFactory)6 JwsJwtCompactConsumer (org.apache.cxf.rs.security.jose.jws.JwsJwtCompactConsumer)6 OAuthServiceException (org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException)4 TxnIdIF (com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 RefreshTokenGrant (org.apache.cxf.rs.security.oauth2.grants.refresh.RefreshTokenGrant)3 AccessToken (com.nimbusds.oauth2.sdk.token.AccessToken)2 BearerAccessToken (com.nimbusds.oauth2.sdk.token.BearerAccessToken)2 TypelessAccessToken (com.nimbusds.oauth2.sdk.token.TypelessAccessToken)2 Connection (com.swiftmq.amqp.v100.client.Connection)2 InvalidStateException (com.swiftmq.amqp.v100.client.InvalidStateException)2