1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.opensaml.saml1.binding.decoding;
18
19 import java.io.ByteArrayInputStream;
20
21 import org.opensaml.common.SAMLObject;
22 import org.opensaml.common.binding.SAMLMessageContext;
23 import org.opensaml.common.binding.artifact.SAMLArtifactMap;
24 import org.opensaml.common.xml.SAMLConstants;
25 import org.opensaml.saml1.core.ResponseAbstractType;
26 import org.opensaml.ws.message.MessageContext;
27 import org.opensaml.ws.message.decoder.MessageDecodingException;
28 import org.opensaml.ws.transport.http.HTTPInTransport;
29 import org.opensaml.xml.parse.ParserPool;
30 import org.opensaml.xml.util.Base64;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35
36
37 public class HTTPPostDecoder extends BaseSAML1MessageDecoder {
38
39
40 private final Logger log = LoggerFactory.getLogger(HTTPPostDecoder.class);
41
42
43 public HTTPPostDecoder() {
44 super();
45 }
46
47
48
49
50
51
52 public HTTPPostDecoder(ParserPool pool) {
53 super(pool);
54 }
55
56
57
58
59
60
61
62
63 public HTTPPostDecoder(SAMLArtifactMap map) {
64 super(map);
65 }
66
67
68
69
70
71
72
73
74
75 public HTTPPostDecoder(SAMLArtifactMap map, ParserPool pool) {
76 super(map, pool);
77 }
78
79
80 public String getBindingURI() {
81 return SAMLConstants.SAML1_POST_BINDING_URI;
82 }
83
84
85 protected void doDecode(MessageContext messageContext) throws MessageDecodingException {
86 if (!(messageContext instanceof SAMLMessageContext)) {
87 log.error("Invalid message context type, this decoder only support SAMLMessageContext");
88 throw new MessageDecodingException(
89 "Invalid message context type, this decoder only support SAMLMessageContext");
90 }
91
92 if (!(messageContext.getInboundMessageTransport() instanceof HTTPInTransport)) {
93 log.error("Invalid inbound message transport type, this decoder only support HTTPInTransport");
94 throw new MessageDecodingException(
95 "Invalid inbound message transport type, this decoder only support HTTPInTransport");
96 }
97
98 SAMLMessageContext samlMsgCtx = (SAMLMessageContext) messageContext;
99
100 HTTPInTransport inTransport = (HTTPInTransport) samlMsgCtx.getInboundMessageTransport();
101 if (!inTransport.getHTTPMethod().equalsIgnoreCase("POST")) {
102 throw new MessageDecodingException("This message deocoder only supports the HTTP POST method");
103 }
104
105 String relayState = inTransport.getParameterValue("TARGET");
106 samlMsgCtx.setRelayState(relayState);
107 log.debug("Decoded SAML relay state (TARGET parameter) of: {}", relayState);
108
109 String base64Message = inTransport.getParameterValue("SAMLResponse");
110 byte[] decodedBytes = Base64.decode(base64Message);
111 if (decodedBytes == null) {
112 log.error("Unable to Base64 decode SAML message");
113 throw new MessageDecodingException("Unable to Base64 decode SAML message");
114 }
115
116 SAMLObject inboundMessage = (SAMLObject) unmarshallMessage(new ByteArrayInputStream(decodedBytes));
117 samlMsgCtx.setInboundMessage(inboundMessage);
118 samlMsgCtx.setInboundSAMLMessage(inboundMessage);
119 log.debug("Decoded SAML message");
120
121 populateMessageContext(samlMsgCtx);
122 }
123
124
125 protected boolean isIntendedDestinationEndpointURIRequired(SAMLMessageContext samlMsgCtx) {
126 return samlMsgCtx.getInboundSAMLMessage() instanceof ResponseAbstractType;
127 }
128 }