if (targetToken instanceof Element) {
Element tokenElement = (Element) targetToken;
NodeList refList =
tokenElement.getElementsByTagNameNS(STSConstants.WSSE_EXT_04_01, "Reference");
if (refList.getLength() == 0) {
throw new STSException(
"Cannot find Reference element in the SecurityTokenReference.",
STSException.REQUEST_FAILED
);
}
referenceURI = refList.item(0).getNodeValue();
} else if (targetToken instanceof SecurityTokenReferenceType) {
Iterator<?> iterator = ((SecurityTokenReferenceType) targetToken).getAny().iterator();
while (iterator.hasNext()) {
JAXBElement<?> jaxbElement = (JAXBElement<?>) iterator.next();
if (jaxbElement.getValue() instanceof ReferenceType) {
referenceURI = ((ReferenceType) jaxbElement.getValue()).getURI();
}
}
}
LOG.fine("Reference URI found " + referenceURI);
if (referenceURI == null) {
LOG.log(Level.WARNING, "No Reference URI was received");
throw new STSException(
"An unknown element was received", STSException.BAD_REQUEST
);
}
// Find processed token corresponding to the URI
if (referenceURI.charAt(0) == '#') {
referenceURI = referenceURI.substring(1);
}
MessageContext messageContext = wsContext.getMessageContext();
final List<WSHandlerResult> handlerResults =
CastUtils.cast((List<?>) messageContext.get(WSHandlerConstants.RECV_RESULTS));
if (handlerResults != null && handlerResults.size() > 0) {
WSHandlerResult handlerResult = handlerResults.get(0);
List<WSSecurityEngineResult> engineResults = handlerResult.getResults();
for (WSSecurityEngineResult engineResult : engineResults) {
Integer actInt = (Integer)engineResult.get(WSSecurityEngineResult.TAG_ACTION);
String id = (String)engineResult.get(WSSecurityEngineResult.TAG_ID);
if (referenceURI.equals(id)) {
Element tokenElement =
(Element)engineResult.get(WSSecurityEngineResult.TAG_TOKEN_ELEMENT);
if (tokenElement == null) {
throw new STSException(
"Cannot retrieve token from reference", STSException.INVALID_REQUEST
);
}
return tokenElement;
} else if (actInt == WSConstants.SCT) {
// Need to check special case of SecurityContextToken Identifier separately
SecurityContextToken sct =
(SecurityContextToken)
engineResult.get(WSSecurityEngineResult.TAG_SECURITY_CONTEXT_TOKEN);
if (referenceURI.equals(sct.getIdentifier())) {
return sct.getElement();
}
}
}
}
throw new STSException("Cannot retreive token from reference", STSException.REQUEST_FAILED);
}