public class BinarySecurityTokenInputHandler extends AbstractInputSecurityHeaderHandler {
@Override
public void handle(final InputProcessorChain inputProcessorChain, final XMLSecurityProperties securityProperties,
final Deque<XMLSecEvent> eventQueue, final Integer index) throws XMLSecurityException {
@SuppressWarnings("unchecked")
final BinarySecurityTokenType binarySecurityTokenType =
((JAXBElement<BinarySecurityTokenType>) parseStructure(eventQueue, index, securityProperties)).getValue();
checkBSPCompliance(inputProcessorChain, binarySecurityTokenType);
if (binarySecurityTokenType.getId() == null) {
binarySecurityTokenType.setId(IDGenerator.generateID(null));
}
final WSInboundSecurityContext wsInboundSecurityContext = (WSInboundSecurityContext) inputProcessorChain.getSecurityContext();
final WSSSecurityProperties wssSecurityProperties = (WSSSecurityProperties) securityProperties;
final List<QName> elementPath = getElementPath(eventQueue);
final List<XMLSecEvent> xmlSecEvents = getResponsibleXMLSecEvents(eventQueue, index);
final TokenContext tokenContext = new TokenContext(wssSecurityProperties, wsInboundSecurityContext, xmlSecEvents, elementPath);
BinarySecurityTokenValidator binarySecurityTokenValidator =
wssSecurityProperties.getValidator(WSSConstants.TAG_wsse_BinarySecurityToken);
if (binarySecurityTokenValidator == null) {
binarySecurityTokenValidator = new BinarySecurityTokenValidatorImpl();
}
final InboundSecurityToken binarySecurityToken =
binarySecurityTokenValidator.validate(binarySecurityTokenType, tokenContext);
SecurityTokenProvider<InboundSecurityToken> securityTokenProvider = new SecurityTokenProvider<InboundSecurityToken>() {
@Override
public InboundSecurityToken getSecurityToken() throws XMLSecurityException {
return binarySecurityToken;
}
@Override
public String getId() {
return binarySecurityToken.getId();
}
};
wsInboundSecurityContext.registerSecurityTokenProvider(binarySecurityTokenType.getId(), securityTokenProvider);
TokenSecurityEvent<? extends SecurityToken> tokenSecurityEvent;
//fire a tokenSecurityEvent
if (binarySecurityTokenType.getValueType().startsWith(WSSConstants.NS_X509TOKEN_PROFILE)) {
X509TokenSecurityEvent x509TokenSecurityEvent = new X509TokenSecurityEvent();
x509TokenSecurityEvent.setSecurityToken((X509SecurityToken) binarySecurityToken);
tokenSecurityEvent = x509TokenSecurityEvent;
} else if (binarySecurityTokenType.getValueType().startsWith(WSSConstants.NS_KERBEROS11_TOKEN_PROFILE)) {
KerberosTokenSecurityEvent kerberosTokenSecurityEvent = new KerberosTokenSecurityEvent();
kerberosTokenSecurityEvent.setSecurityToken((KerberosServiceSecurityToken)binarySecurityToken);
tokenSecurityEvent = kerberosTokenSecurityEvent;
} else {
throw new WSSecurityException(
WSSecurityException.ErrorCode.INVALID_SECURITY_TOKEN, "invalidValueType",
binarySecurityTokenType.getValueType());
}
tokenSecurityEvent.setCorrelationID(binarySecurityTokenType.getId());
wsInboundSecurityContext.registerSecurityEvent(tokenSecurityEvent);
}