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: AuthnContext.java,v 1.3 2008/06/25 05:46:46 qcheng Exp $
026 * Portions Copyrighted 2014 ForgeRock AS
027 */
028
029package com.sun.identity.federation.message.common;
030
031import com.sun.identity.federation.common.FSUtils;
032import com.sun.identity.federation.common.IFSConstants;
033import com.sun.identity.saml.common.SAMLConstants;
034import com.sun.identity.shared.xml.XMLUtils;
035import org.w3c.dom.Element;
036import org.w3c.dom.Node;
037import org.w3c.dom.NodeList;
038
039/**
040 * This class <code>AuthnContext</code> represents an Authentication Context
041 * for the authenticated user with a requested authn context.
042 *
043 * @supported.all.api
044 * @deprecated since 12.0.0
045 */
046@Deprecated
047public class AuthnContext {
048    
049    protected String authnContextClassRef = null;
050    protected String authnContextStatementRef = null;
051    protected int minorVersion = 0;
052    
053    /**
054     * Default constructor
055     */
056    public AuthnContext() {}
057    
058    
059    /**
060     * Constructor creates <code>AuthnContext</code> object.
061     *
062     * @param authnContextClassRef Authentication Context Class Reference URI
063     * @param authnContextStatementRef Authentication Context
064     *        Statement Reference URI
065     */
066    public AuthnContext(String authnContextClassRef,
067            String authnContextStatementRef) {
068        this.authnContextClassRef = authnContextClassRef;
069        this.authnContextStatementRef = authnContextStatementRef;
070    }
071    
072    /**
073     * Constructor creates <code>AuthnContext</code> object
074     * from the Document Element.
075     *
076     * @param root the Document Element.
077     * @throws FSMsgException on error.
078     */
079    public AuthnContext(Element root) throws FSMsgException {
080        if (root == null) {
081            FSUtils.debug.message("AuthnContext(): null input.");
082            throw new FSMsgException("nullInput",null);
083        }
084        String tag = root.getLocalName();
085        if ((tag == null) || (!tag.equals("AuthnContext"))) {
086            FSUtils.debug.message("AuthnContext: wrong input.");
087            throw new FSMsgException("wrongInput",null);
088        }
089        String namespace = root.getNamespaceURI();
090        if ((namespace != null) && namespace.equals(IFSConstants.FF_12_XML_NS)){
091            minorVersion = IFSConstants.FF_12_PROTOCOL_MINOR_VERSION;
092        }
093        NodeList nl = root.getChildNodes();
094        int length = nl.getLength();
095        for (int i = 0; i < length; i++) {
096            Node child = nl.item(i);
097            String childName = child.getLocalName();
098            if (childName == null) {
099                continue;
100            }
101            
102            if(childName.equals("AuthnContextClassRef")) {
103                if(authnContextClassRef != null) {
104                    FSUtils.debug.error("AuthnContext(Element): Should"
105                            + "contain only one AuthnContextClassRef element");
106                    throw new FSMsgException("wrongInput",null);
107                }
108                authnContextClassRef = XMLUtils.getElementValue((Element) child);
109                
110            } else if(childName.equals("AuthnContextStatementRef")) {
111                if(authnContextStatementRef != null) {
112                    FSUtils.debug.error("AuthnContext(Element): Should contain "
113                            + " only one AuthnContextStatementRef element");
114                    throw new FSMsgException("wrongInput",null);
115                }
116                authnContextStatementRef =
117                        XMLUtils.getElementValue((Element) child);
118                
119            } else if(childName.equals("AuthenticationContextStatement")) {
120                if(FSUtils.debug.messageEnabled()) {
121                    FSUtils.debug.message("AuthnContext(Element): " +
122                            "Authentication Statement");
123                }
124            }
125        }
126    }
127    
128    /**
129     * Returns the  AuthnContext Class Reference URI.
130     *
131     * @return the  AuthnContext Class Reference URI.
132     * @see #setAuthnContextClassRef
133     */
134    public String getAuthnContextClassRef(){
135        return authnContextClassRef;
136    }
137    
138    /**
139     * Sets the AuthnContext Class Reference URI.
140     *
141     * @param authnContextClassRef AuthnContext Class Ref URI.
142     * @see #getAuthnContextClassRef
143     */
144    public void setAuthnContextClassRef(String authnContextClassRef) {
145        this.authnContextClassRef = authnContextClassRef;
146    }
147    
148    /**
149     * Returns the AuthnContext Statement Reference URI.
150     *
151     * @return the AuthnContext Statement Reference URI.
152     * @see #setAuthnContextStatementRef
153     */
154    public String getAuthnContextStatementRef(){
155        return authnContextStatementRef;
156    }
157    
158    /**
159     * Sets AuthnContext Statement Reference URI.
160     *
161     * @param authnContextStatementRef AuthnContext Statement Ref URI.
162     * @see #getAuthnContextStatementRef
163     */
164    public void setAuthnContextStatementRef(
165            String authnContextStatementRef) {
166        
167        this.authnContextStatementRef = authnContextStatementRef;
168    }
169    
170    /**
171     * Returns the <code>MinorVersion</code> attribute.
172     *
173     * @return the Minor Version.
174     * @see #setMinorVersion(int)
175     */
176    
177    public int getMinorVersion() {
178        return minorVersion;
179    }
180    
181    /**
182     * Sets the <code>MinorVersion</code>.
183     *
184     * @param version the minor version in the assertion.
185     * @see #setMinorVersion(int)
186     */
187    public void setMinorVersion(int version) {
188        minorVersion = version;
189    }
190    
191    /**
192     * Returns the string representation of this object.
193     * This method translates the response to an XML document string.
194     *
195     * @return An XML String representing the response. NOTE: this is a
196     *         complete SAML response xml string with ResponseID,
197     *         MajorVersion, etc.
198     */
199    public String toXMLString() throws FSMsgException {
200        return this.toXMLString(true, true);
201    }
202    
203    /**
204     * Returns a String representation of this object.
205     *
206     * @param includeNS : Determines whether or not the namespace qualifier
207     *        is prepended to the Element when converted
208     * @param declareNS : Determines whether or not the namespace is declared
209     *        within the Element.
210     * @return a string containing the valid XML for this element
211     * @throws FSMsgException if there is an error converting
212     *         this object ot a string.
213     */
214    public String toXMLString(boolean includeNS,boolean declareNS)
215    throws FSMsgException {
216        return toXMLString(includeNS, declareNS, false);
217    }
218    
219    /**
220     * Returns a String representation of this object.
221     *
222     * @param includeNS Determines whether or not the namespace qualifier
223     *        is prepended to the Element when converted
224     * @param declareNS Determines whether or not the namespace is declared
225     *        within the Element.
226     * @param includeHeader Determines whether the output include the xml
227     *        declaration header.
228     * @return a string containing the valid XML for this element
229     * @throws FSMsgException if there is an error converting
230     *         this object ot a string.
231     */
232    public String toXMLString(boolean includeNS,boolean declareNS,
233            boolean includeHeader) throws FSMsgException {
234        StringBuffer xml = new StringBuffer(300);
235        if (includeHeader) {
236            xml.append("<?xml version=\"1.0\" encoding=\"").
237                    append(SAMLConstants.DEFAULT_ENCODING).append("\" ?>");
238        }
239        String prefixAC = "";
240        String prefixLIB = "";
241        String uriAC = "";
242        String uriLIB = "";
243        if (includeNS) {
244            prefixLIB = IFSConstants.LIB_PREFIX;
245            prefixAC = IFSConstants.AC_PREFIX;
246        }
247        
248        if (declareNS) {
249            if(minorVersion == IFSConstants.FF_12_PROTOCOL_MINOR_VERSION) {
250                uriLIB = IFSConstants.LIB_12_NAMESPACE_STRING;
251                uriAC = IFSConstants.AC_12_NAMESPACE_STRING;
252            } else {
253                uriLIB = IFSConstants.LIB_NAMESPACE_STRING;
254                uriAC = IFSConstants.AC_NAMESPACE_STRING;
255            }
256        }
257        
258        xml.append("<").append(prefixLIB).
259                append("AuthnContext").append(uriLIB).append(">");
260        
261        if(authnContextClassRef != null &&
262                !authnContextClassRef.equals("")) {
263            xml.append("<").append(prefixLIB).
264                    append("AuthnContextClassRef").append(">");
265            xml.append(authnContextClassRef);
266            xml.append("</").append(prefixLIB).
267                    append("AuthnContextClassRef").append(">");
268        } else {
269            xml.append("<").append(prefixLIB).
270                    append("AuthnContextClassRef").append(">");
271            xml.append(IFSConstants.DEFAULT_AUTHNCONTEXT_PASSWORD);
272            xml.append("</").append(prefixLIB).
273                    append("AuthnContextClassRef").append(">");
274            
275        }
276        
277        if(authnContextStatementRef != null &&
278                !authnContextStatementRef.equals("")) {
279            xml.append("<").append(prefixLIB).
280                    append("AuthnContextStatementRef").append(">");
281            xml.append(authnContextStatementRef);
282            xml.append("</").append(prefixLIB).
283                    append("AuthnContextStatementRef").append(">");
284        } else {
285            xml.append("<").append(prefixLIB).
286                    append("AuthnContextStatementRef").append(">");
287            xml.append(IFSConstants.DEFAULT_AUTHNCONTEXT_PASSWORD);
288            xml.append("</").append(prefixLIB).
289                    append("AuthnContextStatementRef").append(">");
290        }
291        
292        xml.append("</").append(prefixLIB).append("AuthnContext").append(">");
293        return xml.toString();
294    }
295}