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.util.Arrays;
20  
21  /**
22   * SAML 1.X Type 0x0001 Artifact. SAML 1, type 1, artifacts contains a 2 byte type code with a value of 1 followed by a
23   * 20 byte source ID followed by a 20 byte assertion handle.
24   */
25  public class SAML1ArtifactType0001 extends AbstractSAML1Artifact {
26  
27      /** Artifact type code (0x0001). */
28      public static final byte[] TYPE_CODE = { 0, 1 };
29  
30      /** 20 byte artifact source ID. */
31      private byte[] sourceID;
32  
33      /** 20 byte assertion handle. */
34      private byte[] assertionHandle;
35  
36      /** Constructor. */
37      public SAML1ArtifactType0001() {
38          super(TYPE_CODE);
39      }
40  
41      /**
42       * Constructor.
43       * 
44       * @param source 20 byte source ID of the artifact
45       * @param handle 20 byte assertion handle of the artifact
46       * 
47       * @throws IllegalArgumentException thrown if the given source ID or message handle are not of the current length
48       *             (20 bytes)
49       */
50      public SAML1ArtifactType0001(byte[] source, byte[] handle) {
51          super(TYPE_CODE);
52  
53          setSourceID(source);
54          setAssertionHandle(handle);
55      }
56  
57      /**
58       * Constructs a SAML 1 artifact from its byte array representation.
59       * 
60       * @param artifact the byte array representing the artifact
61       * 
62       * @return the artifact created from the byte array
63       * 
64       * @throws IllegalArgumentException thrown if the artifact is not the right type or lenght (42 bytes) or is not of
65       *             the correct type (0x0001)
66       */
67      public static SAML1ArtifactType0001 parseArtifact(byte[] artifact) {
68          if (artifact.length != 42) {
69              throw new IllegalArgumentException("Artifact length must be 42 bytes it was " + artifact.length + "bytes");
70          }
71  
72          byte[] typeCode = { artifact[0], artifact[1] };
73          if (!Arrays.equals(typeCode, TYPE_CODE)) {
74              throw new IllegalArgumentException("Artifact is not of appropriate type.");
75          }
76  
77          byte[] sourceID = new byte[20];
78          System.arraycopy(artifact, 2, sourceID, 0, 20);
79  
80          byte[] assertionHandle = new byte[20];
81          System.arraycopy(artifact, 22, assertionHandle, 0, 20);
82  
83          return new SAML1ArtifactType0001(sourceID, assertionHandle);
84      }
85  
86      /**
87       * Gets the 20 byte source ID of the artifact.
88       * 
89       * @return the source ID of the artifact
90       */
91      public byte[] getSourceID() {
92          return sourceID;
93      }
94  
95      /**
96       * Sets the 20 byte source ID of the artifact.
97       * 
98       * @param newSourceID 20 byte source ID of the artifact
99       * 
100      * @throws IllegalArgumentException thrown if the given source ID is not 20 bytes
101      */
102     protected void setSourceID(byte[] newSourceID) {
103         if (newSourceID.length != 20) {
104             throw new IllegalArgumentException("Artifact source ID must be 20 bytes long");
105         }
106         sourceID = newSourceID;
107     }
108 
109     /**
110      * Gets the artifiact's 20 byte assertion handle.
111      * 
112      * @return artifiact's 20 byte assertion handle
113      */
114     public byte[] getAssertionHandle() {
115         return assertionHandle;
116     }
117 
118     /**
119      * Sets the artifiact's 20 byte assertion handle.
120      * 
121      * @param handle artifiact's 20 byte assertion handle
122      */
123     public void setAssertionHandle(byte[] handle) {
124         if (handle.length != 20) {
125             throw new IllegalArgumentException("Artifact assertion handle must be 20 bytes long");
126         }
127         assertionHandle = handle;
128     }
129 
130     /** {@inheritDoc} */
131     public byte[] getRemainingArtifact() {
132         byte[] remainingArtifact = new byte[40];
133 
134         System.arraycopy(getSourceID(), 0, remainingArtifact, 0, 20);
135         System.arraycopy(getAssertionHandle(), 0, remainingArtifact, 20, 20);
136 
137         return remainingArtifact;
138     }
139 }