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);
}
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);
}
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));
}
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);
}
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());
}
Aggregations