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: AuthenticationQuery.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.assertion.Subject;
033
034import com.sun.identity.saml.common.SAMLConstants;
035import com.sun.identity.saml.common.SAMLException;
036import com.sun.identity.saml.common.SAMLRequesterException;
037import com.sun.identity.saml.common.SAMLUtils;
038
039import java.util.List;
040
041import org.w3c.dom.Element;
042import org.w3c.dom.Node;
043import org.w3c.dom.NodeList;
044
045/**
046 * This concrete class extends from the abstract base class
047 * <code>SubjectQuery</code>.
048 * It represents the query for an authentication assertion. It corresponds
049 * to the <code>&lt;samlp:AuthenticationQueryType&gt;</code> in the SAML
050 * protocol schema.
051 *
052 * @supported.all.api
053 */
054public class AuthenticationQuery extends SubjectQuery {
055
056    protected String authMethod = null;
057   
058    /**
059     * Default Constructor
060     */
061    protected AuthenticationQuery() {
062    }
063   
064    /**
065     * This constructor is used to build an Authentication Query from a DOM
066     * tree that was built from the XML string.
067     *
068     * @param element the DOM tree element which contains an Authentication
069     * Query.
070     * @exception SAMLException when an error occurs.
071     */
072    public AuthenticationQuery(Element element) throws SAMLException {
073        // make sure input is not null
074        if (element == null) {
075            SAMLUtils.debug.message("AuthenticationQuery: null input.");
076            throw new SAMLRequesterException(
077                        SAMLUtils.bundle.getString("nullInput"));
078        }
079
080        // check if it's an AuthenticationQuery
081        boolean valid = SAMLUtils.checkQuery(element, "AuthenticationQuery");
082        if (!valid) {
083            SAMLUtils.debug.message("AuthenticationQuery: Wrong input.");
084            throw new SAMLRequesterException(
085                SAMLUtils.bundle.getString("wrongInput"));
086        }
087
088        // Not checking whether Subject is in front of ConfirmatinMethod XXX
089        // But it is checking that there is only one Subject, and
090        // 0 or 1 ConfirmationMethod.
091
092        NodeList nl = element.getChildNodes();
093        Node child;
094        String childName;
095        int length = nl.getLength();
096        // loop through all the children including TEXT and COMMENT
097        for (int k = 0; k < length; k++) {
098            child = nl.item(k);
099            if ((childName = child.getLocalName()) != null) {
100                if (childName.equals("Subject")) {
101                    if (subject != null) {
102                        if (SAMLUtils.debug.messageEnabled()) {
103                            SAMLUtils.debug.message("AuthenticationQuery: "
104                                + "contained more than one <Subject>");
105                        }
106                        throw new SAMLRequesterException(
107                            SAMLUtils.bundle.getString("moreElement"));
108                    }
109                    subject = new Subject((Element) child);
110                } else {
111                    if (SAMLUtils.debug.messageEnabled()) {
112                        SAMLUtils.debug.message("AuthenticationQuery: included"
113                                + " wrong element:" + childName);
114                    }
115                    throw new SAMLRequesterException(
116                        SAMLUtils.bundle.getString("wrongInput"));
117                }
118            } // end childName != null
119        } // end for loop
120        // make sure there is one Subject
121        if (subject == null) {
122            SAMLUtils.debug.message("AuthenticationQuery: missing Subject.");
123            throw new SAMLRequesterException(
124                        SAMLUtils.bundle.getString("missingElement"));
125        }
126
127        // obtain the AuthenticationMethod attribute if any
128        if (element.hasAttribute("AuthenticationMethod")) {
129            authMethod = element.getAttribute("AuthenticationMethod");
130        }
131    }
132           
133    /** 
134     * Constructor.
135     *
136     * @param subject the Subject of the <code>AuthenticationQuery</code>.
137     * @param authMethod the <code>AuthenticationMethod</code> in string
138     *        format. It could be null.
139     * @throws SAMLException
140     */
141    public AuthenticationQuery(Subject subject,
142                                String authMethod) 
143                                throws SAMLException {
144        if (subject == null) {
145            SAMLUtils.debug.message("AuthenticationQuery: missing Subject.");
146            throw new SAMLRequesterException(
147                        SAMLUtils.bundle.getString("missingElement"));
148        }
149        this.subject = subject;
150        this.authMethod = authMethod;
151    }
152
153    /**
154     * Constructor.
155     *
156     * @param subject The Subject of the <code>AuthenticationQuery</code>.
157     * @throws SAMLException
158     */
159    public AuthenticationQuery(Subject subject) throws SAMLException {
160        if (subject == null) {
161            SAMLUtils.debug.message("AuthenticationQuery: missing Subject.");
162            throw new SAMLRequesterException(
163                        SAMLUtils.bundle.getString("missingElement"));
164        }
165        this.subject = subject;
166    }
167
168    /**
169     * Returns the <code>AuthenticationMethod</code>.
170     *
171     * @return <code>AuthenticationMethod</code> in string format; or null
172     *         if there is none.
173     */
174    public String  getAuthenticationMethod() {
175        return authMethod;
176    }
177                        
178    /**
179     * Returns the type of this query.
180     *
181     * @return <code>Query.AUTHENTICATION_QUERY</code>.
182     */
183    public int getQueryType() {
184        return Query.AUTHENTICATION_QUERY;
185    }
186
187    /**
188     * Translates the <code>AuthenticationQuery</code> to an XML document 
189     * String based on the <code>AuthenticationQuery</code> schema described
190     * above.
191     *
192     * @return An XML String representing the <code>AuthenticationQuery</code>.
193     */
194    public String toString() {
195        return this.toString(true, false);
196    }
197
198    /**
199     * Returns a String representation of the <samlp:AuthenticationQuery> 
200     * element.
201     *
202     * @param includeNS Determines whether or not the namespace qualifier
203     *        is prepended to the Element when converted
204     * @param declareNS Determines whether or not the namespace is declared
205     *        within the Element.
206     * @return A string containing the valid XML for this element
207     */
208    public String toString(boolean includeNS, boolean declareNS) {
209        StringBuffer xml = new StringBuffer(200);
210        String prefix = "";
211        String uri = "";
212        if (includeNS) {
213            prefix = SAMLConstants.PROTOCOL_PREFIX;
214        }
215        if (declareNS) {
216            uri = SAMLConstants.PROTOCOL_NAMESPACE_STRING;
217        }
218        xml.append("<").append(prefix).append("AuthenticationQuery").
219                append(uri);
220        if (authMethod != null) {
221            xml.append(" AuthenticationMethod=\"").append(authMethod).
222                append("\"");
223        }
224        xml.append(">\n").append(subject.toString(true, true));
225        xml.append("</").append(prefix).append("AuthenticationQuery>\n");
226        return xml.toString();
227    }
228}




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.