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: FSAuthnResponseEnvelope.java,v 1.2 2008/06/25 05:46:43 qcheng Exp $
026 * Portions Copyrighted 2014 ForgeRock AS
027 */
028
029package com.sun.identity.federation.message;
030
031import com.sun.identity.federation.common.FSUtils;
032import com.sun.identity.federation.common.IFSConstants;
033import com.sun.identity.federation.message.common.FSMsgException;
034import com.sun.identity.saml.common.SAMLException;
035import com.sun.identity.shared.encode.Base64;
036import com.sun.identity.shared.xml.XMLUtils;
037import java.util.List;
038import org.w3c.dom.Document;
039import org.w3c.dom.Element;
040import org.w3c.dom.Node;
041import org.w3c.dom.NodeList;
042
043/**
044 * This  class defines methods for setting and retrieving attributes and
045 * elements associated with a Liberty Response . 
046 *
047 * @supported.all.api
048 * @deprecated since 12.0.0
049 */
050@Deprecated
051
052public class FSAuthnResponseEnvelope {
053   private List otherElements;
054   private FSAuthnResponse authnResponse;
055   private String assertionConsumerServiceURL = null;
056   private int minorVersion = IFSConstants.FF_11_PROTOCOL_MINOR_VERSION;
057   
058   /**
059    * Default Constructor.
060    */
061    public FSAuthnResponseEnvelope() {    
062    }
063   
064  /**
065   * Constructor create <code>FSAuthnResponseEnvelope</code> object.
066   *
067   * @param authnResponse the <code>FSAuthnResponse</code> object.
068   */
069   public FSAuthnResponseEnvelope(FSAuthnResponse authnResponse) {    
070       this.authnResponse = authnResponse;
071       this.otherElements = null;
072   }
073   
074  /**
075   * Constructor create <code>FSAuthnResponseEnvelope</code> object.
076   *
077   * @param root the Document element .
078   * @throws FSMsgException if there is an error creating the object.
079   * @throws SAMLException if there is an error creating the object.
080   */
081   public FSAuthnResponseEnvelope(Element root) 
082                    throws FSMsgException, SAMLException {    
083        if (root == null) {
084            FSUtils.debug.message("FSAuthnResponseEnvelope.parseXML: "
085                                  + "null input.");
086            throw new FSMsgException("nullInput",null);
087        }
088        String tag = null;
089        if (((tag = root.getLocalName()) == null) ||
090                        (!tag.equals(IFSConstants.AUTHN_RESPONSE_ENVELOPE))) {
091            FSUtils.debug.message("FSAuthnResponseEnvelope.parseXML: "
092                                  + "wrong input.");
093            throw new FSMsgException("wrongInput",null);
094        }
095        String ns = root.getNamespaceURI();
096        if (ns == null) {
097           FSUtils.debug.error("FSAuthnResponseEnvelope(Element):"
098                               + "No namespace");
099           throw new FSMsgException("wrongInput",null);
100        }
101
102        if (ns.equals(IFSConstants.FF_12_XML_NS)) {
103           minorVersion = IFSConstants.FF_12_PROTOCOL_MINOR_VERSION;
104        }
105        NodeList nl = root.getChildNodes();
106        Node child;
107        String childName;
108        int length = nl.getLength();
109        for (int i = 0; i < length; i++) {
110            child = nl.item(i);
111            if ((childName = child.getLocalName()) != null) {
112                if (childName.equals(IFSConstants.AUTHN_RESPONSE)) {
113                    if (authnResponse != null) {
114                        if (FSUtils.debug.messageEnabled()) {
115                            FSUtils.debug.message("FSAuthnResponseEnvelope: "
116                                + "included more than one <AuthnResponse>");
117                        }
118                        throw new FSMsgException("moreElement",null);
119                    }
120                    authnResponse = new FSAuthnResponse((Element) child);
121                } else if (childName.equals(
122                              IFSConstants.ASSERTION_CONSUMER_SERVICE_URL)) {
123                    assertionConsumerServiceURL = 
124                        XMLUtils.getElementValue((Element) child);                    
125                } 
126            } 
127       }
128   }
129
130  /** 
131   * Returns the value of <code>MinorVersion</code> attribute.
132   *
133   * @return the value of <code>MinorVersion</code> attribute.
134   * @see #setMinorVersion(int)
135   */
136   public int getMinorVersion() {
137       return minorVersion;
138   }
139
140  /**
141   * Sets the value of <code>MinorVersion<code> attribute.
142   *
143   * @param minorVersion the <code>MinorVersion</code> attribute.
144   */
145   public void setMinorVersion(int minorVersion) {
146       this.minorVersion = minorVersion;
147   }
148
149   /**
150    * Returns a list of elements.
151    *
152    * @return list of elements.
153    * @see #setOtherElements(List)
154    */
155    public List getOtherElements() {
156        return otherElements;
157    }
158  /**
159   * Sets a list of elements.
160   *
161   * @param otherElement a list of elements.
162   * @see #getOtherElements
163   */
164   public void setOtherElements(List otherElement) {
165       this.otherElements = otherElement;
166   }
167   
168   /**
169    * Returns the <code>FSAuthnResponse</code> object.
170    *
171    * @return the <code>FSAuthnResponse</code> object.
172    * @see #setAuthnResponse(FSAuthnResponse)
173    */
174
175   public FSAuthnResponse getAuthnResponse() {
176       return authnResponse;
177   }
178   
179   /**
180    * Sets the <code>FSAuthnResponse</code> object.
181    *
182    * @param authnResponse the <code>FSAuthnResponse</code> object.
183    * @see #getAuthnResponse
184    */
185
186   public void setAuthnResponse(FSAuthnResponse authnResponse) {
187       this.authnResponse = authnResponse;
188   }
189    
190   /**
191    * Returns the Assertion Consumer Service URL.
192    *
193    * @return the  Assertion Consumer Service URL.
194    * @see #setAssertionConsumerServiceURL(String)
195    */
196    public String getAssertionConsumerServiceURL() {
197       return assertionConsumerServiceURL;
198   }
199   /**
200    * Sets the Assertion Consumer Service URL.
201    *
202    * @param assertionConsumerUrl the Assertion Consumer Service Identifier.
203    * @see #getAssertionConsumerServiceURL
204    */
205    public void setAssertionConsumerServiceURL(String assertionConsumerUrl) {
206        this.assertionConsumerServiceURL = assertionConsumerUrl;
207   }
208    
209   /**
210    * Returns the <code>FSAuthnResponseEnvelope</code> object.
211    *
212    * @param xml the XML string to create this object from
213    * @return <code>FSAuthnResponseEnvelope</code> object.
214    * @throws FSMsgException if there is error creating the object.
215    */
216   public static FSAuthnResponseEnvelope parseXML(String xml)
217                                         throws FSMsgException {
218      try {
219           Document doc = XMLUtils.toDOMDocument(xml, FSUtils.debug);
220           Element root = doc.getDocumentElement();
221           return new FSAuthnResponseEnvelope(root);           
222       } catch(SAMLException ex){
223           if (FSUtils.debug.messageEnabled()) {
224                    FSUtils.debug.message("FSAuthnResponseEnvelope.parseXML: "
225                                + "Error while parsing input xml string");
226                }
227           throw new FSMsgException("parseError",null);
228        }          
229   }
230   /**
231    * Returns XML document String for this object based on the Response Schema.
232    *
233    * @return XML String representing the <code>Response</code>
234    * @throws FSMsgException if there is an error.
235    */
236    public String toXMLString() throws FSMsgException {
237        return toXMLString(true, true);
238    }
239    
240   /**
241    * Creates a String representation of the <code>Response<code> object.
242    * @param includeNS : Determines whether or not the namespace qualifier
243    *        is prepended to the Element when converted
244    * @param declareNS : Determines whether or not the namespace is declared
245    *        within the Element.
246    * @return A string containing the valid XML for this element.
247    * @throws FSMsgException if there is an error.
248    */
249    public String toXMLString(boolean includeNS, boolean declareNS)
250                              throws FSMsgException {
251        return toXMLString(includeNS, declareNS, false);
252    }
253    
254    /**
255     * Creates a String representation of the  <code>Response</code> object.
256     *
257     * @param includeNS  Determines whether or not the namespace qualifier
258     *        is prepended to the Element when converted
259     * @param declareNS  Determines whether or not the namespace is declared
260     *        within the Element.
261     * @param includeHeader Determines whether the output include the xml
262     *        declaration header.
263     * @return a string containing the valid XML for this object.
264     * @throws FSMsgException if there is an error.
265     */
266    public String toXMLString(boolean includeNS,
267                              boolean declareNS,
268                              boolean includeHeader) throws FSMsgException {        
269                        
270        StringBuffer xml = new StringBuffer(300);
271        if (includeHeader) {
272            xml.append(IFSConstants.XML_PREFIX)
273               .append(IFSConstants.DEFAULT_ENCODING)
274               .append(IFSConstants.QUOTE)
275               .append(IFSConstants.SPACE)
276               .append(IFSConstants.QUESTION_MARK)
277               .append(IFSConstants.RIGHT_ANGLE);
278        }
279        String prefix = "";
280        String uri = "";
281        if (includeNS) {
282            prefix = IFSConstants.LIB_PREFIX;
283        }
284        if (declareNS) {
285            if (minorVersion == IFSConstants.FF_12_PROTOCOL_MINOR_VERSION) {
286                uri = IFSConstants.LIB_12_NAMESPACE_STRING;
287            } else {
288               uri = IFSConstants.LIB_NAMESPACE_STRING;
289            }
290        }       
291        xml.append(IFSConstants.LEFT_ANGLE)
292           .append(prefix)
293           .append(IFSConstants.AUTHN_RESPONSE_ENVELOPE)
294           .append(uri)
295           .append(IFSConstants.RIGHT_ANGLE);            
296
297        if (authnResponse != null) {
298            xml.append(authnResponse.toXMLString());
299        }
300
301        if(assertionConsumerServiceURL != null && 
302            assertionConsumerServiceURL.length() != 0) {
303            xml.append(IFSConstants.LEFT_ANGLE)
304               .append(prefix)
305               .append(IFSConstants.ASSERTION_CONSUMER_SERVICE_URL)
306               .append(uri)
307               .append(IFSConstants.RIGHT_ANGLE)
308               .append(assertionConsumerServiceURL)
309               .append(IFSConstants.START_END_ELEMENT)
310               .append(prefix)
311               .append(IFSConstants.ASSERTION_CONSUMER_SERVICE_URL)
312               .append(IFSConstants.RIGHT_ANGLE);            
313        } else {
314            throw new FSMsgException("nullInput",null);
315        }
316        xml.append(IFSConstants.START_END_ELEMENT)
317           .append(prefix)
318           .append(IFSConstants.AUTHN_RESPONSE_ENVELOPE);
319
320        return xml.toString();
321    }
322      
323   /**
324    * Returns a <code>Base64</code> Encoded String.
325    *
326    * @return a <code>Base64</code> Encoded String.
327    * @throws FSMsgException if there is an error encoding     
328    *         the string.
329    */
330    public String toBASE64EncodedString() throws FSMsgException  {        
331        return Base64.encode(this.toXMLString().getBytes());
332    }
333    
334   /**
335    * Returns <code>FSAuthnResponseEnvelope</code> object. The
336    * object is creating by parsing the <code>Base64</code>
337    * encoded <code>XML</code> string.
338    *
339    * @param encodedReq the <code>Based64</code> encoded <code>XML</code> 
340    *        string.
341    * @throws FSMsgException if there is an error
342    *         creating <code>FSAuthnResponseEnvelope</code> object.
343    */
344    public static FSAuthnResponseEnvelope parseBASE64EncodedString(
345                                   String encodedReq) throws FSMsgException {        
346        if (encodedReq != null) {
347            String decodedAuthnReq = new String(Base64.decode(encodedReq));
348            if (FSUtils.debug.messageEnabled()) {
349                FSUtils.debug.message("FSAuthnResponseEnvelope."
350                    + "parseBASE64EncodedString: decoded input string: \n"
351                    + decodedAuthnReq);
352            }
353            return parseXML(decodedAuthnReq);
354        } else {
355            if (FSUtils.debug.messageEnabled()) {
356                FSUtils.debug.message("FSAuthnResponseEnvelope."
357                    + "parseBASE64EncodedString: null String passed"
358                    + "in as argument.");
359            }
360            throw new FSMsgException("nullInput",null);            
361        }
362    }
363}




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.