View Javadoc

1   /*
2    * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * SAML 1.X HTTP POST message decoder.
36   */
37  public class HTTPPostDecoder extends BaseSAML1MessageDecoder {
38  
39      /** Class logger. */
40      private final Logger log = LoggerFactory.getLogger(HTTPPostDecoder.class);
41  
42      /** Constructor. */
43      public HTTPPostDecoder() {
44          super();
45      }
46  
47      /**
48       * Constructor.
49       * 
50       * @param pool parser pool used to deserialize messages
51       */
52      public HTTPPostDecoder(ParserPool pool) {
53          super(pool);
54      }
55  
56      /**
57       * Constructor.
58       * 
59       * @param map Artifact to SAML map
60       * 
61       * @deprecated
62       */
63      public HTTPPostDecoder(SAMLArtifactMap map) {
64          super(map);
65      }
66  
67      /**
68       * Constructor.
69       * 
70       * @param map used to map artifacts to SAML
71       * @param pool parser pool used to deserialize messages
72       * 
73       * @deprecated
74       */
75      public HTTPPostDecoder(SAMLArtifactMap map, ParserPool pool) {
76          super(map, pool);
77      }
78  
79      /** {@inheritDoc} */
80      public String getBindingURI() {
81          return SAMLConstants.SAML1_POST_BINDING_URI;
82      }
83  
84      /** {@inheritDoc} */
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     /** {@inheritDoc} */
125     protected boolean isIntendedDestinationEndpointURIRequired(SAMLMessageContext samlMsgCtx) {
126         return samlMsgCtx.getInboundSAMLMessage() instanceof ResponseAbstractType;
127     }
128 }