use of org.apache.cxf.rt.security.claims.ClaimCollection 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);
assertNotNull(providerResponse);
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.rt.security.claims.ClaimCollection 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);
assertNotNull(providerResponse);
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.rt.security.claims.ClaimCollection in project cxf by apache.
the class SAMLClaimsTest method testSaml2SeparateClaimsMultipleClaimsHandlers.
/**
* Here we have two ClaimsHandlers that can both add claims for the same claim type. We configure the
* SAMLTokenProvider not to combine the claims unlike the test above.
*/
@org.junit.Test
public void testSaml2SeparateClaimsMultipleClaimsHandlers() throws Exception {
TokenProvider samlTokenProvider = new SAMLTokenProvider();
((SAMLTokenProvider) samlTokenProvider).setCombineClaimAttributes(false);
TokenProviderParameters providerParameters = createProviderParameters(WSS4JConstants.WSS_SAML2_TOKEN_TYPE, STSConstants.BEARER_KEY_KEYTYPE, null);
ClaimsManager claimsManager = new ClaimsManager();
ClaimsHandler claimsHandler = new CustomClaimsHandler();
ClaimsHandler claimsHandler2 = new CustomClaimsHandler();
((CustomClaimsHandler) claimsHandler2).setRole("CustomRole");
claimsManager.setClaimHandlers(Arrays.asList(claimsHandler, claimsHandler2));
providerParameters.setClaimsManager(claimsManager);
ClaimCollection claims = new ClaimCollection();
Claim claim = new Claim();
claim.setClaimType(CustomClaimsHandler.ROLE_CLAIM);
claims.add(claim);
providerParameters.setRequestedPrimaryClaims(claims);
assertTrue(samlTokenProvider.canHandleToken(WSS4JConstants.WSS_SAML2_TOKEN_TYPE));
TokenProviderResponse providerResponse = samlTokenProvider.createToken(providerParameters);
assertNotNull(providerResponse);
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"));
String requiredClaim = CustomClaimsHandler.ROLE_CLAIM.toString();
assertTrue(tokenString.contains(requiredClaim));
assertTrue(tokenString.contains("DUMMY"));
assertTrue(tokenString.contains("CustomRole"));
// Check we have two Role Claims
assertNotEquals(tokenString.indexOf(requiredClaim), tokenString.lastIndexOf(requiredClaim));
}
use of org.apache.cxf.rt.security.claims.ClaimCollection in project cxf by apache.
the class SAMLClaimsTest method testSaml2StaticClaims.
/**
* Test the creation of a SAML2 Assertion with StaticClaimsHandler
*/
@org.junit.Test
public void testSaml2StaticClaims() throws Exception {
TokenProvider samlTokenProvider = new SAMLTokenProvider();
TokenProviderParameters providerParameters = createProviderParameters(WSS4JConstants.WSS_SAML2_TOKEN_TYPE, STSConstants.BEARER_KEY_KEYTYPE, null);
ClaimsManager claimsManager = new ClaimsManager();
StaticClaimsHandler claimsHandler = new StaticClaimsHandler();
Map<String, String> staticClaimsMap = new HashMap<>();
staticClaimsMap.put(CLAIM_STATIC_COMPANY, CLAIM_STATIC_COMPANY_VALUE);
claimsHandler.setGlobalClaims(staticClaimsMap);
claimsManager.setClaimHandlers(Collections.singletonList((ClaimsHandler) claimsHandler));
providerParameters.setClaimsManager(claimsManager);
ClaimCollection claims = new ClaimCollection();
Claim claim = new Claim();
claim.setClaimType(CLAIM_STATIC_COMPANY);
claims.add(claim);
providerParameters.setRequestedPrimaryClaims(claims);
assertTrue(samlTokenProvider.canHandleToken(WSS4JConstants.WSS_SAML2_TOKEN_TYPE));
TokenProviderResponse providerResponse = samlTokenProvider.createToken(providerParameters);
assertNotNull(providerResponse);
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));
SamlAssertionWrapper assertion = new SamlAssertionWrapper(token);
List<Attribute> attributes = assertion.getSaml2().getAttributeStatements().get(0).getAttributes();
assertEquals(attributes.size(), 1);
assertEquals(attributes.get(0).getName(), CLAIM_STATIC_COMPANY);
XMLObject valueObj = attributes.get(0).getAttributeValues().get(0);
assertEquals(valueObj.getDOM().getTextContent(), CLAIM_STATIC_COMPANY_VALUE);
}
use of org.apache.cxf.rt.security.claims.ClaimCollection in project cxf by apache.
the class SAMLClaimsTest method testSaml2CombinedClaimsMultipleClaimsHandlers.
/**
* Here we have two ClaimsHandlers that can both add claims for the same claim type. By default we will
* combine the Claims into one Attribute.
*/
@org.junit.Test
public void testSaml2CombinedClaimsMultipleClaimsHandlers() 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();
ClaimsHandler claimsHandler2 = new CustomClaimsHandler();
((CustomClaimsHandler) claimsHandler2).setRole("CustomRole");
claimsManager.setClaimHandlers(Arrays.asList(claimsHandler, claimsHandler2));
providerParameters.setClaimsManager(claimsManager);
ClaimCollection claims = new ClaimCollection();
Claim claim = new Claim();
claim.setClaimType(CustomClaimsHandler.ROLE_CLAIM);
claims.add(claim);
providerParameters.setRequestedPrimaryClaims(claims);
assertTrue(samlTokenProvider.canHandleToken(WSS4JConstants.WSS_SAML2_TOKEN_TYPE));
TokenProviderResponse providerResponse = samlTokenProvider.createToken(providerParameters);
assertNotNull(providerResponse);
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"));
String requiredClaim = CustomClaimsHandler.ROLE_CLAIM.toString();
assertTrue(tokenString.contains(requiredClaim));
assertTrue(tokenString.contains("DUMMY"));
assertTrue(tokenString.contains("CustomRole"));
// Check only one Role Claim
assertEquals(tokenString.indexOf(requiredClaim), tokenString.lastIndexOf(requiredClaim));
}
Aggregations