samlRequestOrResponse = SamlRequestOrResponse.REQUEST;
} else if (samlRequestParam == null && samlResponseParam != null) {
samlMessage = samlResponseParam;
samlRequestOrResponse = SamlRequestOrResponse.RESPONSE;
} else {
throw new InvalidRequestException("SAML message should either have a SAMLRequest parameter or a SAMLResponse parameter");
}
InputStream is;
if (httpRequest.getMethod().equals("POST")) {
byte[] decodedMessage = Base64.decode(samlMessage);
is = new ByteArrayInputStream(decodedMessage);
} else {
byte[] base64Decoded = Base64.decode(samlMessage);
ByteArrayInputStream bais = new ByteArrayInputStream(base64Decoded);
is = new InflaterInputStream(bais, new Inflater(true));
}
Document document = getDocument(is);
String issuerEntityId;
RequestAbstractType samlRequestMessage = null;
StatusResponseType samlResponseMessage = null;
if (samlRequestOrResponse.isRequest()) {
samlRequestMessage = getSamlRequest(document);
issuerEntityId = samlRequestMessage.getIssuer().getValue();
} else {
samlResponseMessage = getSamlResponse(document);
issuerEntityId = samlResponseMessage.getIssuer().getValue();
}
log.debug("Received: " + SamlUtils.getDocumentAsString(document));
try {
if (samlRequestOrResponse.isRequest() || samlResponseMessage.getInResponseTo() == null) {
// Request or unsolicited response
String destination = samlRequestOrResponse.isRequest() ? samlRequestMessage.getDestination() : samlResponseMessage.getDestination();
if (!samlEntityBean.get().getServiceURL(service).equals(destination)) {
throw new InvalidRequestException("Destination (" + destination + ") is not valid.");
}
dialogueManager.beginDialogue();
samlDialogue.get().setExternalProviderMessageId(samlRequestOrResponse.isRequest() ? samlRequestMessage.getID() : samlResponseMessage.getID());
SamlExternalEntity externalProvider = samlEntityBean.get().getExternalSamlEntityByEntityId(issuerEntityId);
if (externalProvider == null) {
throw new InvalidRequestException("Received message from unknown entity id " + issuerEntityId);
}
samlDialogue.get().setExternalProvider(externalProvider);
} else {
String dialogueId = samlResponseMessage.getInResponseTo();
if (!dialogueManager.isExistingDialogue(dialogueId)) {
throw new InvalidRequestException("No request that corresponds with the received response");
}
dialogueManager.attachDialogue(dialogueId);
if (!(samlDialogue.get().getExternalProvider().getEntityId().equals(issuerEntityId))) {
throw new InvalidRequestException("Identity samlEntityBean of request and response do not match");
}
}
SamlExternalEntity externalProvider = samlEntityBean.get().getExternalSamlEntityByEntityId(issuerEntityId);