use of org.apache.cxf.sts.request.ReceivedToken in project cxf by apache.
the class X509TokenValidatorTest method testInvalidCertificate.
/**
* Test an invalid certificate
*/
@org.junit.Test
public void testInvalidCertificate() throws Exception {
TokenValidator x509TokenValidator = new X509TokenValidator();
TokenValidatorParameters validatorParameters = createValidatorParameters();
TokenRequirements tokenRequirements = validatorParameters.getTokenRequirements();
// Create a ValidateTarget consisting of an X509Certificate
BinarySecurityTokenType binarySecurityToken = new BinarySecurityTokenType();
JAXBElement<BinarySecurityTokenType> tokenType = new JAXBElement<BinarySecurityTokenType>(QNameConstants.BINARY_SECURITY_TOKEN, BinarySecurityTokenType.class, binarySecurityToken);
CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS);
cryptoType.setAlias("eve");
Crypto crypto = CryptoFactory.getInstance(getEveCryptoProperties());
X509Certificate[] certs = crypto.getX509Certificates(cryptoType);
assertTrue(certs != null && certs.length > 0);
binarySecurityToken.setValue(Base64.getMimeEncoder().encodeToString(certs[0].getEncoded()));
binarySecurityToken.setValueType(X509TokenValidator.X509_V3_TYPE);
binarySecurityToken.setEncodingType(WSS4JConstants.SOAPMESSAGE_NS + "#Base64Binary");
ReceivedToken validateTarget = new ReceivedToken(tokenType);
tokenRequirements.setValidateTarget(validateTarget);
validatorParameters.setToken(validateTarget);
assertTrue(x509TokenValidator.canHandleToken(validateTarget));
TokenValidatorResponse validatorResponse = x509TokenValidator.validateToken(validatorParameters);
assertTrue(validatorResponse != null);
assertTrue(validatorResponse.getToken() != null);
assertTrue(validatorResponse.getToken().getState() == STATE.INVALID);
}
use of org.apache.cxf.sts.request.ReceivedToken in project cxf by apache.
the class X509TokenValidatorTest method testValidCertificate.
/**
* Test a valid certificate
*/
@org.junit.Test
public void testValidCertificate() throws Exception {
TokenValidator x509TokenValidator = new X509TokenValidator();
TokenValidatorParameters validatorParameters = createValidatorParameters();
TokenRequirements tokenRequirements = validatorParameters.getTokenRequirements();
// Create a ValidateTarget consisting of an X509Certificate
BinarySecurityTokenType binarySecurityToken = new BinarySecurityTokenType();
JAXBElement<BinarySecurityTokenType> tokenType = new JAXBElement<BinarySecurityTokenType>(QNameConstants.BINARY_SECURITY_TOKEN, BinarySecurityTokenType.class, binarySecurityToken);
CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS);
cryptoType.setAlias("myclientkey");
Crypto crypto = validatorParameters.getStsProperties().getSignatureCrypto();
X509Certificate[] certs = crypto.getX509Certificates(cryptoType);
assertTrue(certs != null && certs.length > 0);
binarySecurityToken.setValue(Base64.getMimeEncoder().encodeToString(certs[0].getEncoded()));
ReceivedToken validateTarget = new ReceivedToken(tokenType);
tokenRequirements.setValidateTarget(validateTarget);
validatorParameters.setToken(validateTarget);
// It can't handle the token as the value type is not set
assertFalse(x509TokenValidator.canHandleToken(validateTarget));
binarySecurityToken.setValueType(X509TokenValidator.X509_V3_TYPE);
assertTrue(x509TokenValidator.canHandleToken(validateTarget));
// This will fail as the encoding type is not set
TokenValidatorResponse validatorResponse = null;
validatorResponse = x509TokenValidator.validateToken(validatorParameters);
assertTrue(validatorResponse != null);
assertTrue(validatorResponse.getToken() != null);
assertTrue(validatorResponse.getToken().getState() == STATE.INVALID);
binarySecurityToken.setEncodingType(WSS4JConstants.SOAPMESSAGE_NS + "#Base64Binary");
validatorResponse = x509TokenValidator.validateToken(validatorParameters);
assertTrue(validatorResponse.getToken() != null);
assertTrue(validatorResponse.getToken().getState() == STATE.VALID);
Principal principal = validatorResponse.getPrincipal();
assertTrue(principal != null && principal.getName() != null);
}
use of org.apache.cxf.sts.request.ReceivedToken in project cxf by apache.
the class UsernameTokenValidator method validateToken.
/**
* Validate a Token using the given TokenValidatorParameters.
*/
public TokenValidatorResponse validateToken(TokenValidatorParameters tokenParameters) {
TokenValidatorResponse response = new TokenValidatorResponse();
ReceivedToken validateTarget = tokenParameters.getToken();
validateTarget.setState(STATE.INVALID);
response.setToken(validateTarget);
UsernameTokenType usernameTokenType = (UsernameTokenType) validateTarget.getToken();
// Ignore the fact that no password is provided
// Some other requirements must be met to issue a token onbehalfof a subject
// whose authentication is not proved
validateTarget.setState(STATE.VALID);
response.setPrincipal(new CustomTokenPrincipal(usernameTokenType.getUsername().getValue()));
return response;
}
use of org.apache.cxf.sts.request.ReceivedToken in project cas by apereo.
the class X509TokenDelegationHandler method isDelegationAllowed.
@Override
public TokenDelegationResponse isDelegationAllowed(final TokenDelegationParameters tokenParameters) {
final TokenDelegationResponse response = new TokenDelegationResponse();
final ReceivedToken delegateTarget = tokenParameters.getToken();
response.setToken(delegateTarget);
if (!delegateTarget.isDOMElement()) {
return response;
}
if (delegateTarget.getState() == ReceivedToken.STATE.VALID && delegateTarget.getPrincipal() != null) {
response.setDelegationAllowed(true);
LOGGER.debug("Delegation is allowed for: [{}]", delegateTarget.getPrincipal());
} else {
LOGGER.debug("Delegation is not allowed, as the token is invalid or the principal is null");
}
return response;
}
use of org.apache.cxf.sts.request.ReceivedToken in project OpenAM by OpenRock.
the class TokenCancellerBase method cancelToken.
@Override
public TokenCancellerResponse cancelToken(TokenCancellerParameters tokenParameters) {
TokenCancellerResponse response = new TokenCancellerResponse();
ReceivedToken cancelTarget = tokenParameters.getToken();
cancelTarget.setState(ReceivedToken.STATE.VALID);
response.setToken(cancelTarget);
String tokenServiceConsumptionToken = null;
try {
final String tokenId = generateIdFromValidateTarget(cancelTarget);
tokenServiceConsumptionToken = getTokenServiceConsumptionToken();
tokenServiceConsumer.cancelToken(tokenId, tokenServiceConsumptionToken);
cancelTarget.setState(ReceivedToken.STATE.CANCELLED);
return response;
} catch (TokenCancellationException e) {
throw new STSException("Exception caught validating issued token: " + e.getMessage(), e);
} finally {
if (tokenServiceConsumptionToken != null) {
invalidateTokenGenerationServiceConsumptionToken(tokenServiceConsumptionToken);
}
}
}
Aggregations