// 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"
);
}
}
BinarySecurity token = createSecurityToken(elem, data.getWssConfig());
X509Certificate[] certs = null;
Validator validator = data.getValidator(new QName(elem.getNamespaceURI(),
elem.getLocalName()));
if (data.getSigCrypto() == null) {
certs = getCertificatesTokenReference(token, data.getDecCrypto());
} else {
certs = getCertificatesTokenReference(token, data.getSigCrypto());
}
WSSecurityEngineResult result =
new WSSecurityEngineResult(WSConstants.BST, token, certs);
wsDocInfo.addTokenElement(elem);
result.put(WSSecurityEngineResult.TAG_ID, id);
if (validator != null) {
// Hook to allow the user to validate the BinarySecurityToken
Credential credential = new Credential();
credential.setBinarySecurityToken(token);
credential.setCertificates(certs);
Credential returnedCredential = validator.validate(credential, data);
result.put(WSSecurityEngineResult.TAG_VALIDATED_TOKEN, Boolean.TRUE);
result.put(WSSecurityEngineResult.TAG_SECRET, returnedCredential.getSecretKey());
if (returnedCredential.getTransformedToken() != null) {
result.put(
WSSecurityEngineResult.TAG_TRANSFORMED_TOKEN,
returnedCredential.getTransformedToken()
);
SAMLTokenPrincipal samlPrincipal =
new SAMLTokenPrincipal(credential.getTransformedToken());
result.put(WSSecurityEngineResult.TAG_PRINCIPAL, samlPrincipal);
} else if (credential.getPrincipal() != null) {
result.put(WSSecurityEngineResult.TAG_PRINCIPAL, credential.getPrincipal());
} else if (certs != null && certs[0] != null) {
result.put(WSSecurityEngineResult.TAG_PRINCIPAL, certs[0].getSubjectX500Principal());
}
result.put(WSSecurityEngineResult.TAG_SUBJECT, credential.getSubject());
}
wsDocInfo.addResult(result);
return java.util.Collections.singletonList(result);
}