use of org.apache.cxf.sts.token.provider.AttributeStatementProvider in project cxf by apache.
the class IssueSamlClaimsUnitTest method addTokenProvider.
/**
* @param issueOperation
*/
private void addTokenProvider(TokenIssueOperation issueOperation) {
List<TokenProvider> providerList = new ArrayList<>();
List<AttributeStatementProvider> customProviderList = new ArrayList<>();
customProviderList.add(new CustomAttributeProvider());
SAMLTokenProvider samlTokenProvider = new SAMLTokenProvider();
samlTokenProvider.setAttributeStatementProviders(customProviderList);
providerList.add(samlTokenProvider);
issueOperation.setTokenProviders(providerList);
}
use of org.apache.cxf.sts.token.provider.AttributeStatementProvider in project cxf by apache.
the class ValidateTokenTransformationUnitTest method runUsernameTokenTransformationClaims.
/**
* Test to successfully validate a UsernameToken and transform it into a SAML Assertion with claims.
*/
private void runUsernameTokenTransformationClaims(boolean useSecondaryParameters) throws Exception {
TokenValidateOperation validateOperation = new TokenValidateOperation();
// Add Token Validator
List<TokenValidator> validatorList = new ArrayList<>();
validatorList.add(new UsernameTokenValidator());
validateOperation.setTokenValidators(validatorList);
// Add Token Provider
List<TokenProvider> providerList = new ArrayList<>();
List<AttributeStatementProvider> customProviderList = new ArrayList<>();
customProviderList.add(new CustomAttributeProvider());
SAMLTokenProvider samlTokenProvider = new SAMLTokenProvider();
samlTokenProvider.setAttributeStatementProviders(customProviderList);
providerList.add(samlTokenProvider);
validateOperation.setTokenProviders(providerList);
// Add STSProperties object
STSPropertiesMBean stsProperties = new StaticSTSProperties();
Crypto crypto = CryptoFactory.getInstance(getEncryptionProperties());
stsProperties.setEncryptionCrypto(crypto);
stsProperties.setSignatureCrypto(crypto);
stsProperties.setEncryptionUsername("myservicekey");
stsProperties.setSignatureUsername("mystskey");
stsProperties.setCallbackHandler(new PasswordCallbackHandler());
stsProperties.setIssuer("STS");
validateOperation.setStsProperties(stsProperties);
// Set the ClaimsManager
ClaimsManager claimsManager = new ClaimsManager();
ClaimsHandler claimsHandler = new CustomClaimsHandler();
claimsManager.setClaimHandlers(Collections.singletonList(claimsHandler));
validateOperation.setClaimsManager(claimsManager);
// Mock up a request
RequestSecurityTokenType request = new RequestSecurityTokenType();
JAXBElement<String> tokenType = new JAXBElement<String>(QNameConstants.TOKEN_TYPE, String.class, WSS4JConstants.WSS_SAML2_TOKEN_TYPE);
request.getAny().add(tokenType);
Object claims = useSecondaryParameters ? createClaimsElementInSecondaryParameters() : createClaimsElement();
request.getAny().add(claims);
// Create a UsernameToken
JAXBElement<UsernameTokenType> usernameTokenType = createUsernameToken("alice", "clarinet");
ValidateTargetType validateTarget = new ValidateTargetType();
validateTarget.setAny(usernameTokenType);
JAXBElement<ValidateTargetType> validateTargetType = new JAXBElement<ValidateTargetType>(QNameConstants.VALIDATE_TARGET, ValidateTargetType.class, validateTarget);
request.getAny().add(validateTargetType);
// Mock up message context
MessageImpl msg = new MessageImpl();
WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
Principal principal = new CustomTokenPrincipal("ted");
msgCtx.put(SecurityContext.class.getName(), createSecurityContext(principal));
// Validate a token
RequestSecurityTokenResponseType response = validateOperation.validate(request, principal, msgCtx);
assertTrue(validateResponse(response));
// Test the generated token.
Element assertion = null;
for (Object tokenObject : response.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"));
assertTrue(tokenString.contains("alice"));
assertTrue(tokenString.contains(SAML2Constants.CONF_BEARER));
assertTrue(tokenString.contains(ClaimTypes.LASTNAME.toString()));
}
use of org.apache.cxf.sts.token.provider.AttributeStatementProvider in project cxf by apache.
the class ValidateTokenTransformationUnitTest method testValidateSaml2TokenOnBehalfOfSaml2DifferentRealmFederateClaims.
/**
* Test to successfully validate a SAML 2 Token issued by realm "A" and
* transform it into a SAML 2 token (realm "B")
* The relationship type between realm A and B is: FederateClaims
*/
@org.junit.Test
public void testValidateSaml2TokenOnBehalfOfSaml2DifferentRealmFederateClaims() throws Exception {
TokenValidateOperation validateOperation = new TokenValidateOperation();
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);
validateOperation.setTokenProviders(providerList);
// Add Token Validator
List<TokenValidator> validatorList = new ArrayList<>();
SAMLTokenValidator samlTokenValidator = new SAMLTokenValidator();
samlTokenValidator.setSamlRealmCodec(new IssuerSAMLRealmCodec());
validatorList.add(samlTokenValidator);
validateOperation.setTokenValidators(validatorList);
// Add Service
ServiceMBean service = new StaticService();
service.setEndpoints(Collections.singletonList("http://dummy-service.com/dummy"));
validateOperation.setServices(Collections.singletonList(service));
// 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.setIdentityMapper(new CustomIdentityMapper());
stsProperties.setRelationships(relationshipList);
validateOperation.setStsProperties(stsProperties);
// Set the ClaimsManager
ClaimsManager claimsManager = new ClaimsManager();
claimsManager.setClaimHandlers(Collections.singletonList((ClaimsHandler) new CustomClaimsHandler()));
validateOperation.setClaimsManager(claimsManager);
// Mock up a request
RequestSecurityTokenType request = new RequestSecurityTokenType();
JAXBElement<String> tokenType = new JAXBElement<String>(QNameConstants.TOKEN_TYPE, String.class, WSS4JConstants.WSS_SAML2_TOKEN_TYPE);
request.getAny().add(tokenType);
// Add a ClaimsType
ClaimsType claimsType = new ClaimsType();
claimsType.setDialect(STSConstants.IDT_NS_05_05);
Document doc = DOMUtils.createDocument();
Element claimType = createClaimsType(doc);
claimsType.getAny().add(claimType);
JAXBElement<ClaimsType> claimsTypeJaxb = new JAXBElement<ClaimsType>(QNameConstants.CLAIMS, ClaimsType.class, claimsType);
request.getAny().add(claimsTypeJaxb);
// request.getAny().add(createAppliesToElement("http://dummy-service.com/dummy"));
// create a SAML Token via the SAMLTokenProvider which contains claims
CallbackHandler callbackHandler = new PasswordCallbackHandler();
Element samlToken = createSAMLAssertion(WSS4JConstants.WSS_SAML2_TOKEN_TYPE, crypto, "mystskey", callbackHandler, realms);
Document docToken = samlToken.getOwnerDocument();
samlToken = (Element) docToken.appendChild(samlToken);
String samlString = DOM2Writer.nodeToString(samlToken);
assertTrue(samlString.contains("AttributeStatement"));
assertTrue(samlString.contains("alice"));
assertTrue(samlString.contains("doe"));
assertTrue(samlString.contains(SAML2Constants.CONF_BEARER));
// Add SAML token as ValidateTarget element
ValidateTargetType validateTarget = new ValidateTargetType();
validateTarget.setAny(samlToken);
JAXBElement<ValidateTargetType> validateTargetType = new JAXBElement<ValidateTargetType>(QNameConstants.VALIDATE_TARGET, ValidateTargetType.class, validateTarget);
request.getAny().add(validateTargetType);
// Mock up message context
MessageImpl msg = new MessageImpl();
WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
msgCtx.put("url", "https");
// Validate a token
RequestSecurityTokenResponseType response = validateOperation.validate(request, null, msgCtx);
assertTrue(validateResponse(response));
// Test the generated token.
Element assertion = null;
for (Object tokenObject : response.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"));
// claim changed (to uppercase)
assertTrue(tokenString.contains("DOE"));
assertTrue(tokenString.contains(SAML2Constants.CONF_BEARER));
}
use of org.apache.cxf.sts.token.provider.AttributeStatementProvider in project cxf by apache.
the class SAMLTokenValidatorTest method createSAMLAssertionWithClaimsProvider.
private Element createSAMLAssertionWithClaimsProvider(String tokenType, Crypto crypto, String signatureUsername, CallbackHandler callbackHandler) throws WSSecurityException {
SAMLTokenProvider samlTokenProvider = new SAMLTokenProvider();
AttributeStatementProvider statementProvider = new ClaimsAttributeStatementProvider();
samlTokenProvider.setAttributeStatementProviders(Collections.singletonList(statementProvider));
TokenProviderParameters providerParameters = createProviderParameters("alice", tokenType, STSConstants.BEARER_KEY_KEYTYPE, crypto, signatureUsername, callbackHandler);
TokenProviderResponse providerResponse = samlTokenProvider.createToken(providerParameters);
assertTrue(providerResponse != null);
assertTrue(providerResponse.getToken() != null && providerResponse.getTokenId() != null);
return (Element) providerResponse.getToken();
}
use of org.apache.cxf.sts.token.provider.AttributeStatementProvider in project cxf by apache.
the class SCTSAMLTokenProvider method createCallbackHandler.
public SamlCallbackHandler createCallbackHandler(TokenProviderParameters tokenParameters, byte[] secret, Document doc) throws Exception {
// Parse the AttributeStatements
List<AttributeStatementBean> attrBeanList = null;
if (attributeStatementProviders != null && !attributeStatementProviders.isEmpty()) {
attrBeanList = new ArrayList<>();
for (AttributeStatementProvider statementProvider : attributeStatementProviders) {
AttributeStatementBean statementBean = statementProvider.getStatement(tokenParameters);
if (statementBean != null) {
LOG.fine("AttributeStatements" + statementBean.toString() + "returned by AttributeStatementProvider " + statementProvider.getClass().getName());
attrBeanList.add(statementBean);
}
}
}
// If no statements, then default to the DefaultAttributeStatementProvider
if (attrBeanList == null || attrBeanList.isEmpty()) {
attrBeanList = new ArrayList<>();
AttributeStatementProvider attributeProvider = new DefaultAttributeStatementProvider();
AttributeStatementBean attributeBean = attributeProvider.getStatement(tokenParameters);
attrBeanList.add(attributeBean);
}
// Get the Subject and Conditions
SubjectProviderParameters subjectProviderParameters = new SubjectProviderParameters();
subjectProviderParameters.setProviderParameters(tokenParameters);
subjectProviderParameters.setDoc(doc);
subjectProviderParameters.setSecret(secret);
subjectProviderParameters.setAttrBeanList(attrBeanList);
SubjectBean subjectBean = subjectProvider.getSubject(subjectProviderParameters);
ConditionsBean conditionsBean = conditionsProvider.getConditions(tokenParameters);
// Set all of the beans on the SamlCallbackHandler
SamlCallbackHandler handler = new SamlCallbackHandler();
handler.setTokenProviderParameters(tokenParameters);
handler.setSubjectBean(subjectBean);
handler.setConditionsBean(conditionsBean);
handler.setAttributeBeans(attrBeanList);
return handler;
}
Aggregations