use of org.apache.cxf.sts.operation.CustomRealmParser in project cxf by apache.
the class JexlIssueSamlClaimsTest method testIssueSaml2TokenOnBehalfOfSaml2DifferentRealmFederateClaims.
/**
* Test to successfully issue a SAML 2 token (realm "B") on-behalf-of a SAML 2 token which was issued by
* realm "A". The relationship type between realm A and B is: FederateClaims
*/
@org.junit.Test
public void testIssueSaml2TokenOnBehalfOfSaml2DifferentRealmFederateClaims() throws Exception {
TokenIssueOperation issueOperation = new TokenIssueOperation();
Map<String, RealmProperties> realms = createSamlRealms();
// Add Token Provider
List<TokenProvider> providerList = new ArrayList<>();
SAMLTokenProvider samlTokenProvider = new SAMLTokenProvider();
samlTokenProvider.setRealmMap(realms);
List<AttributeStatementProvider> customProviderList = new ArrayList<>();
customProviderList.add(new ClaimsAttributeStatementProvider());
samlTokenProvider.setAttributeStatementProviders(customProviderList);
providerList.add(samlTokenProvider);
issueOperation.setTokenProviders(providerList);
TokenDelegationHandler delegationHandler = new SAMLDelegationHandler();
issueOperation.setDelegationHandlers(Collections.singletonList(delegationHandler));
// Add Token Validator
List<TokenValidator> validatorList = new ArrayList<>();
SAMLTokenValidator samlTokenValidator = new SAMLTokenValidator();
samlTokenValidator.setSamlRealmCodec(new IssuerSAMLRealmCodec());
validatorList.add(samlTokenValidator);
issueOperation.setTokenValidators(validatorList);
addService(issueOperation);
// Add Relationship list
List<Relationship> relationshipList = new ArrayList<>();
Relationship rs = createRelationship();
relationshipList.add(rs);
// Add STSProperties object
Crypto crypto = CryptoFactory.getInstance(getEncryptionProperties());
STSPropertiesMBean stsProperties = createSTSPropertiesMBean(crypto);
stsProperties.setRealmParser(new CustomRealmParser());
stsProperties.setRelationships(relationshipList);
issueOperation.setStsProperties(stsProperties);
// Set the ClaimsManager
ClaimsManager claimsManager = new ClaimsManager();
ClaimsHandler claimsHandler = new CustomClaimsHandler();
claimsManager.setClaimHandlers(Collections.singletonList(claimsHandler));
issueOperation.setClaimsManager(claimsManager);
// Mock up a request
RequestSecurityTokenType request = createRequest(realms, crypto);
// Mock up message context
MessageImpl msg = new MessageImpl();
WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
msgCtx.put("url", "https");
List<RequestSecurityTokenResponseType> securityTokenResponseList = issueToken(issueOperation, request, null, msgCtx);
RequestSecurityTokenResponseType securityTokenResponse = securityTokenResponseList.get(0);
// Test the generated token.
Element assertion = null;
for (Object tokenObject : securityTokenResponse.getAny()) {
if (tokenObject instanceof JAXBElement<?> && REQUESTED_SECURITY_TOKEN.equals(((JAXBElement<?>) tokenObject).getName())) {
RequestedSecurityTokenType rstType = (RequestedSecurityTokenType) ((JAXBElement<?>) tokenObject).getValue();
assertion = (Element) rstType.getAny();
break;
}
}
assertNotNull(assertion);
String tokenString = DOM2Writer.nodeToString(assertion);
assertTrue(tokenString.contains("AttributeStatement"));
// subject unchanged
assertTrue(tokenString.contains("alice"));
// transformed claim (to uppercase)
assertTrue(tokenString.contains("DOE"));
assertTrue(tokenString.contains(ROLE_CLAIM.toString()));
// mapped role from admin to administrator
assertTrue(tokenString.contains("administrator"));
assertTrue(tokenString.contains("manager"));
assertFalse(tokenString.contains("user"));
assertFalse(tokenString.contains(ClaimTypes.EMAILADDRESS.toString()));
assertTrue(tokenString.contains(SAML2Constants.CONF_BEARER));
}
Aggregations