use of org.apache.cxf.sts.token.provider.TokenProviderResponse in project cxf by apache.
the class ValidateJWTUnitTest method testValidateJWT.
@org.junit.Test
public void testValidateJWT() throws Exception {
TokenValidateOperation validateOperation = new TokenValidateOperation();
// Add Token Validator
List<TokenValidator> validatorList = new ArrayList<>();
validatorList.add(new JWTTokenValidator());
validateOperation.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");
validateOperation.setStsProperties(stsProperties);
// Mock up a request
RequestSecurityTokenType request = new RequestSecurityTokenType();
JAXBElement<String> tokenType = new JAXBElement<String>(QNameConstants.TOKEN_TYPE, String.class, STSConstants.STATUS);
request.getAny().add(tokenType);
// Get a JWTToken via the JWTTokenProvider
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));
}
use of org.apache.cxf.sts.token.provider.TokenProviderResponse in project cxf by apache.
the class ValidateJWTUnitTest 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 ValidateSCTUnitTest method testValidateSCT.
/**
* Test to successfully validate a SecurityContextToken
*/
@org.junit.Test
public void testValidateSCT() throws Exception {
TokenValidateOperation validateOperation = new TokenValidateOperation();
validateOperation.setTokenStore(tokenStore);
// Add Token Validator
List<TokenValidator> validatorList = new ArrayList<>();
validatorList.add(new SCTValidator());
validateOperation.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");
validateOperation.setStsProperties(stsProperties);
// Mock up a request
RequestSecurityTokenType request = new RequestSecurityTokenType();
JAXBElement<String> tokenType = new JAXBElement<String>(QNameConstants.TOKEN_TYPE, String.class, STSConstants.STATUS);
request.getAny().add(tokenType);
// Get a SecurityContextToken via the SCTProvider
TokenProviderResponse providerResponse = createSCT();
Element sct = (Element) providerResponse.getToken();
ValidateTargetType validateTarget = new ValidateTargetType();
validateTarget.setAny(sct);
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));
// Now remove the token from the cache before validating again
tokenStore.remove(tokenStore.getToken(providerResponse.getTokenId()).getId());
assertNull(tokenStore.getToken(providerResponse.getTokenId()));
response = validateOperation.validate(request, principal, msgCtx);
assertFalse(validateResponse(response));
}
use of org.apache.cxf.sts.token.provider.TokenProviderResponse in project cxf by apache.
the class ValidateSCTUnitTest method createSCT.
private TokenProviderResponse createSCT() throws WSSecurityException {
TokenProvider sctTokenProvider = new SCTProvider();
TokenProviderParameters providerParameters = createProviderParameters(STSUtils.TOKEN_TYPE_SCT_05_12);
assertTrue(sctTokenProvider.canHandleToken(STSUtils.TOKEN_TYPE_SCT_05_12));
TokenProviderResponse providerResponse = sctTokenProvider.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 SCTCancellerTest method testCancelToken.
/**
* Get a (valid) SecurityContextToken and successfully cancel it.
*/
@org.junit.Test
public void testCancelToken() throws Exception {
TokenCanceller sctCanceller = new SCTCanceller();
sctCanceller.setVerifyProofOfPossession(false);
TokenCancellerParameters cancellerParameters = createCancellerParameters();
TokenRequirements tokenRequirements = cancellerParameters.getTokenRequirements();
// Create a CancelTarget consisting of a SecurityContextToken
TokenProviderResponse providerResponse = getSecurityContextToken();
ReceivedToken cancelTarget = new ReceivedToken(providerResponse.getToken());
tokenRequirements.setCancelTarget(cancelTarget);
cancellerParameters.setToken(cancelTarget);
assertTrue(sctCanceller.canHandleToken(cancelTarget));
TokenCancellerResponse cancellerResponse = sctCanceller.cancelToken(cancellerParameters);
assertTrue(cancellerResponse != null);
assertTrue(cancellerResponse.getToken().getState() == STATE.CANCELLED);
// Try to cancel the token again - this should fail
cancellerResponse = sctCanceller.cancelToken(cancellerParameters);
assertTrue(cancellerResponse != null);
assertFalse(cancellerResponse.getToken().getState() == STATE.CANCELLED);
}
Aggregations