// See if the token has been previously processed
String id = elem.getAttributeNS(WSConstants.WSU_NS, "Id");
if (!"".equals(id)) {
Element foundElement = wsDocInfo.getTokenElement(id);
if (elem.equals(foundElement)) {
WSSecurityEngineResult result = wsDocInfo.getResult(id);
return java.util.Collections.singletonList(result);
} else if (foundElement != null) {
throw new WSSecurityException(
WSSecurityException.INVALID_SECURITY_TOKEN, "duplicateError"
);
}
}
Validator validator = data.getValidator(WSSecurityEngine.USERNAME_TOKEN);
Credential credential = handleUsernameToken(elem, validator, data);
UsernameToken token = credential.getUsernametoken();
int action = WSConstants.UT;
byte[] secretKey = null;
if (token.getPassword() == null) {
action = WSConstants.UT_NOPASSWORD;
if (token.isDerivedKey()) {
token.setRawPassword(data);
secretKey = token.getDerivedKey();
}
}
WSSecurityEngineResult result = new WSSecurityEngineResult(action, token);
result.put(WSSecurityEngineResult.TAG_ID, token.getID());
result.put(WSSecurityEngineResult.TAG_SECRET, secretKey);
if (validator != null) {
result.put(WSSecurityEngineResult.TAG_VALIDATED_TOKEN, Boolean.TRUE);
if (credential.getTransformedToken() != null) {
result.put(
WSSecurityEngineResult.TAG_TRANSFORMED_TOKEN, credential.getTransformedToken()
);
SAMLTokenPrincipal samlPrincipal =
new SAMLTokenPrincipal(credential.getTransformedToken());
result.put(WSSecurityEngineResult.TAG_PRINCIPAL, samlPrincipal);
} else {
WSUsernameTokenPrincipal principal =
new WSUsernameTokenPrincipal(token.getName(), token.isHashed());
principal.setNonce(token.getNonce());
principal.setPassword(token.getPassword());
principal.setCreatedTime(token.getCreated());
principal.setPasswordType(token.getPasswordType());
result.put(WSSecurityEngineResult.TAG_PRINCIPAL, principal);
}
result.put(WSSecurityEngineResult.TAG_SUBJECT, credential.getSubject());
}
wsDocInfo.addTokenElement(elem);
wsDocInfo.addResult(result);
return java.util.Collections.singletonList(result);