use of org.apache.cxf.sts.common.PasswordCallbackHandler 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
TokenRenewer tokenRenewer = new SAMLTokenRenewer();
tokenRenewer.setVerifyProofOfPossession(false);
tokenRenewer.setAllowRenewalAfterExpiry(true);
renewOperation.setTokenRenewers(Collections.singletonList(tokenRenewer));
// Add Token Validator
renewOperation.setTokenValidators(Collections.singletonList(new SAMLTokenValidator()));
// 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.common.PasswordCallbackHandler 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
TokenRenewer tokenRenewer = new SAMLTokenRenewer();
tokenRenewer.setVerifyProofOfPossession(false);
tokenRenewer.setAllowRenewalAfterExpiry(true);
renewOperation.setTokenRenewers(Collections.singletonList(tokenRenewer));
// Add Token Validator
renewOperation.setTokenValidators(Collections.singletonList(new SAMLTokenValidator()));
// 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.common.PasswordCallbackHandler in project cxf by apache.
the class ValidateJWTUnitTest method createProviderParameters.
private TokenProviderParameters createProviderParameters(String tokenType) throws WSSecurityException {
TokenProviderParameters parameters = new TokenProviderParameters();
TokenRequirements tokenRequirements = new TokenRequirements();
tokenRequirements.setTokenType(tokenType);
parameters.setTokenRequirements(tokenRequirements);
KeyRequirements keyRequirements = new KeyRequirements();
parameters.setKeyRequirements(keyRequirements);
parameters.setPrincipal(new CustomTokenPrincipal("alice"));
// Mock up message context
MessageImpl msg = new MessageImpl();
WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
parameters.setMessageContext(msgCtx);
parameters.setAppliesToAddress("http://dummy-service.com/dummy");
// Add STSProperties object
StaticSTSProperties stsProperties = new StaticSTSProperties();
Crypto crypto = CryptoFactory.getInstance(getEncryptionProperties());
stsProperties.setSignatureCrypto(crypto);
stsProperties.setSignatureUsername("mystskey");
stsProperties.setCallbackHandler(new PasswordCallbackHandler());
stsProperties.setIssuer("STS");
parameters.setStsProperties(stsProperties);
parameters.setEncryptionProperties(new EncryptionProperties());
return parameters;
}
use of org.apache.cxf.sts.common.PasswordCallbackHandler in project cxf by apache.
the class JWTProviderActAsTest method createProviderParameters.
private TokenProviderParameters createProviderParameters(String tokenType, Object actAs) throws WSSecurityException {
TokenProviderParameters parameters = new TokenProviderParameters();
TokenRequirements tokenRequirements = new TokenRequirements();
tokenRequirements.setTokenType(tokenType);
if (actAs != null) {
ReceivedToken actAsToken = new ReceivedToken(actAs);
actAsToken.setState(STATE.VALID);
tokenRequirements.setActAs(actAsToken);
}
parameters.setTokenRequirements(tokenRequirements);
KeyRequirements keyRequirements = new KeyRequirements();
parameters.setKeyRequirements(keyRequirements);
parameters.setPrincipal(new CustomTokenPrincipal("technical-user"));
// Mock up message context
MessageImpl msg = new MessageImpl();
WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
parameters.setMessageContext(msgCtx);
parameters.setAppliesToAddress("http://dummy-service.com/dummy");
// Add STSProperties object
StaticSTSProperties stsProperties = new StaticSTSProperties();
Crypto crypto = CryptoFactory.getInstance(getEncryptionProperties());
stsProperties.setSignatureCrypto(crypto);
stsProperties.setSignatureUsername("mystskey");
stsProperties.setCallbackHandler(new PasswordCallbackHandler());
stsProperties.setIssuer("STS");
parameters.setStsProperties(stsProperties);
parameters.setEncryptionProperties(new EncryptionProperties());
return parameters;
}
use of org.apache.cxf.sts.common.PasswordCallbackHandler in project cxf by apache.
the class JWTProviderOnBehalfOfTest method createProviderParameters.
private TokenProviderParameters createProviderParameters(String tokenType, Object onBehalfOf) throws WSSecurityException {
TokenProviderParameters parameters = new TokenProviderParameters();
TokenRequirements tokenRequirements = new TokenRequirements();
tokenRequirements.setTokenType(tokenType);
if (onBehalfOf != null) {
ReceivedToken onBehalfOfToken = new ReceivedToken(onBehalfOf);
onBehalfOfToken.setState(STATE.VALID);
tokenRequirements.setOnBehalfOf(onBehalfOfToken);
}
parameters.setTokenRequirements(tokenRequirements);
KeyRequirements keyRequirements = new KeyRequirements();
parameters.setKeyRequirements(keyRequirements);
parameters.setPrincipal(new CustomTokenPrincipal("alice"));
// Mock up message context
MessageImpl msg = new MessageImpl();
WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
parameters.setMessageContext(msgCtx);
parameters.setAppliesToAddress("http://dummy-service.com/dummy");
// Add STSProperties object
StaticSTSProperties stsProperties = new StaticSTSProperties();
Crypto crypto = CryptoFactory.getInstance(getEncryptionProperties());
stsProperties.setSignatureCrypto(crypto);
stsProperties.setSignatureUsername("mystskey");
stsProperties.setCallbackHandler(new PasswordCallbackHandler());
stsProperties.setIssuer("STS");
parameters.setStsProperties(stsProperties);
parameters.setEncryptionProperties(new EncryptionProperties());
return parameters;
}
Aggregations