use of org.apache.cxf.sts.request.ReceivedToken in project cxf by apache.
the class SCTValidator method validateToken.
/**
* Validate a Token using the given TokenValidatorParameters.
*/
public TokenValidatorResponse validateToken(TokenValidatorParameters tokenParameters) {
LOG.fine("Validating SecurityContextToken");
TokenValidatorResponse response = new TokenValidatorResponse();
ReceivedToken validateTarget = tokenParameters.getToken();
validateTarget.setState(STATE.INVALID);
response.setToken(validateTarget);
if (tokenParameters.getTokenStore() == null) {
LOG.log(Level.FINE, "A cache must be configured to use the SCTValidator");
return response;
}
if (validateTarget.isDOMElement()) {
try {
Element validateTargetElement = (Element) validateTarget.getToken();
SecurityContextToken sct = new SecurityContextToken(validateTargetElement);
String identifier = sct.getIdentifier();
SecurityToken token = tokenParameters.getTokenStore().getToken(identifier);
if (token == null) {
LOG.fine("Identifier: " + identifier + " is not found in the cache");
return response;
}
if (token.isExpired()) {
validateTarget.setState(STATE.EXPIRED);
LOG.fine("Token: " + identifier + " is in the cache but expired");
return response;
}
byte[] secret = token.getSecret();
Map<String, Object> properties = new HashMap<>(1);
properties.put(SCT_VALIDATOR_SECRET, secret);
response.setAdditionalProperties(properties);
response.setPrincipal(token.getPrincipal());
Map<String, Object> props = token.getProperties();
if (props != null) {
String realm = (String) props.get(STSConstants.TOKEN_REALM);
response.setTokenRealm(realm);
}
validateTarget.setState(STATE.VALID);
LOG.fine("SecurityContextToken successfully validated");
} catch (WSSecurityException ex) {
LOG.log(Level.WARNING, "", ex);
}
}
return response;
}
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) {
LOG.fine("Validating UsernameToken");
STSPropertiesMBean stsProperties = tokenParameters.getStsProperties();
Crypto sigCrypto = stsProperties.getSignatureCrypto();
CallbackHandler callbackHandler = stsProperties.getCallbackHandler();
RequestData requestData = new RequestData();
requestData.setSigVerCrypto(sigCrypto);
WSSConfig wssConfig = WSSConfig.getNewInstance();
requestData.setWssConfig(wssConfig);
requestData.setCallbackHandler(callbackHandler);
requestData.setMsgContext(tokenParameters.getMessageContext());
TokenValidatorResponse response = new TokenValidatorResponse();
ReceivedToken validateTarget = tokenParameters.getToken();
validateTarget.setState(STATE.INVALID);
response.setToken(validateTarget);
if (!validateTarget.isUsernameToken()) {
return response;
}
//
// Turn the JAXB UsernameTokenType into a DOM Element for validation
//
UsernameTokenType usernameTokenType = (UsernameTokenType) validateTarget.getToken();
// Marshall the received JAXB object into a DOM Element
Element usernameTokenElement = null;
try {
Set<Class<?>> classes = new HashSet<>();
classes.add(ObjectFactory.class);
classes.add(org.apache.cxf.ws.security.sts.provider.model.wstrust14.ObjectFactory.class);
CachedContextAndSchemas cache = JAXBContextCache.getCachedContextAndSchemas(classes, null, null, null, false);
JAXBContext jaxbContext = cache.getContext();
Marshaller marshaller = jaxbContext.createMarshaller();
Document doc = DOMUtils.getEmptyDocument();
Element rootElement = doc.createElement("root-element");
JAXBElement<UsernameTokenType> tokenType = new JAXBElement<UsernameTokenType>(QNameConstants.USERNAME_TOKEN, UsernameTokenType.class, usernameTokenType);
marshaller.marshal(tokenType, rootElement);
usernameTokenElement = (Element) rootElement.getFirstChild();
} catch (JAXBException ex) {
LOG.log(Level.WARNING, "", ex);
return response;
}
//
try {
boolean allowNamespaceQualifiedPasswordTypes = requestData.isAllowNamespaceQualifiedPasswordTypes();
UsernameToken ut = new UsernameToken(usernameTokenElement, allowNamespaceQualifiedPasswordTypes, new BSPEnforcer());
// The parsed principal is set independent whether validation is successful or not
response.setPrincipal(new CustomTokenPrincipal(ut.getName()));
if (ut.getPassword() == null) {
return response;
}
// See if the UsernameToken is stored in the cache
int hash = ut.hashCode();
SecurityToken secToken = null;
if (tokenParameters.getTokenStore() != null) {
secToken = tokenParameters.getTokenStore().getToken(Integer.toString(hash));
if (secToken != null && (secToken.getTokenHash() != hash || secToken.isExpired())) {
secToken = null;
}
}
Principal principal = null;
if (secToken == null) {
Credential credential = new Credential();
credential.setUsernametoken(ut);
credential = validator.validate(credential, requestData);
principal = credential.getPrincipal();
if (credential.getSubject() != null && roleParser != null) {
// Parse roles from the validated token
Set<Principal> roles = roleParser.parseRolesFromSubject(principal, credential.getSubject());
response.setRoles(roles);
}
}
if (principal == null) {
principal = createPrincipal(ut.getName(), ut.getPassword(), ut.getPasswordType(), ut.getNonce(), ut.getCreated());
}
// Get the realm of the UsernameToken
String tokenRealm = null;
if (usernameTokenRealmCodec != null) {
tokenRealm = usernameTokenRealmCodec.getRealmFromToken(ut);
// verify the realm against the cached token
if (secToken != null) {
Map<String, Object> props = secToken.getProperties();
if (props != null) {
String cachedRealm = (String) props.get(STSConstants.TOKEN_REALM);
if (!tokenRealm.equals(cachedRealm)) {
return response;
}
}
}
}
// Store the successfully validated token in the cache
if (tokenParameters.getTokenStore() != null && secToken == null) {
secToken = new SecurityToken(ut.getID());
secToken.setToken(ut.getElement());
int hashCode = ut.hashCode();
String identifier = Integer.toString(hashCode);
secToken.setTokenHash(hashCode);
tokenParameters.getTokenStore().add(identifier, secToken);
}
response.setPrincipal(principal);
response.setTokenRealm(tokenRealm);
validateTarget.setState(STATE.VALID);
LOG.fine("Username Token successfully validated");
} catch (WSSecurityException ex) {
LOG.log(Level.WARNING, "", ex);
}
return response;
}
use of org.apache.cxf.sts.request.ReceivedToken in project cxf by apache.
the class DummyTokenValidator method validateToken.
public TokenValidatorResponse validateToken(TokenValidatorParameters tokenParameters) {
TokenValidatorResponse response = new TokenValidatorResponse();
ReceivedToken validateTarget = tokenParameters.getToken();
validateTarget.setState(STATE.INVALID);
response.setToken(validateTarget);
if (validateTarget != null && validateTarget.isBinarySecurityToken()) {
BinarySecurityTokenType binarySecurity = (BinarySecurityTokenType) validateTarget.getToken();
if ("12345678".equals(binarySecurity.getValue())) {
validateTarget.setState(STATE.VALID);
}
}
return response;
}
use of org.apache.cxf.sts.request.ReceivedToken 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);
}
use of org.apache.cxf.sts.request.ReceivedToken in project cxf by apache.
the class SCTCancellerTest method testCancelInvalidToken.
/**
* Try to cancel an invalid SecurityContextToken
*/
@org.junit.Test
public void testCancelInvalidToken() throws Exception {
TokenCanceller sctCanceller = new SCTCanceller();
sctCanceller.setVerifyProofOfPossession(false);
TokenCancellerParameters cancellerParameters = createCancellerParameters();
TokenRequirements tokenRequirements = cancellerParameters.getTokenRequirements();
// Create a CancelTarget consisting of a SecurityContextToken
Document doc = DOMUtils.getEmptyDocument();
SecurityContextToken sct = new SecurityContextToken(doc);
ReceivedToken cancelTarget = new ReceivedToken(sct.getElement());
tokenRequirements.setCancelTarget(cancelTarget);
cancellerParameters.setToken(cancelTarget);
assertTrue(sctCanceller.canHandleToken(cancelTarget));
TokenCancellerResponse cancellerResponse = sctCanceller.cancelToken(cancellerParameters);
assertTrue(cancellerResponse != null);
assertFalse(cancellerResponse.getToken().getState() == STATE.CANCELLED);
}
Aggregations