Search in sources :

Example 11 with ClientAccessToken

use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project testcases by coheigea.

the class BalanceServiceTest method testPartnerServiceWithToken.

@org.junit.Test
public void testPartnerServiceWithToken() throws Exception {
    URL busFile = BalanceServiceTest.class.getResource("cxf-client.xml");
    // Create an initial account at the bank
    String address = "https://localhost:" + PORT + "/bankservice/customers/balance";
    WebClient client = WebClient.create(address, "bob", "security", busFile.toString());
    client.type("text/plain").accept("text/plain");
    client.path("/bob");
    client.post(40);
    // Get Authorization Code (as "bob")
    String oauthService = "https://localhost:" + OAUTH_PORT + "/services/";
    WebClient oauthClient = WebClient.create(oauthService, setupProviders(), "bob", "security", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(oauthClient).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    String code = getAuthorizationCode(oauthClient);
    assertNotNull(code);
    // Now get the access token
    oauthClient = WebClient.create(oauthService, setupProviders(), "consumer-id", "this-is-a-secret", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(oauthClient).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    ClientAccessToken accessToken = getAccessTokenWithAuthorizationCode(oauthClient, code, "http://www.blah.apache.org");
    assertNotNull(accessToken.getTokenKey());
    // Now invoke on the service with the access token
    String partnerAddress = "https://localhost:" + PORT + "/bankservice/partners/balance";
    WebClient partnerClient = WebClient.create(partnerAddress, busFile.toString());
    partnerClient.type("text/plain").accept("text/plain");
    partnerClient.header("Authorization", "Bearer " + accessToken.getTokenKey());
    partnerClient.path("/bob");
    // Now make a service invocation with the access token
    Response serviceResponse = partnerClient.get();
    assertEquals(serviceResponse.getStatus(), 200);
    assertEquals(serviceResponse.readEntity(Integer.class).intValue(), 40);
}
Also used : Response(javax.ws.rs.core.Response) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL)

Example 12 with ClientAccessToken

use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project testcases by coheigea.

the class BalanceServiceTest method testPartnerServiceWithTokenUsingAudience.

@org.junit.Test
public void testPartnerServiceWithTokenUsingAudience() throws Exception {
    URL busFile = BalanceServiceTest.class.getResource("cxf-client.xml");
    // Create an initial account at the bank
    String address = "https://localhost:" + PORT + "/bankservice/customers/balance";
    WebClient client = WebClient.create(address, "bob", "security", busFile.toString());
    client.type("text/plain").accept("text/plain");
    client.path("/bob");
    client.post(40);
    // Get Authorization Code (as "bob")
    String oauthService = "https://localhost:" + OAUTH_PORT + "/services/";
    WebClient oauthClient = WebClient.create(oauthService, setupProviders(), "bob", "security", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(oauthClient).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    String code = getAuthorizationCode(oauthClient, null, "consumer-id-aud");
    assertNotNull(code);
    // Now get the access token
    oauthClient = WebClient.create(oauthService, setupProviders(), "consumer-id-aud", "this-is-a-secret", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(oauthClient).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    String partnerAddress = "https://localhost:" + PORT + "/bankservice/partners/balance";
    ClientAccessToken accessToken = getAccessTokenWithAuthorizationCode(oauthClient, code, "consumer-id-aud", partnerAddress, "http://www.blah.apache.org");
    assertNotNull(accessToken.getTokenKey());
    // Now invoke on the service with the access token
    WebClient partnerClient = WebClient.create(partnerAddress, busFile.toString());
    partnerClient.type("text/plain").accept("text/plain");
    partnerClient.header("Authorization", "Bearer " + accessToken.getTokenKey());
    partnerClient.path("/bob");
    // Now make a service invocation with the access token
    Response serviceResponse = partnerClient.get();
    assertEquals(serviceResponse.getStatus(), 200);
    assertEquals(serviceResponse.readEntity(Integer.class).intValue(), 40);
}
Also used : Response(javax.ws.rs.core.Response) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL)

Example 13 with ClientAccessToken

use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project testcases by coheigea.

the class UserInfoTest method testSignedUserInfo.

@org.junit.Test
public void testSignedUserInfo() throws Exception {
    URL busFile = UserInfoTest.class.getResource("cxf-client.xml");
    String address = "https://localhost:" + PORT + "/services/";
    WebClient client = WebClient.create(address, setupProviders(), "alice", "security", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    // Get Authorization Code
    String code = getAuthorizationCode(client, "openid");
    assertNotNull(code);
    // Now get the access token
    client = WebClient.create(address, setupProviders(), "consumer-id", "this-is-a-secret", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    ClientAccessToken accessToken = getAccessTokenWithAuthorizationCode(client, code);
    assertNotNull(accessToken.getTokenKey());
    assertTrue(accessToken.getApprovedScope().contains("openid"));
    // Now invoke on the UserInfo service with the access token
    String userInfoAddress = "https://localhost:" + USERINFO_PORT + "/services/signed/userinfo";
    WebClient userInfoClient = WebClient.create(userInfoAddress, busFile.toString());
    userInfoClient.accept("application/jwt");
    userInfoClient.header("Authorization", "Bearer " + accessToken.getTokenKey());
    Response serviceResponse = userInfoClient.get();
    assertEquals(serviceResponse.getStatus(), 200);
    String token = serviceResponse.readEntity(String.class);
    assertNotNull(token);
    JwsJwtCompactConsumer jwtConsumer = new JwsJwtCompactConsumer(token);
    JwtToken jwt = jwtConsumer.getJwtToken();
    assertEquals("alice", jwt.getClaim(JwtConstants.CLAIM_SUBJECT));
    assertEquals("consumer-id", jwt.getClaim(JwtConstants.CLAIM_AUDIENCE));
    KeyStore keystore = KeyStore.getInstance("JKS");
    keystore.load(Loader.getResource("servicestore.jks").openStream(), "sspass".toCharArray());
    Certificate cert = keystore.getCertificate("myservicekey");
    Assert.assertNotNull(cert);
    Assert.assertTrue(jwtConsumer.verifySignatureWith((X509Certificate) cert, SignatureAlgorithm.RS256));
}
Also used : Response(javax.ws.rs.core.Response) JwtToken(org.apache.cxf.rs.security.jose.jwt.JwtToken) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) JwsJwtCompactConsumer(org.apache.cxf.rs.security.jose.jws.JwsJwtCompactConsumer) WebClient(org.apache.cxf.jaxrs.client.WebClient) KeyStore(java.security.KeyStore) URL(java.net.URL) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 14 with ClientAccessToken

use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project testcases by coheigea.

the class IntrospectionServiceTest method testTokenIntrospectionWithAudience.

@org.junit.Test
public void testTokenIntrospectionWithAudience() throws Exception {
    URL busFile = AuthorizationGrantTest.class.getResource("cxf-client.xml");
    String address = "https://localhost:" + PORT + "/services/";
    WebClient client = WebClient.create(address, setupProviders(), "alice", "security", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    // Get Authorization Code
    String code = getAuthorizationCode(client, null, "consumer-id-aud");
    assertNotNull(code);
    // Now get the access token
    client = WebClient.create(address, setupProviders(), "consumer-id-aud", "this-is-a-secret", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    String audience = "https://localhost:" + BANK_PORT + "/bankservice/partners/balance";
    ClientAccessToken accessToken = getAccessTokenWithAuthorizationCode(client, code, "consumer-id-aud", audience);
    assertNotNull(accessToken.getTokenKey());
    // Now query the token introspection service
    client = WebClient.create(address, setupProviders(), "consumer-id", "this-is-a-secret", busFile.toString());
    client.accept("application/json").type("application/x-www-form-urlencoded");
    Form form = new Form();
    form.param("token", accessToken.getTokenKey());
    client.path("introspect/");
    Response response = client.post(form);
    TokenIntrospection tokenIntrospection = response.readEntity(TokenIntrospection.class);
    assertEquals(tokenIntrospection.isActive(), true);
    assertEquals(tokenIntrospection.getUsername(), "alice");
    assertEquals(tokenIntrospection.getClientId(), "consumer-id-aud");
    assertEquals(tokenIntrospection.getScope(), accessToken.getApprovedScope());
    Long validity = tokenIntrospection.getExp() - tokenIntrospection.getIat();
    assertTrue(validity == accessToken.getExpiresIn());
    assertEquals(tokenIntrospection.getAud().get(0), audience);
}
Also used : Response(javax.ws.rs.core.Response) TokenIntrospection(org.apache.cxf.rs.security.oauth2.common.TokenIntrospection) Form(javax.ws.rs.core.Form) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL)

Example 15 with ClientAccessToken

use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project testcases by coheigea.

the class JWTClientAuthenticationTest method testJWT.

@org.junit.Test
public void testJWT() throws Exception {
    URL busFile = JWTClientAuthenticationTest.class.getResource("cxf-client.xml");
    String address = "https://localhost:" + PORT + "/services/";
    WebClient client = WebClient.create(address, setupProviders(), "alice", "security", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    // Get Authorization Code
    String code = getAuthorizationCode(client);
    assertNotNull(code);
    // Now get the access token
    String jwtAddress = "https://localhost:" + PORT + "/jwtservices/";
    client = WebClient.create(jwtAddress, setupProviders(), busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    // Create the JWT Token
    String token = createToken("DoubleItSTSIssuer", "consumer-id", jwtAddress + "token", true, true);
    ClientAccessToken accessToken = getAccessTokenWithAuthorizationCode(client, code, token);
    assertNotNull(accessToken.getTokenKey());
}
Also used : ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL)

Aggregations

ClientAccessToken (org.apache.cxf.rs.security.oauth2.common.ClientAccessToken)134 WebClient (org.apache.cxf.jaxrs.client.WebClient)116 URL (java.net.URL)53 Response (javax.ws.rs.core.Response)51 Form (javax.ws.rs.core.Form)41 Test (org.junit.Test)21 Consumer (org.apache.cxf.rs.security.oauth2.client.Consumer)16 Book (org.apache.cxf.systest.jaxrs.security.Book)12 JwsJwtCompactConsumer (org.apache.cxf.rs.security.jose.jws.JwsJwtCompactConsumer)11 TokenIntrospection (org.apache.cxf.rs.security.oauth2.common.TokenIntrospection)11 OAuthServiceException (org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException)8 JsonMapObjectProvider (org.apache.cxf.jaxrs.provider.json.JsonMapObjectProvider)7 OAuthJSONProvider (org.apache.cxf.rs.security.oauth2.provider.OAuthJSONProvider)7 ClientRegistration (org.apache.cxf.rs.security.oauth2.services.ClientRegistration)7 ClientRegistrationResponse (org.apache.cxf.rs.security.oauth2.services.ClientRegistrationResponse)7 ResponseProcessingException (javax.ws.rs.client.ResponseProcessingException)6 JwtToken (org.apache.cxf.rs.security.jose.jwt.JwtToken)6 AuthorizationCodeGrant (org.apache.cxf.rs.security.oauth2.grants.code.AuthorizationCodeGrant)6 HashMap (java.util.HashMap)4 Produces (javax.ws.rs.Produces)4