use of org.apache.cxf.sts.token.renewer.TokenRenewer in project cxf by apache.
the class TokenRenewOperation method renew.
public RequestSecurityTokenResponseType renew(RequestSecurityTokenType request, Principal principal, Map<String, Object> messageContext) {
long start = System.currentTimeMillis();
TokenRenewerParameters renewerParameters = new TokenRenewerParameters();
try {
RequestRequirements requestRequirements = parseRequest(request, messageContext);
KeyRequirements keyRequirements = requestRequirements.getKeyRequirements();
TokenRequirements tokenRequirements = requestRequirements.getTokenRequirements();
renewerParameters.setStsProperties(stsProperties);
renewerParameters.setPrincipal(principal);
renewerParameters.setMessageContext(messageContext);
renewerParameters.setTokenStore(getTokenStore());
renewerParameters.setKeyRequirements(keyRequirements);
renewerParameters.setTokenRequirements(tokenRequirements);
ReceivedToken renewTarget = tokenRequirements.getRenewTarget();
if (renewTarget == null || renewTarget.getToken() == null) {
throw new STSException("No element presented for renewal", STSException.INVALID_REQUEST);
}
renewerParameters.setToken(renewTarget);
if (tokenRequirements.getTokenType() == null) {
LOG.fine("Received TokenType is null");
}
// Get the realm of the request
String realm = null;
if (stsProperties.getRealmParser() != null) {
RealmParser realmParser = stsProperties.getRealmParser();
realm = realmParser.parseRealm(messageContext);
}
renewerParameters.setRealm(realm);
// Validate the request
TokenValidatorResponse tokenResponse = validateReceivedToken(principal, messageContext, realm, tokenRequirements, renewTarget);
if (tokenResponse == null) {
LOG.fine("No Token Validator has been found that can handle this token");
renewTarget.setState(STATE.INVALID);
throw new STSException("No Token Validator has been found that can handle this token" + tokenRequirements.getTokenType(), STSException.REQUEST_FAILED);
}
// Reject an invalid token
if (tokenResponse.getToken().getState() != STATE.EXPIRED && tokenResponse.getToken().getState() != STATE.VALID) {
LOG.fine("The token is not valid or expired, and so it cannot be renewed");
throw new STSException("No Token Validator has been found that can handle this token" + tokenRequirements.getTokenType(), STSException.REQUEST_FAILED);
}
//
// Renew the token
//
TokenRenewerResponse tokenRenewerResponse = null;
renewerParameters = createTokenRenewerParameters(requestRequirements, principal, messageContext);
Map<String, Object> additionalProperties = tokenResponse.getAdditionalProperties();
if (additionalProperties != null) {
renewerParameters.setAdditionalProperties(additionalProperties);
}
renewerParameters.setRealm(tokenResponse.getTokenRealm());
renewerParameters.setToken(tokenResponse.getToken());
realm = tokenResponse.getTokenRealm();
for (TokenRenewer tokenRenewer : tokenRenewers) {
boolean canHandle = false;
if (realm == null) {
canHandle = tokenRenewer.canHandleToken(tokenResponse.getToken());
} else {
canHandle = tokenRenewer.canHandleToken(tokenResponse.getToken(), realm);
}
if (canHandle) {
try {
tokenRenewerResponse = tokenRenewer.renewToken(renewerParameters);
} catch (STSException ex) {
LOG.log(Level.WARNING, "", ex);
throw ex;
} catch (RuntimeException ex) {
LOG.log(Level.WARNING, "", ex);
throw new STSException("Error in providing a token", ex, STSException.REQUEST_FAILED);
}
break;
}
}
if (tokenRenewerResponse == null || tokenRenewerResponse.getToken() == null) {
LOG.fine("No Token Renewer has been found that can handle this token");
throw new STSException("No token renewer found for requested token type", STSException.REQUEST_FAILED);
}
// prepare response
try {
EncryptionProperties encryptionProperties = renewerParameters.getEncryptionProperties();
RequestSecurityTokenResponseType response = createResponse(encryptionProperties, tokenRenewerResponse, tokenRequirements, keyRequirements);
STSRenewSuccessEvent event = new STSRenewSuccessEvent(renewerParameters, System.currentTimeMillis() - start);
publishEvent(event);
return response;
} catch (Throwable ex) {
LOG.log(Level.WARNING, "", ex);
throw new STSException("Error in creating the response", ex, STSException.REQUEST_FAILED);
}
} catch (RuntimeException ex) {
STSRenewFailureEvent event = new STSRenewFailureEvent(renewerParameters, System.currentTimeMillis() - start, ex);
publishEvent(event);
throw ex;
}
}
use of org.apache.cxf.sts.token.renewer.TokenRenewer in project cxf by apache.
the class RenewSamlUnitTest method testRenewExpiredSaml1Token.
/**
* Test to successfully renew an expired Saml 1.1 token.
*/
@org.junit.Test
public void testRenewExpiredSaml1Token() throws Exception {
TokenRenewOperation renewOperation = new TokenRenewOperation();
renewOperation.setTokenStore(tokenStore);
// Add Token Renewer
List<TokenRenewer> renewerList = new ArrayList<>();
TokenRenewer tokenRenewer = new SAMLTokenRenewer();
tokenRenewer.setVerifyProofOfPossession(false);
tokenRenewer.setAllowRenewalAfterExpiry(true);
renewerList.add(tokenRenewer);
renewOperation.setTokenRenewers(renewerList);
// Add Token Validator
List<TokenValidator> validatorList = new ArrayList<>();
validatorList.add(new SAMLTokenValidator());
renewOperation.setTokenValidators(validatorList);
// 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");
renewOperation.setStsProperties(stsProperties);
// Mock up a request
RequestSecurityTokenType request = new RequestSecurityTokenType();
JAXBElement<String> tokenType = new JAXBElement<String>(QNameConstants.TOKEN_TYPE, String.class, STSConstants.BEARER_KEY_KEYTYPE);
request.getAny().add(tokenType);
// Get a SAML Token via the SAMLTokenProvider
CallbackHandler callbackHandler = new PasswordCallbackHandler();
Element samlToken = createSAMLAssertion(WSS4JConstants.WSS_SAML_TOKEN_TYPE, crypto, "mystskey", callbackHandler, 50, true, true);
// Sleep to expire the token
Thread.sleep(100);
Document doc = samlToken.getOwnerDocument();
samlToken = (Element) doc.appendChild(samlToken);
RenewTargetType renewTarget = new RenewTargetType();
renewTarget.setAny(samlToken);
JAXBElement<RenewTargetType> renewTargetType = new JAXBElement<RenewTargetType>(QNameConstants.RENEW_TARGET, RenewTargetType.class, renewTarget);
request.getAny().add(renewTargetType);
// Mock up message context
MessageImpl msg = new MessageImpl();
WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
Principal principal = new CustomTokenPrincipal("alice");
msgCtx.put(SecurityContext.class.getName(), createSecurityContext(principal));
// Renew a token
RequestSecurityTokenResponseType response = renewOperation.renew(request, principal, msgCtx);
assertTrue(response != null && response.getAny() != null && !response.getAny().isEmpty());
// 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(SAML1Constants.CONF_BEARER));
}
use of org.apache.cxf.sts.token.renewer.TokenRenewer in project cxf by apache.
the class RenewSamlUnitTest method testRenewExpiredSaml2TokenNoCacheNoTokenType.
/**
* Test to successfully renew an expired Saml 2 token and sending no TokenType.
*/
@org.junit.Test
public void testRenewExpiredSaml2TokenNoCacheNoTokenType() throws Exception {
TokenRenewOperation renewOperation = new TokenRenewOperation();
renewOperation.setTokenStore(tokenStore);
// Add Token Renewer
List<TokenRenewer> renewerList = new ArrayList<>();
TokenRenewer tokenRenewer = new SAMLTokenRenewer();
tokenRenewer.setVerifyProofOfPossession(false);
tokenRenewer.setAllowRenewalAfterExpiry(true);
renewerList.add(tokenRenewer);
renewOperation.setTokenRenewers(renewerList);
// Add Token Validator
List<TokenValidator> validatorList = new ArrayList<>();
validatorList.add(new SAMLTokenValidator());
renewOperation.setTokenValidators(validatorList);
// 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");
renewOperation.setStsProperties(stsProperties);
// Mock up a request
RequestSecurityTokenType request = new RequestSecurityTokenType();
// Get a SAML Token via the SAMLTokenProvider
CallbackHandler callbackHandler = new PasswordCallbackHandler();
Element samlToken = createSAMLAssertion(WSS4JConstants.WSS_SAML2_TOKEN_TYPE, crypto, "mystskey", callbackHandler, 50, true, true);
// Sleep to expire the token
Thread.sleep(100);
Document doc = samlToken.getOwnerDocument();
samlToken = (Element) doc.appendChild(samlToken);
RenewTargetType renewTarget = new RenewTargetType();
renewTarget.setAny(samlToken);
JAXBElement<RenewTargetType> renewTargetType = new JAXBElement<RenewTargetType>(QNameConstants.RENEW_TARGET, RenewTargetType.class, renewTarget);
request.getAny().add(renewTargetType);
// Mock up message context
MessageImpl msg = new MessageImpl();
WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
Principal principal = new CustomTokenPrincipal("alice");
msgCtx.put(SecurityContext.class.getName(), createSecurityContext(principal));
// Renew a token
RequestSecurityTokenResponseType response = renewOperation.renew(request, principal, msgCtx);
assertTrue(response != null && response.getAny() != null && !response.getAny().isEmpty());
// 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));
}
use of org.apache.cxf.sts.token.renewer.TokenRenewer in project cxf by apache.
the class RenewSamlUnitTest method testRenewValidSaml1Token.
/**
* Test to successfully renew a valid Saml 1.1 token
*/
@org.junit.Test
public void testRenewValidSaml1Token() throws Exception {
TokenRenewOperation renewOperation = new TokenRenewOperation();
renewOperation.setTokenStore(tokenStore);
// Add Token Renewer
List<TokenRenewer> renewerList = new ArrayList<>();
TokenRenewer tokenRenewer = new SAMLTokenRenewer();
tokenRenewer.setVerifyProofOfPossession(false);
renewerList.add(tokenRenewer);
renewOperation.setTokenRenewers(renewerList);
// Add Token Validator
List<TokenValidator> validatorList = new ArrayList<>();
validatorList.add(new SAMLTokenValidator());
renewOperation.setTokenValidators(validatorList);
// 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");
renewOperation.setStsProperties(stsProperties);
// Mock up a request
RequestSecurityTokenType request = new RequestSecurityTokenType();
JAXBElement<String> tokenType = new JAXBElement<String>(QNameConstants.TOKEN_TYPE, String.class, STSConstants.BEARER_KEY_KEYTYPE);
request.getAny().add(tokenType);
// Get a SAML Token via the SAMLTokenProvider
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);
RenewTargetType renewTarget = new RenewTargetType();
renewTarget.setAny(samlToken);
JAXBElement<RenewTargetType> renewTargetType = new JAXBElement<RenewTargetType>(QNameConstants.RENEW_TARGET, RenewTargetType.class, renewTarget);
request.getAny().add(renewTargetType);
// Mock up message context
MessageImpl msg = new MessageImpl();
WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
Principal principal = new CustomTokenPrincipal("alice");
msgCtx.put(SecurityContext.class.getName(), createSecurityContext(principal));
// Renew a token
RequestSecurityTokenResponseType response = renewOperation.renew(request, principal, msgCtx);
assertTrue(response != null && response.getAny() != null && !response.getAny().isEmpty());
// 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(SAML1Constants.CONF_BEARER));
}
use of org.apache.cxf.sts.token.renewer.TokenRenewer in project cxf by apache.
the class RenewSamlUnitTest method testRenewExpiredSaml2Token.
/**
* Test to successfully renew an expired Saml 2 token.
*/
@org.junit.Test
public void testRenewExpiredSaml2Token() throws Exception {
TokenRenewOperation renewOperation = new TokenRenewOperation();
renewOperation.setTokenStore(tokenStore);
// Add Token Renewer
List<TokenRenewer> renewerList = new ArrayList<>();
TokenRenewer tokenRenewer = new SAMLTokenRenewer();
tokenRenewer.setVerifyProofOfPossession(false);
tokenRenewer.setAllowRenewalAfterExpiry(true);
renewerList.add(tokenRenewer);
renewOperation.setTokenRenewers(renewerList);
// Add Token Validator
List<TokenValidator> validatorList = new ArrayList<>();
validatorList.add(new SAMLTokenValidator());
renewOperation.setTokenValidators(validatorList);
// 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");
renewOperation.setStsProperties(stsProperties);
// Mock up a request
RequestSecurityTokenType request = new RequestSecurityTokenType();
JAXBElement<String> tokenType = new JAXBElement<String>(QNameConstants.TOKEN_TYPE, String.class, STSConstants.BEARER_KEY_KEYTYPE);
request.getAny().add(tokenType);
// Get a SAML Token via the SAMLTokenProvider
CallbackHandler callbackHandler = new PasswordCallbackHandler();
Element samlToken = createSAMLAssertion(WSS4JConstants.WSS_SAML2_TOKEN_TYPE, crypto, "mystskey", callbackHandler, 50, true, true);
// Sleep to expire the token
Thread.sleep(100);
Document doc = samlToken.getOwnerDocument();
samlToken = (Element) doc.appendChild(samlToken);
RenewTargetType renewTarget = new RenewTargetType();
renewTarget.setAny(samlToken);
JAXBElement<RenewTargetType> renewTargetType = new JAXBElement<RenewTargetType>(QNameConstants.RENEW_TARGET, RenewTargetType.class, renewTarget);
request.getAny().add(renewTargetType);
// Mock up message context
MessageImpl msg = new MessageImpl();
WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
Principal principal = new CustomTokenPrincipal("alice");
msgCtx.put(SecurityContext.class.getName(), createSecurityContext(principal));
// Renew a token
RequestSecurityTokenResponseType response = renewOperation.renew(request, principal, msgCtx);
assertTrue(response != null && response.getAny() != null && !response.getAny().isEmpty());
// 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));
}
Aggregations