use of org.apache.cxf.sts.common.CustomClaimsHandler in project cxf by apache.
the class JWTClaimsTest method testJWTMultipleClaimsSameDialect.
/**
* Test the creation of a JWTToken with various claims set by a ClaimsHandler.
* We have both a primary claim (sent in wst:RequestSecurityToken) and a secondary claim
* (send in wst:RequestSecurityToken/wst:SecondaryParameters), and both have the
* same dialect in this test.
*/
@org.junit.Test
public void testJWTMultipleClaimsSameDialect() throws Exception {
TokenProvider tokenProvider = new JWTTokenProvider();
TokenProviderParameters providerParameters = createProviderParameters(JWTTokenProvider.JWT_TOKEN_TYPE, null);
ClaimsManager claimsManager = new ClaimsManager();
ClaimsHandler claimsHandler = new CustomClaimsHandler();
claimsManager.setClaimHandlers(Collections.singletonList(claimsHandler));
providerParameters.setClaimsManager(claimsManager);
ClaimCollection primaryClaims = createClaims();
primaryClaims.setDialect(ClaimTypes.URI_BASE);
providerParameters.setRequestedPrimaryClaims(primaryClaims);
ClaimCollection secondaryClaims = new ClaimCollection();
Claim claim = new Claim();
claim.setClaimType(ClaimTypes.STREETADDRESS);
secondaryClaims.add(claim);
secondaryClaims.setDialect(ClaimTypes.URI_BASE);
providerParameters.setRequestedSecondaryClaims(secondaryClaims);
TokenProviderResponse providerResponse = tokenProvider.createToken(providerParameters);
assertTrue(providerResponse != null);
assertTrue(providerResponse.getToken() != null && providerResponse.getTokenId() != null);
String token = (String) providerResponse.getToken();
assertNotNull(token);
JwsJwtCompactConsumer jwtConsumer = new JwsJwtCompactConsumer(token);
JwtToken jwt = jwtConsumer.getJwtToken();
assertEquals(jwt.getClaim(ClaimTypes.EMAILADDRESS.toString()), "alice@cxf.apache.org");
assertEquals(jwt.getClaim(ClaimTypes.FIRSTNAME.toString()), "alice");
assertEquals(jwt.getClaim(ClaimTypes.LASTNAME.toString()), "doe");
assertEquals(jwt.getClaim(ClaimTypes.STREETADDRESS.toString()), "1234 1st Street");
}
use of org.apache.cxf.sts.common.CustomClaimsHandler in project cxf by apache.
the class JWTClaimsTest method testJWTMultipleClaims.
/**
* Test the creation of a JWTToken with various claims set by a ClaimsHandler.
* We have both a primary claim (sent in wst:RequestSecurityToken) and a secondary claim
* (send in wst:RequestSecurityToken/wst:SecondaryParameters).
*/
@org.junit.Test
public void testJWTMultipleClaims() throws Exception {
TokenProvider tokenProvider = new JWTTokenProvider();
TokenProviderParameters providerParameters = createProviderParameters(JWTTokenProvider.JWT_TOKEN_TYPE, null);
ClaimsManager claimsManager = new ClaimsManager();
ClaimsHandler claimsHandler = new CustomClaimsHandler();
claimsManager.setClaimHandlers(Collections.singletonList(claimsHandler));
providerParameters.setClaimsManager(claimsManager);
ClaimCollection primaryClaims = createClaims();
providerParameters.setRequestedPrimaryClaims(primaryClaims);
ClaimCollection secondaryClaims = new ClaimCollection();
Claim claim = new Claim();
claim.setClaimType(ClaimTypes.STREETADDRESS);
secondaryClaims.add(claim);
providerParameters.setRequestedSecondaryClaims(secondaryClaims);
TokenProviderResponse providerResponse = tokenProvider.createToken(providerParameters);
assertTrue(providerResponse != null);
assertTrue(providerResponse.getToken() != null && providerResponse.getTokenId() != null);
String token = (String) providerResponse.getToken();
assertNotNull(token);
JwsJwtCompactConsumer jwtConsumer = new JwsJwtCompactConsumer(token);
JwtToken jwt = jwtConsumer.getJwtToken();
assertEquals(jwt.getClaim(ClaimTypes.EMAILADDRESS.toString()), "alice@cxf.apache.org");
assertEquals(jwt.getClaim(ClaimTypes.FIRSTNAME.toString()), "alice");
assertEquals(jwt.getClaim(ClaimTypes.LASTNAME.toString()), "doe");
assertEquals(jwt.getClaim(ClaimTypes.STREETADDRESS.toString()), "1234 1st Street");
}
use of org.apache.cxf.sts.common.CustomClaimsHandler in project cxf by apache.
the class JWTClaimsTest method testJWTRoleUsingCustomReturnType.
@org.junit.Test
public void testJWTRoleUsingCustomReturnType() throws Exception {
TokenProvider tokenProvider = new JWTTokenProvider();
TokenProviderParameters providerParameters = createProviderParameters(JWTTokenProvider.JWT_TOKEN_TYPE, null);
ClaimsManager claimsManager = new ClaimsManager();
ClaimsHandler claimsHandler = new CustomClaimsHandler();
claimsManager.setClaimHandlers(Collections.singletonList(claimsHandler));
providerParameters.setClaimsManager(claimsManager);
ClaimCollection claims = new ClaimCollection();
URI role = URI.create("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/role");
Claim claim = new Claim();
claim.setClaimType(role);
claims.add(claim);
providerParameters.setRequestedPrimaryClaims(claims);
Map<String, String> claimTypeMap = new HashMap<>();
claimTypeMap.put(role.toString(), "roles");
DefaultJWTClaimsProvider claimsProvider = new DefaultJWTClaimsProvider();
claimsProvider.setClaimTypeMap(claimTypeMap);
((JWTTokenProvider) tokenProvider).setJwtClaimsProvider(claimsProvider);
assertTrue(tokenProvider.canHandleToken(JWTTokenProvider.JWT_TOKEN_TYPE));
TokenProviderResponse providerResponse = tokenProvider.createToken(providerParameters);
assertTrue(providerResponse != null);
assertTrue(providerResponse.getToken() != null && providerResponse.getTokenId() != null);
String token = (String) providerResponse.getToken();
assertNotNull(token);
JwsJwtCompactConsumer jwtConsumer = new JwsJwtCompactConsumer(token);
JwtToken jwt = jwtConsumer.getJwtToken();
assertEquals(jwt.getClaim("roles"), "DUMMY");
}
use of org.apache.cxf.sts.common.CustomClaimsHandler in project cxf by apache.
the class JWTClaimsTest method testJWTClaims.
/**
* Test the creation of a JWTToken with various claims set by a ClaimsHandler.
*/
@org.junit.Test
public void testJWTClaims() throws Exception {
TokenProvider tokenProvider = new JWTTokenProvider();
TokenProviderParameters providerParameters = createProviderParameters(JWTTokenProvider.JWT_TOKEN_TYPE, null);
ClaimsManager claimsManager = new ClaimsManager();
ClaimsHandler claimsHandler = new CustomClaimsHandler();
claimsManager.setClaimHandlers(Collections.singletonList(claimsHandler));
providerParameters.setClaimsManager(claimsManager);
ClaimCollection claims = createClaims();
providerParameters.setRequestedPrimaryClaims(claims);
assertTrue(tokenProvider.canHandleToken(JWTTokenProvider.JWT_TOKEN_TYPE));
TokenProviderResponse providerResponse = tokenProvider.createToken(providerParameters);
assertTrue(providerResponse != null);
assertTrue(providerResponse.getToken() != null && providerResponse.getTokenId() != null);
String token = (String) providerResponse.getToken();
assertNotNull(token);
JwsJwtCompactConsumer jwtConsumer = new JwsJwtCompactConsumer(token);
JwtToken jwt = jwtConsumer.getJwtToken();
assertEquals(jwt.getClaim(ClaimTypes.EMAILADDRESS.toString()), "alice@cxf.apache.org");
assertEquals(jwt.getClaim(ClaimTypes.FIRSTNAME.toString()), "alice");
assertEquals(jwt.getClaim(ClaimTypes.LASTNAME.toString()), "doe");
}
use of org.apache.cxf.sts.common.CustomClaimsHandler in project cxf by apache.
the class SAMLClaimsTest method testSaml2MultipleClaimsSameDialect.
/**
* Test the creation of a SAML2 Assertion with various Attributes set by a ClaimsHandler.
* We have both a primary claim (sent in wst:RequestSecurityToken) and a secondary claim
* (send in wst:RequestSecurityToken/wst:SecondaryParameters), and both have the
* same dialect in this test.
*/
@org.junit.Test
public void testSaml2MultipleClaimsSameDialect() throws Exception {
TokenProvider samlTokenProvider = new SAMLTokenProvider();
TokenProviderParameters providerParameters = createProviderParameters(WSS4JConstants.WSS_SAML2_TOKEN_TYPE, STSConstants.BEARER_KEY_KEYTYPE, null);
ClaimsManager claimsManager = new ClaimsManager();
ClaimsHandler claimsHandler = new CustomClaimsHandler();
claimsManager.setClaimHandlers(Collections.singletonList(claimsHandler));
providerParameters.setClaimsManager(claimsManager);
ClaimCollection primaryClaims = createClaims();
primaryClaims.setDialect(ClaimTypes.URI_BASE);
providerParameters.setRequestedPrimaryClaims(primaryClaims);
ClaimCollection secondaryClaims = new ClaimCollection();
Claim claim = new Claim();
claim.setClaimType(ClaimTypes.STREETADDRESS);
secondaryClaims.add(claim);
secondaryClaims.setDialect(ClaimTypes.URI_BASE);
providerParameters.setRequestedSecondaryClaims(secondaryClaims);
assertTrue(samlTokenProvider.canHandleToken(WSS4JConstants.WSS_SAML2_TOKEN_TYPE));
TokenProviderResponse providerResponse = samlTokenProvider.createToken(providerParameters);
assertTrue(providerResponse != null);
assertTrue(providerResponse.getToken() != null && providerResponse.getTokenId() != null);
Element token = (Element) providerResponse.getToken();
String tokenString = DOM2Writer.nodeToString(token);
assertTrue(tokenString.contains(providerResponse.getTokenId()));
assertTrue(tokenString.contains("AttributeStatement"));
assertTrue(tokenString.contains("alice"));
assertTrue(tokenString.contains(SAML2Constants.CONF_BEARER));
assertTrue(tokenString.contains(ClaimTypes.EMAILADDRESS.toString()));
assertTrue(tokenString.contains(ClaimTypes.FIRSTNAME.toString()));
assertTrue(tokenString.contains(ClaimTypes.LASTNAME.toString()));
assertTrue(tokenString.contains(ClaimTypes.STREETADDRESS.toString()));
}
Aggregations