View Javadoc

1   /*
2    * Copyright [2006] [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.artifact;
18  
19  import java.security.NoSuchAlgorithmException;
20  import java.security.SecureRandom;
21  
22  import org.opensaml.common.binding.BasicEndpointSelector;
23  import org.opensaml.common.binding.SAMLMessageContext;
24  import org.opensaml.common.xml.SAMLConstants;
25  import org.opensaml.saml1.core.Assertion;
26  import org.opensaml.saml1.core.NameIdentifier;
27  import org.opensaml.saml1.core.RequestAbstractType;
28  import org.opensaml.saml1.core.Response;
29  import org.opensaml.saml2.metadata.ArtifactResolutionService;
30  import org.opensaml.saml2.metadata.Endpoint;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   * SAML 1, type 0x0002, artifact builder.
36   */
37  public class SAML1ArtifactType0002Builder implements SAML1ArtifactBuilder<SAML1ArtifactType0002> {
38  
39      /** Class logger. */
40      private final Logger log = LoggerFactory.getLogger(SAML1ArtifactType0002Builder.class);
41  
42      /** {@inheritDoc} */
43      public SAML1ArtifactType0002 buildArtifact(byte[] artifact) {
44          return SAML1ArtifactType0002.parseArtifact(artifact);
45      }
46  
47      /** {@inheritDoc} */
48      public SAML1ArtifactType0002 buildArtifact(
49              SAMLMessageContext<RequestAbstractType, Response, NameIdentifier> requestContext, Assertion assertion) {
50          try {
51              String sourceLocation = getSourceLocation(requestContext);
52              if (sourceLocation == null) {
53                  return null;
54              }
55  
56              SecureRandom handleGenerator = SecureRandom.getInstance("SHA1PRNG");
57              byte[] assertionHandle = new byte[20];
58              handleGenerator.nextBytes(assertionHandle);
59              return new SAML1ArtifactType0002(assertionHandle, sourceLocation);
60          } catch (NoSuchAlgorithmException e) {
61              log.error("JVM does not support required cryptography algorithms: SHA1PRNG.", e);
62              throw new InternalError("JVM does not support required cryptography algorithms: SHA1PRNG.");
63          }
64      }
65  
66      /**
67       * Gets the source location used to for the artifacts created by this encoder.
68       * 
69       * @param requestContext current request context
70       * 
71       * @return source location used to for the artifacts created by this encoder
72       */
73      protected String getSourceLocation(SAMLMessageContext<RequestAbstractType, Response, NameIdentifier> requestContext) {
74          BasicEndpointSelector selector = new BasicEndpointSelector();
75          selector.setEndpointType(ArtifactResolutionService.DEFAULT_ELEMENT_NAME);
76          selector.getSupportedIssuerBindings().add(SAMLConstants.SAML1_SOAP11_BINDING_URI);
77          selector.setMetadataProvider(requestContext.getMetadataProvider());
78          selector.setEntityMetadata(requestContext.getLocalEntityMetadata());
79          selector.setEntityRoleMetadata(requestContext.getLocalEntityRoleMetadata());
80  
81          Endpoint acsEndpoint = selector.selectEndpoint();
82  
83          if (acsEndpoint == null) {
84              log.error("Unable to select source location for artifact.  No artifact resolution service defined for issuer.");
85              return null;
86          }
87  
88          return acsEndpoint.getLocation();
89      }
90  }