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.encoding;
18  
19  import java.util.List;
20  
21  import org.opensaml.Configuration;
22  import org.opensaml.common.SAMLObject;
23  import org.opensaml.common.binding.SAMLMessageContext;
24  import org.opensaml.common.binding.artifact.SAMLArtifactMap;
25  import org.opensaml.common.xml.SAMLConstants;
26  import org.opensaml.saml1.binding.artifact.AbstractSAML1Artifact;
27  import org.opensaml.saml1.binding.artifact.SAML1ArtifactBuilder;
28  import org.opensaml.saml1.binding.artifact.SAML1ArtifactType0001;
29  import org.opensaml.saml1.core.Assertion;
30  import org.opensaml.saml1.core.NameIdentifier;
31  import org.opensaml.saml1.core.Response;
32  import org.opensaml.util.URLBuilder;
33  import org.opensaml.ws.message.MessageContext;
34  import org.opensaml.ws.message.encoder.MessageEncodingException;
35  import org.opensaml.ws.transport.http.HTTPOutTransport;
36  import org.opensaml.xml.io.MarshallingException;
37  import org.opensaml.xml.util.Pair;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  /**
42   * SAML 1.X HTTP Artifact message encoder.
43   */
44  public class HTTPArtifactEncoder extends BaseSAML1MessageEncoder {
45  
46      /** Class logger. */
47      private final Logger log = LoggerFactory.getLogger(HTTPArtifactEncoder.class);
48  
49      /** SAML artifact map used to store created artifacts for later retrival. */
50      private SAMLArtifactMap artifactMap;
51  
52      /** Default artifact type to use when encoding messages. */
53      private byte[] defaultArtifactType;
54  
55      /**
56       * Constructor.
57       * 
58       * @param map SAML artifact map used to store created artifacts for later retrival
59       */
60      public HTTPArtifactEncoder(SAMLArtifactMap map) {
61          artifactMap = map;
62          defaultArtifactType = SAML1ArtifactType0001.TYPE_CODE;
63      }
64  
65      /** {@inheritDoc} */
66      public String getBindingURI() {
67          return SAMLConstants.SAML1_ARTIFACT_BINDING_URI;
68      }
69  
70      /** {@inheritDoc} */
71      public boolean providesMessageConfidentiality(MessageContext messageContext) throws MessageEncodingException {
72          return false;
73      }
74  
75      /** {@inheritDoc} */
76      public boolean providesMessageIntegrity(MessageContext messageContext) throws MessageEncodingException {
77          return false;
78      }
79  
80      /** {@inheritDoc} */
81      protected void doEncode(MessageContext messageContext) throws MessageEncodingException {
82          if (!(messageContext instanceof SAMLMessageContext)) {
83              log.error("Invalid message context type, this encoder only support SAMLMessageContext");
84              throw new MessageEncodingException(
85                      "Invalid message context type, this encoder only support SAMLMessageContext");
86          }
87  
88          if (!(messageContext.getOutboundMessageTransport() instanceof HTTPOutTransport)) {
89              log.error("Invalid outbound message transport type, this encoder only support HTTPOutTransport");
90              throw new MessageEncodingException(
91                      "Invalid outbound message transport type, this encoder only support HTTPOutTransport");
92          }
93  
94          SAMLMessageContext<SAMLObject, Response, NameIdentifier> artifactContext = (SAMLMessageContext) messageContext;
95          HTTPOutTransport outTransport = (HTTPOutTransport) artifactContext.getOutboundMessageTransport();
96  
97          URLBuilder urlBuilder = getEndpointURL(artifactContext);
98  
99          List<Pair<String, String>> params = urlBuilder.getQueryParams();
100 
101         params.add(new Pair<String, String>("TARGET", artifactContext.getRelayState()));
102 
103         SAML1ArtifactBuilder artifactBuilder;
104         if (artifactContext.getOutboundMessageArtifactType() != null) {
105             artifactBuilder = Configuration.getSAML1ArtifactBuilderFactory().getArtifactBuilder(
106                     artifactContext.getOutboundMessageArtifactType());
107         } else {
108             artifactBuilder = Configuration.getSAML1ArtifactBuilderFactory().getArtifactBuilder(defaultArtifactType);
109             artifactContext.setOutboundMessageArtifactType(defaultArtifactType);
110         }
111 
112         AbstractSAML1Artifact artifact;
113         String artifactString;
114         for (Assertion assertion : artifactContext.getOutboundSAMLMessage().getAssertions()) {
115             artifact = artifactBuilder.buildArtifact(artifactContext, assertion);
116             if(artifact == null){
117                 log.error("Unable to build artifact for message to relying party");
118                 throw new MessageEncodingException("Unable to builder artifact for message to relying party");
119             }
120 
121             try {
122                 artifactMap.put(artifact.base64Encode(), messageContext.getInboundMessageIssuer(), messageContext
123                         .getOutboundMessageIssuer(), assertion);
124             } catch (MarshallingException e) {
125                 log.error("Unable to marshall assertion to be represented as an artifact", e);
126                 throw new MessageEncodingException("Unable to marshall assertion to be represented as an artifact", e);
127             }
128             artifactString = artifact.base64Encode();
129             params.add(new Pair<String, String>("SAMLart", artifactString));
130         }
131 
132         String redirectUrl = urlBuilder.buildURL();
133 
134         log.debug("Sending redirect to URL {} to relying party {}", redirectUrl, artifactContext
135                 .getInboundMessageIssuer());
136         outTransport.sendRedirect(urlBuilder.buildURL());
137     }
138 }