KeyRequirements keyRequirements = requestParser.getKeyRequirements();
TokenRequirements tokenRequirements = requestParser.getTokenRequirements();
ReceivedToken validateTarget = tokenRequirements.getValidateTarget();
if (validateTarget == null || validateTarget.getToken() == null) {
throw new STSException("No element presented for validation", STSException.INVALID_REQUEST);
}
if (tokenRequirements.getTokenType() == null) {
tokenRequirements.setTokenType(STSConstants.STATUS);
LOG.fine(
"Received TokenType is null, falling back to default token type: "
+ STSConstants.STATUS
);
}
TokenValidatorParameters validatorParameters = new TokenValidatorParameters();
validatorParameters.setStsProperties(stsProperties);
validatorParameters.setPrincipal(context.getUserPrincipal());
validatorParameters.setWebServiceContext(context);
validatorParameters.setTokenStore(getTokenStore());
validatorParameters.setKeyRequirements(keyRequirements);
validatorParameters.setTokenRequirements(tokenRequirements);
//
// Validate token
//
TokenValidatorResponse tokenResponse = null;
for (TokenValidator tokenValidator : tokenValidators) {
if (tokenValidator.canHandleToken(validateTarget)) {
try {
tokenResponse = tokenValidator.validateToken(validatorParameters);
} catch (RuntimeException ex) {
LOG.log(Level.WARNING, "", ex);
tokenResponse = new TokenValidatorResponse();
tokenResponse.setValid(false);
}
break;
}
}
if (tokenResponse == null) {
LOG.fine("No Token Validator has been found that can handle this token");
tokenResponse = new TokenValidatorResponse();
tokenResponse.setValid(false);
}
//
// Create a new token (if requested)
//
TokenProviderResponse tokenProviderResponse = null;
String tokenType = tokenRequirements.getTokenType();
if (tokenResponse.isValid() && !STSConstants.STATUS.equals(tokenType)) {
TokenProviderParameters providerParameters =
createTokenProviderParameters(requestParser, context);
// Map the principal (if it exists)
Principal responsePrincipal = tokenResponse.getPrincipal();
if (responsePrincipal != null) {
String targetRealm = providerParameters.getRealm();
String sourceRealm = tokenResponse.getTokenRealm();
IdentityMapper identityMapper = stsProperties.getIdentityMapper();
if (sourceRealm != null && !sourceRealm.equals(targetRealm) && identityMapper != null) {
Principal targetPrincipal =
identityMapper.mapPrincipal(sourceRealm, responsePrincipal, targetRealm);
providerParameters.setPrincipal(targetPrincipal);
} else {
providerParameters.setPrincipal(responsePrincipal);
}
}
Map<String, Object> additionalProperties = tokenResponse.getAdditionalProperties();
if (additionalProperties != null) {
providerParameters.setAdditionalProperties(additionalProperties);
}
String realm = providerParameters.getRealm();
for (TokenProvider tokenProvider : tokenProviders) {
if (tokenProvider.canHandleToken(tokenType, realm)) {
try {
tokenProviderResponse = tokenProvider.createToken(providerParameters);
} 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 (tokenProviderResponse == null || tokenProviderResponse.getToken() == null) {
LOG.fine("No Token Provider has been found that can handle this token");
throw new STSException(
"No token provider found for requested token type: " + tokenType,
STSException.REQUEST_FAILED
);
}
}
// prepare response
try {
return createResponse(tokenResponse, tokenProviderResponse, tokenRequirements);
} catch (Throwable ex) {
LOG.log(Level.WARNING, "", ex);
throw new STSException("Error in creating the response", ex, STSException.REQUEST_FAILED);
}
}