use of org.apache.cxf.sts.token.provider.TokenProviderResponse in project cxf by apache.
the class IssueJWTOnbehalfofUnitTest method createSAMLAssertion.
/*
* Mock up an SAML assertion element
*/
private Element createSAMLAssertion(String tokenType, Crypto crypto, String signatureUsername, CallbackHandler callbackHandler, Map<String, RealmProperties> realms, String keyType) throws WSSecurityException {
SAMLTokenProvider samlTokenProvider = new SAMLTokenProvider();
samlTokenProvider.setRealmMap(realms);
TokenProviderParameters providerParameters = createProviderParameters(tokenType, keyType, crypto, signatureUsername, callbackHandler);
if (realms != null) {
providerParameters.setRealm("A");
}
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.TokenProviderResponse in project cxf by apache.
the class RenewSamlUnitTest method createSAMLAssertion.
private Element createSAMLAssertion(String tokenType, Crypto crypto, String signatureUsername, CallbackHandler callbackHandler, long ttlMs, boolean allowRenewing, boolean allowRenewingAfterExpiry) throws WSSecurityException {
SAMLTokenProvider samlTokenProvider = new SAMLTokenProvider();
DefaultConditionsProvider conditionsProvider = new DefaultConditionsProvider();
conditionsProvider.setAcceptClientLifetime(true);
samlTokenProvider.setConditionsProvider(conditionsProvider);
TokenProviderParameters providerParameters = createProviderParameters(tokenType, STSConstants.BEARER_KEY_KEYTYPE, crypto, signatureUsername, callbackHandler);
Renewing renewing = new Renewing();
renewing.setAllowRenewing(allowRenewing);
renewing.setAllowRenewingAfterExpiry(allowRenewingAfterExpiry);
providerParameters.getTokenRequirements().setRenewing(renewing);
if (ttlMs != 0) {
Lifetime lifetime = new Lifetime();
Instant creationTime = Instant.now();
Instant expirationTime = creationTime.plusNanos(ttlMs * 1000000L);
lifetime.setCreated(creationTime.atZone(ZoneOffset.UTC).format(DateUtil.getDateTimeFormatter(true)));
lifetime.setExpires(expirationTime.atZone(ZoneOffset.UTC).format(DateUtil.getDateTimeFormatter(true)));
providerParameters.getTokenRequirements().setLifetime(lifetime);
}
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.TokenProviderResponse in project cxf by apache.
the class ValidateJWTTransformationTest method testJWTToSAMLTransformation.
@org.junit.Test
public void testJWTToSAMLTransformation() throws Exception {
TokenValidateOperation validateOperation = new TokenValidateOperation();
// Add Token Validator
List<TokenValidator> validatorList = new ArrayList<>();
validatorList.add(new JWTTokenValidator());
validateOperation.setTokenValidators(validatorList);
// Add Token Provider
List<TokenProvider> providerList = new ArrayList<>();
providerList.add(new 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);
// 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);
// Create a JWTToken
TokenProviderResponse providerResponse = createJWT();
Element wrapper = createTokenWrapper((String) providerResponse.getToken());
ValidateTargetType validateTarget = new ValidateTargetType();
validateTarget.setAny(wrapper);
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("alice");
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));
}
use of org.apache.cxf.sts.token.provider.TokenProviderResponse in project cxf by apache.
the class ValidateJWTTransformationTest method createJWT.
private TokenProviderResponse createJWT() throws WSSecurityException {
TokenProvider tokenProvider = new JWTTokenProvider();
TokenProviderParameters providerParameters = createProviderParameters(JWTTokenProvider.JWT_TOKEN_TYPE);
assertTrue(tokenProvider.canHandleToken(JWTTokenProvider.JWT_TOKEN_TYPE));
TokenProviderResponse providerResponse = tokenProvider.createToken(providerParameters);
assertTrue(providerResponse != null);
assertTrue(providerResponse.getToken() != null && providerResponse.getTokenId() != null);
return providerResponse;
}
use of org.apache.cxf.sts.token.provider.TokenProviderResponse in project cxf by apache.
the class ValidateJWTTransformationTest method testJWTToSAMLTransformationRealm.
@org.junit.Test
public void testJWTToSAMLTransformationRealm() throws Exception {
TokenValidateOperation validateOperation = new TokenValidateOperation();
// Add Token Validator
List<TokenValidator> validatorList = new ArrayList<>();
JWTTokenValidator validator = new JWTTokenValidator();
validator.setRealmCodec(new CustomJWTRealmCodec());
validatorList.add(validator);
validateOperation.setTokenValidators(validatorList);
// Add Token Provider
List<TokenProvider> providerList = new ArrayList<>();
SAMLTokenProvider samlTokenProvider = new SAMLTokenProvider();
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");
stsProperties.setRealmParser(new CustomRealmParser());
stsProperties.setIdentityMapper(new CustomIdentityMapper());
validateOperation.setStsProperties(stsProperties);
// 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);
// Create a JWTToken
TokenProviderResponse providerResponse = createJWT();
Element wrapper = createTokenWrapper((String) providerResponse.getToken());
Document doc = wrapper.getOwnerDocument();
wrapper = (Element) doc.appendChild(wrapper);
ValidateTargetType validateTarget = new ValidateTargetType();
validateTarget.setAny(wrapper);
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("alice");
msgCtx.put(SecurityContext.class.getName(), createSecurityContext(principal));
msgCtx.put("url", "https");
// realm "B"
try {
validateOperation.validate(request, principal, msgCtx);
} catch (STSException ex) {
// expected
}
samlTokenProvider.setRealmMap(createSamlRealms());
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));
}
Aggregations