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.MessageDigest;
20  import java.security.NoSuchAlgorithmException;
21  import java.security.SecureRandom;
22  
23  import org.opensaml.common.binding.SAMLMessageContext;
24  import org.opensaml.saml1.core.Assertion;
25  import org.opensaml.saml1.core.NameIdentifier;
26  import org.opensaml.saml1.core.RequestAbstractType;
27  import org.opensaml.saml1.core.Response;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * Builder of SAML 1, type 0x001, artifacts.
33   */
34  public class SAML1ArtifactType0001Builder implements SAML1ArtifactBuilder<SAML1ArtifactType0001> {
35  
36      /** Class logger. */
37      private final Logger log = LoggerFactory.getLogger(SAML1ArtifactType0001Builder.class);
38  
39      /** {@inheritDoc} */
40      public SAML1ArtifactType0001 buildArtifact(byte[] artifact) {
41          return SAML1ArtifactType0001.parseArtifact(artifact);
42      }
43  
44      /** {@inheritDoc} */
45      public SAML1ArtifactType0001 buildArtifact(
46              SAMLMessageContext<RequestAbstractType, Response, NameIdentifier> requestContext, Assertion assertion) {
47          try {
48              MessageDigest sha1Digester = MessageDigest.getInstance("SHA-1");
49              byte[] source = sha1Digester.digest(requestContext.getLocalEntityId().getBytes());
50  
51              SecureRandom handleGenerator = SecureRandom.getInstance("SHA1PRNG");
52              byte[] assertionHandle = new byte[20];
53              handleGenerator.nextBytes(assertionHandle);
54  
55              return new SAML1ArtifactType0001(source, assertionHandle);
56          } catch (NoSuchAlgorithmException e) {
57              log.error("JVM does not support required cryptography algorithms.", e);
58              throw new InternalError("JVM does not support required cryptography algorithms: SHA-1 and/or SHA1PRNG.");
59          }
60      }
61  }