use of org.apache.cxf.sts.token.validator.TokenValidator in project OpenAM by OpenRock.
the class TokenValidateOperationProvider method getFunctionalValidateOperation.
private ValidateOperation getFunctionalValidateOperation() {
TokenValidateOperation tokenValidateOperation = new TokenValidateOperation();
tokenValidateOperation.setStsProperties(stsPropertiesMBean);
tokenValidateOperation.setTokenStore(tokenStore);
try {
List<TokenValidator> tokenValidators = new ArrayList<>();
for (TokenType tokentype : validatedTokens) {
tokenValidators.add(operationFactory.getSimpleTokenValidator(tokentype));
}
tokenValidateOperation.setTokenValidators(tokenValidators);
} catch (STSInitializationException e) {
throw new RuntimeException(e);
}
return new TokenValidateOperationWrapper(tokenValidateOperation, threadLocalAMTokenCache);
}
use of org.apache.cxf.sts.token.validator.TokenValidator in project cxf by apache.
the class DefaultSecurityTokenServiceProvider method populateAbstractOperation.
private void populateAbstractOperation(AbstractOperation abstractOperation) {
if (stsProperties == null) {
LOG.warning("No 'stsProperties' configured on the DefaultSecurityTokenServiceProvider");
return;
}
List<TokenProvider> tokenProviders = new ArrayList<>();
tokenProviders.add(new SAMLTokenProvider());
List<TokenValidator> tokenValidators = new ArrayList<>();
tokenValidators.add(new SAMLTokenValidator());
tokenValidators.add(new UsernameTokenValidator());
tokenValidators.add(new X509TokenValidator());
abstractOperation.setTokenProviders(tokenProviders);
abstractOperation.setTokenValidators(tokenValidators);
abstractOperation.setStsProperties(stsProperties);
abstractOperation.setEncryptIssuedToken(encryptIssuedToken);
abstractOperation.setServices(services);
abstractOperation.setReturnReferences(returnReferences);
abstractOperation.setTokenStore(tokenStore);
abstractOperation.setClaimsManager(claimsManager);
abstractOperation.setEventListener(eventListener);
}
use of org.apache.cxf.sts.token.validator.TokenValidator in project cxf by apache.
the class SAMLTokenRenewerPOPTest method renewValidSAML1Assertion.
/**
* Renew a valid SAML1 Assertion
*/
@org.junit.Test
public void renewValidSAML1Assertion() throws Exception {
// Create the Assertion
Crypto crypto = CryptoFactory.getInstance(getEncryptionProperties());
CallbackHandler callbackHandler = new PasswordCallbackHandler();
Element samlToken = createSAMLAssertion(WSS4JConstants.WSS_SAML_TOKEN_TYPE, crypto, "mystskey", callbackHandler, 50000, true, false);
Document doc = samlToken.getOwnerDocument();
samlToken = (Element) doc.appendChild(samlToken);
// Validate the Assertion
TokenValidator samlTokenValidator = new SAMLTokenValidator();
TokenValidatorParameters validatorParameters = createValidatorParameters();
TokenRequirements tokenRequirements = validatorParameters.getTokenRequirements();
ReceivedToken validateTarget = new ReceivedToken(samlToken);
tokenRequirements.setValidateTarget(validateTarget);
validatorParameters.setToken(validateTarget);
assertTrue(samlTokenValidator.canHandleToken(validateTarget));
TokenValidatorResponse validatorResponse = samlTokenValidator.validateToken(validatorParameters);
assertNotNull(validatorResponse);
assertNotNull(validatorResponse.getToken());
assertTrue(validatorResponse.getToken().getState() == STATE.VALID);
// Renew the Assertion
TokenRenewerParameters renewerParameters = new TokenRenewerParameters();
renewerParameters.setAppliesToAddress("http://dummy-service.com/dummy");
renewerParameters.setStsProperties(validatorParameters.getStsProperties());
renewerParameters.setPrincipal(new CustomTokenPrincipal("alice"));
renewerParameters.setMessageContext(validatorParameters.getMessageContext());
renewerParameters.setKeyRequirements(validatorParameters.getKeyRequirements());
renewerParameters.setTokenRequirements(validatorParameters.getTokenRequirements());
renewerParameters.setTokenStore(validatorParameters.getTokenStore());
renewerParameters.setToken(validatorResponse.getToken());
TokenRenewer samlTokenRenewer = new SAMLTokenRenewer();
assertTrue(samlTokenRenewer.canHandleToken(validatorResponse.getToken()));
try {
samlTokenRenewer.renewToken(renewerParameters);
fail("Expected failure on lack of proof of possession");
} catch (Exception ex) {
// expected
}
WSSecurityEngineResult signedResult = new WSSecurityEngineResult(WSConstants.SIGN);
CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS);
cryptoType.setAlias("myclientkey");
signedResult.put(WSSecurityEngineResult.TAG_X509_CERTIFICATES, crypto.getX509Certificates(cryptoType));
List<WSSecurityEngineResult> signedResults = Collections.singletonList(signedResult);
WSHandlerResult handlerResult = new WSHandlerResult(null, signedResults, Collections.singletonMap(WSConstants.SIGN, signedResults));
Map<String, Object> messageContext = validatorParameters.getMessageContext();
messageContext.put(WSHandlerConstants.RECV_RESULTS, Collections.singletonList(handlerResult));
// Now successfully renew the token
TokenRenewerResponse renewerResponse = samlTokenRenewer.renewToken(renewerParameters);
assertNotNull(renewerResponse);
assertNotNull(renewerResponse.getToken());
}
use of org.apache.cxf.sts.token.validator.TokenValidator in project cxf by apache.
the class SAMLTokenRenewerTest method renewNotAllowedOfValidSAML1Assertion.
/**
* Renew a valid SAML1 Assertion. However, the issuer does not allow renewal
*/
@org.junit.Test
public void renewNotAllowedOfValidSAML1Assertion() throws Exception {
// Create the Assertion
Crypto crypto = CryptoFactory.getInstance(getEncryptionProperties());
CallbackHandler callbackHandler = new PasswordCallbackHandler();
Element samlToken = createSAMLAssertion(WSS4JConstants.WSS_SAML_TOKEN_TYPE, crypto, "mystskey", callbackHandler, 50000, false, false);
Document doc = samlToken.getOwnerDocument();
samlToken = (Element) doc.appendChild(samlToken);
// Validate the Assertion
TokenValidator samlTokenValidator = new SAMLTokenValidator();
TokenValidatorParameters validatorParameters = createValidatorParameters();
TokenRequirements tokenRequirements = validatorParameters.getTokenRequirements();
ReceivedToken validateTarget = new ReceivedToken(samlToken);
tokenRequirements.setValidateTarget(validateTarget);
validatorParameters.setToken(validateTarget);
assertTrue(samlTokenValidator.canHandleToken(validateTarget));
TokenValidatorResponse validatorResponse = samlTokenValidator.validateToken(validatorParameters);
assertNotNull(validatorResponse);
assertNotNull(validatorResponse.getToken());
assertTrue(validatorResponse.getToken().getState() == STATE.VALID);
// Renew the Assertion
TokenRenewerParameters renewerParameters = new TokenRenewerParameters();
renewerParameters.setAppliesToAddress("http://dummy-service.com/dummy");
renewerParameters.setStsProperties(validatorParameters.getStsProperties());
renewerParameters.setPrincipal(new CustomTokenPrincipal("alice"));
renewerParameters.setMessageContext(validatorParameters.getMessageContext());
renewerParameters.setKeyRequirements(validatorParameters.getKeyRequirements());
renewerParameters.setTokenRequirements(validatorParameters.getTokenRequirements());
renewerParameters.setTokenStore(validatorParameters.getTokenStore());
renewerParameters.setToken(validatorResponse.getToken());
TokenRenewer samlTokenRenewer = new SAMLTokenRenewer();
samlTokenRenewer.setVerifyProofOfPossession(false);
assertTrue(samlTokenRenewer.canHandleToken(validatorResponse.getToken()));
try {
samlTokenRenewer.renewToken(renewerParameters);
fail("Failure expected on attempting to renew a token that was not allowed to be renewed");
} catch (Exception ex) {
// expected
}
}
use of org.apache.cxf.sts.token.validator.TokenValidator in project cxf by apache.
the class SAMLTokenRenewerTest method renewExpiredNotAllowedSAML2Assertion.
/**
* Renew an expired SAML2 Assertion. However the issuer does not allow the renewal of expired
* tokens.
*/
@org.junit.Test
public void renewExpiredNotAllowedSAML2Assertion() throws Exception {
// Create the Assertion
Crypto crypto = CryptoFactory.getInstance(getEncryptionProperties());
CallbackHandler callbackHandler = new PasswordCallbackHandler();
Element samlToken = createSAMLAssertion(WSS4JConstants.WSS_SAML2_TOKEN_TYPE, crypto, "mystskey", callbackHandler, 50, true, false);
Document doc = samlToken.getOwnerDocument();
samlToken = (Element) doc.appendChild(samlToken);
// Sleep to expire the token
Thread.sleep(100);
// Validate the Assertion
TokenValidator samlTokenValidator = new SAMLTokenValidator();
TokenValidatorParameters validatorParameters = createValidatorParameters();
TokenRequirements tokenRequirements = validatorParameters.getTokenRequirements();
ReceivedToken validateTarget = new ReceivedToken(samlToken);
tokenRequirements.setValidateTarget(validateTarget);
validatorParameters.setToken(validateTarget);
assertTrue(samlTokenValidator.canHandleToken(validateTarget));
TokenValidatorResponse validatorResponse = samlTokenValidator.validateToken(validatorParameters);
assertNotNull(validatorResponse);
assertNotNull(validatorResponse.getToken());
assertTrue(validatorResponse.getToken().getState() == STATE.EXPIRED);
// Renew the Assertion
TokenRenewerParameters renewerParameters = new TokenRenewerParameters();
renewerParameters.setAppliesToAddress("http://dummy-service.com/dummy");
renewerParameters.setStsProperties(validatorParameters.getStsProperties());
renewerParameters.setPrincipal(new CustomTokenPrincipal("alice"));
renewerParameters.setMessageContext(validatorParameters.getMessageContext());
renewerParameters.setKeyRequirements(validatorParameters.getKeyRequirements());
renewerParameters.setTokenRequirements(validatorParameters.getTokenRequirements());
renewerParameters.setTokenStore(validatorParameters.getTokenStore());
renewerParameters.setToken(validatorResponse.getToken());
TokenRenewer samlTokenRenewer = new SAMLTokenRenewer();
samlTokenRenewer.setVerifyProofOfPossession(false);
samlTokenRenewer.setAllowRenewalAfterExpiry(true);
assertTrue(samlTokenRenewer.canHandleToken(validatorResponse.getToken()));
try {
samlTokenRenewer.renewToken(renewerParameters);
fail("Failure on attempting to renew an expired token, which is not allowed");
} catch (Exception ex) {
// expected
}
}
Aggregations