001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: AssertionArtifact.java,v 1.2 2008/06/25 05:47:36 qcheng Exp $
026 *
027 */
028
029
030package com.sun.identity.saml.protocol;
031
032import com.sun.identity.saml.common.SAMLConstants;
033import com.sun.identity.saml.common.SAMLException;
034import com.sun.identity.saml.common.SAMLRequesterException;
035import com.sun.identity.saml.common.SAMLUtils;
036import com.sun.identity.shared.encode.Base64;
037
038/**
039 * This class represents the <code>AssertionArtifact</code> element in
040 * SAML protocol schema. Current implementation supports TYPE 1 artifact only.
041 * Other type of artifact can be supported by extending this class.
042 *
043 * @supported.all.api
044 */
045public class AssertionArtifact {
046
047    /**
048     * This value specifies the assertion artifact as a string. 
049     */ 
050    protected String artifact;
051
052    protected String assertionHandle = null;
053    protected String sourceID = null;
054    protected byte[] typeCode = null;
055
056    final static int ARTIFACT_1_LENGTH = 42;
057    final static byte ARTIFACT_1_TYPE_CODE_0 = 0;
058    final static byte ARTIFACT_1_TYPE_CODE_1 = 1;
059    final static byte[] ARTIFACT_1_TYPE_CODE = {0, 1};
060            
061    /**
062     * This is the default constructor of assertion artifact.
063     */
064    protected AssertionArtifact() {
065    }
066  
067    /**
068     * This constructor is used to construct an assertion artifact.      
069     * @param theArtifact is the string that is generated by a provider.
070     * @exception SAMLException if an error occurs.
071     */
072    public AssertionArtifact(String theArtifact) throws SAMLException {
073        // check if the input is empty
074        if ((theArtifact == null) || (theArtifact.length() == 0)) {
075            SAMLUtils.debug.message("AssertionArtifact: empty input.");
076            throw new SAMLRequesterException(
077                SAMLUtils.bundle.getString("nullInput"));
078        }
079
080        // decode the artifact
081        byte raw[] = null;
082        try {
083            raw = Base64.decode(theArtifact);
084        } catch (Exception e) {
085            if (SAMLUtils.debug.messageEnabled()) {
086                SAMLUtils.debug.message("AssertionArtifact: exception decode"
087                        + " input:", e);
088            }
089            throw new SAMLRequesterException(
090                SAMLUtils.bundle.getString("wrongInput"));
091        }
092
093        // check if the length is 42bytes
094        if (raw.length != ARTIFACT_1_LENGTH) {
095            if (SAMLUtils.debug.messageEnabled()) {
096                SAMLUtils.debug.message("AssertionArtifact: the length is"
097                        + " not 42:" + raw.length);
098            }
099            throw new SAMLRequesterException(
100                SAMLUtils.bundle.getString("wrongInput"));
101        }
102
103        // check if the typecode is correct
104        if ((raw[0] != ARTIFACT_1_TYPE_CODE_0) ||
105            (raw[1] != ARTIFACT_1_TYPE_CODE_1)) {
106            SAMLUtils.debug.message("AssertionArtifact: wrong typecode.");
107            throw new SAMLRequesterException(
108                SAMLUtils.bundle.getString("wrongInput"));
109        }
110        typeCode = ARTIFACT_1_TYPE_CODE;
111        
112        artifact = theArtifact;
113
114        // get the sourceID and assertionHandle
115        byte sBytes[] = new byte[SAMLConstants.ID_LENGTH];
116        byte aBytes[] = new byte[SAMLConstants.ID_LENGTH];
117        System.arraycopy(raw, 2, sBytes, 0, SAMLConstants.ID_LENGTH);
118        System.arraycopy(raw, 22, aBytes, 0, SAMLConstants.ID_LENGTH);
119
120        try {
121            sourceID = SAMLUtils.byteArrayToString(sBytes);
122            assertionHandle = SAMLUtils.byteArrayToString(aBytes);
123        } catch (Exception e) {
124            SAMLUtils.debug.error("AssertionArtifact: encoding exception: ", e);
125            sourceID = new String(sBytes);
126            assertionHandle = new String(aBytes);
127        }
128    }
129
130    /**
131     * This constructor will be used at the sender side to create a new
132     * <code>AssertionArtifact</code>.
133     *
134     * @param id A string that represents the <code>sourceID</code>.
135     * @param handle A string that represents the <code>assertionHandle</code>.
136     * @exception SAMLException if wrong input or could not encode the artifact.
137     */
138    public AssertionArtifact(String id, String handle) throws SAMLException {
139        if ((id == null) || (handle == null)) {
140            SAMLUtils.debug.message("AssertionArtifact: null input.");
141            throw new SAMLRequesterException(
142                SAMLUtils.bundle.getString("nullInput"));
143        }
144        
145        byte idBytes[] = null;
146        byte handleBytes[] = null;
147        try {
148            idBytes = SAMLUtils.stringToByteArray(id);
149            handleBytes = SAMLUtils.stringToByteArray(handle);
150        } catch (Exception e) {
151            SAMLUtils.debug.error("AssertionArtifact: encoding exception: ",e);
152            idBytes = id.getBytes();
153            handleBytes = handle.getBytes();
154        }
155
156        if ((idBytes.length != SAMLConstants.ID_LENGTH) ||
157            (handleBytes.length != SAMLConstants.ID_LENGTH)) {
158            SAMLUtils.debug.message("AssertionArtifact: wrong input length.");
159            throw new SAMLRequesterException(
160                SAMLUtils.bundle.getString("wrongInput"));
161        }
162
163        sourceID = id;
164        assertionHandle = handle;
165
166        byte raw[] = new byte[42];
167        raw[0] = ARTIFACT_1_TYPE_CODE_0;
168        raw[1] = ARTIFACT_1_TYPE_CODE_1;
169        for (int i = 0; i < SAMLConstants.ID_LENGTH; i++) {
170            raw[2+i] = idBytes[i];
171            raw[22+i] = handleBytes[i];
172        }
173        try {
174            artifact = Base64.encode(raw).trim();
175        } catch (Exception e) {
176            if (SAMLUtils.debug.messageEnabled()) {
177                SAMLUtils.debug.message("AssertionArtifact: exception encode"
178                        + " input:", e);
179            }
180            throw new SAMLRequesterException(
181                SAMLUtils.bundle.getString("errorCreateArtifact"));
182        }
183        typeCode = ARTIFACT_1_TYPE_CODE;
184    }
185
186    /**
187     * Gets the artifact.
188     * @return the string format of the artifact. It's base64 encoded.
189     */
190    public String getAssertionArtifact() {
191        return artifact;
192    }
193
194    /**
195     * Returns the <code>SourceID</code> of the artifact.
196     *
197     * @return The <code>SourceID</code> of the artifact.
198     */
199    public String getSourceID() {
200        return sourceID;
201    }
202
203    /**
204     * Gets the <code>AssertionHandle</code> of the artifact. The result will be
205     * decoded.
206     *
207     * @return The <code>AssertionHandle</code> of the artifact.
208     */
209    public String getAssertionHandle() {
210        return assertionHandle;
211    }
212
213    /**
214     * Gets the <code>typeCode</code> of the artifact.
215     * @return The byte array of the <code>TypeCode</code> for the artifact.
216     */
217    public byte[] getTypeCode() {
218        return typeCode;
219    }
220
221    /**
222     * Translates the <code>AssertionArtifact</code> to an XML document String
223     * based on the SAML schema.
224     * @return An XML String representing the <code>AssertionArtifact</code>.
225     */
226    public String toString() {
227        return toString(true, false);
228    }
229
230    /**
231     * Creates a String representation of the
232     * <code>&lt;samlp:AssertionArtifact&gt;</code> element.
233     *
234     * @param includeNS Determines whether or not the namespace qualifier
235     *        is prepended to the Element when converted
236     * @param declareNS Determines whether or not the namespace is declared
237     *        within the Element.
238     * @return A string containing the valid XML for this element
239     */   
240    public String toString(boolean includeNS, boolean declareNS) {
241        StringBuffer xml = new StringBuffer(100);
242        String prefix = "";
243        if (includeNS) {
244            prefix = SAMLConstants.PROTOCOL_PREFIX;
245        }
246
247        String uri = "";
248        if (declareNS) {
249            uri = SAMLConstants.PROTOCOL_NAMESPACE_STRING;
250        }
251
252        xml.append("<").append(prefix).append("AssertionArtifact").append(uri).
253                append(">").append(artifact).append("</").append(prefix).
254                append("AssertionArtifact>\n");
255        return xml.toString();
256    }
257}




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.