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: SASLRequest.java,v 1.2 2008/06/25 05:47:08 qcheng Exp $
026 *
027 */
028
029
030package com.sun.identity.liberty.ws.authnsvc.protocol;
031
032import org.w3c.dom.Element;
033import org.w3c.dom.Document;
034import org.w3c.dom.Node;
035import org.w3c.dom.NodeList;
036
037import com.sun.identity.shared.xml.XMLUtils;
038import com.sun.identity.shared.encode.Base64;
039import com.sun.identity.liberty.ws.authnsvc.AuthnSvcConstants;
040import com.sun.identity.liberty.ws.authnsvc.AuthnSvcException;
041import com.sun.identity.liberty.ws.authnsvc.AuthnSvcUtils;
042
043/**
044 * The <code>SASLRequest</code> class represents <code>SASLRequest</code>
045 * element defined in Authentication Service schema.
046 * @supported.all.api
047 */
048public class SASLRequest {
049    private byte[]  data = null;
050    private Element requestAuthnContext = null;
051    private String mechanism = null;
052    private String authzID = null;
053    private String advisoryAuthnID = null;
054    private String id = null;
055    private String messageID = null;
056    private String refToMessageID = null;
057
058    /**
059     * Constructs a <code>SASLRequest</code> instance.
060     *
061     * @param mechanism Mechanism attribute value.
062     */
063    public SASLRequest(String mechanism) {
064        this.mechanism = mechanism;
065    }
066
067    /**
068     * Constructs a <code>SAMLRequest</code> with a 
069     * <code>org.w3c.dom.Element</code>.
070     * @param element a <code>SASLRequest</code> element
071     * @exception AuthnSvcException if an error occurs while parsing the
072     *            <code>SASLRequest</code> element
073     */
074    public SASLRequest(Element element) throws AuthnSvcException {
075        Element dataE = null;
076
077        NodeList nl = element.getChildNodes();
078        int length = nl.getLength();
079
080        for(int i = 0; i < length; i++) {
081            Node child = nl.item(i);
082            if (child.getNodeType() == Node.ELEMENT_NODE) {
083                Element childElement = (Element)child;
084                String localName = childElement.getLocalName();
085                String namespaceURI = childElement.getNamespaceURI();
086
087                if (AuthnSvcConstants.NS_AUTHN_SVC.equals(namespaceURI) &&
088                    AuthnSvcConstants.TAG_DATA.equals(localName)) {
089 
090                   if (dataE != null) {
091                        throw new AuthnSvcException("tooManyDataInReq");
092                    } else if (requestAuthnContext != null) {
093                        throw new AuthnSvcException("invalidSeqInReq");
094                    }
095                    dataE = childElement;
096                } else if (AuthnSvcConstants.NS_PROTOCOLS_SCHEMA
097                                            .equals(namespaceURI) &&
098                           AuthnSvcConstants.TAG_REQUEST_AUTHN_CONTEXT
099                                            .equals(localName)) {
100                    if (requestAuthnContext != null) {
101                        throw new AuthnSvcException("tooManyReqAuthnCon");
102                    }
103                    requestAuthnContext = childElement;
104                } else {
105                    throw new AuthnSvcException("invalidChildReq");
106                }
107            }
108        }
109
110        data = AuthnSvcUtils.decodeDataElement(dataE);
111
112        mechanism = XMLUtils.getNodeAttributeValue(element,
113                                    AuthnSvcConstants.ATTR_MECHANISM);
114        if (mechanism == null) {
115            String msg = AuthnSvcUtils.getString("missingMechanism");
116            AuthnSvcUtils.debug.error("SASLRequest: " + msg);
117            throw new AuthnSvcException(msg);
118        }
119
120        id = XMLUtils.getNodeAttributeValue(element,
121                                            AuthnSvcConstants.ATTR_id);
122
123        authzID = XMLUtils.getNodeAttributeValue(element,
124                                           AuthnSvcConstants.ATTR_AUTHZ_ID);
125
126        advisoryAuthnID = XMLUtils.getNodeAttributeValue(element,
127                                AuthnSvcConstants.ATTR_ADVISORY_AUTHN_ID);
128
129    }
130
131    /**
132     * Returns value of Element 'Data'.
133     * @return value of Element 'Data'
134     * @see #setData(byte[])
135     */
136    public byte[] getData() {
137        return data;
138    }
139
140    /**
141     * Returns Element <code>RequestAuthnContext</code>.
142     * @return Element <code>RequestAuthnContext</code>
143     * @see #setRequestAuthnContext(Element)
144     */
145    public Element getRequestAuthnContext() {
146        return requestAuthnContext;
147    }
148
149    /**
150     * Returns value of <code>mechanism</code> attribute.
151     * @return value of <code>mechanism</code> attribute
152     * @see #setMechanism(String)
153     */
154    public String getMechanism() {
155        return mechanism;
156    }
157
158    /**
159     * Returns value of <code>authzID</code> attribute.
160     * @return value of <code>authzID</code> attribute
161     * @see #setAuthzID(String)
162     */
163    public String getAuthzID() {
164        return authzID;
165    }
166
167    /**
168     * Returns value of <code>advisoryAuthnID</code> attribute.
169     * @return value of <code>advisoryAuthnID</code> attribute
170     * @see #setAdvisoryAuthnID(String)
171     */
172    public String getAdvisoryAuthnID() {
173        return advisoryAuthnID;
174    }
175
176    /**
177     * Returns value of <code>id</code> attribute.
178     * @return value of <code>id</code> attribute
179     * @see #setId(String)
180     */
181    public String getId() {
182        return id;
183    }
184
185    /**
186     * Returns value of <code>messageID</code> attribute of
187     * <code>CorrelationHeader</code>.
188     * @return value of <code>messageID</code> attribute
189     * @see #setMessageID(String)
190     */
191    public String getMessageID() {
192        return messageID;
193    }
194
195    /**
196     * Returns value of <code>refToMessageID</code> attribute of
197     * <code>CorrelationHeader</code>.
198     * @return value of <code>refToMessageID</code> attribute
199     * @see #setRefToMessageID(String)
200     */
201    public String getRefToMessageID() {
202        return refToMessageID;
203    }
204
205    /**
206     * Sets value of Element 'Data'.
207     * @param data value of Element 'Data'
208     * @see #getData()
209     */
210    public void setData(byte[] data) {
211        this.data = data;
212    }
213
214    /**
215     * Sets Element <code>RequestAuthnContext</code>.
216     * @param requestAuthnContext Element <code>RequestAuthnContext</code>
217     * @see #getRequestAuthnContext()
218     */
219    public void setRequestAuthnContext(Element requestAuthnContext) {
220        this.requestAuthnContext = requestAuthnContext;
221    }
222
223    /**
224     * Sets value of <code>mechanism</code> attribute
225     * @param mechanism value of <code>mechanism</code> attribute
226     * @see #getMechanism()
227     */
228    public void setMechanism(String mechanism) {
229        this.mechanism = mechanism;
230    }
231
232    /**
233     * Sets value of <code>authzID</code> attribute.
234     * @param authzID value of <code>authzID</code> attribute
235     * @see #getAuthzID()
236     */
237    public void setAuthzID(String authzID) {
238        this.authzID = authzID;
239    }
240
241    /**
242     * Sets value of <code>advisoryAuthnID</code> attribute.
243     * @param advisoryAuthnID value of <code>advisoryAuthnID</code> attribute
244     * @see #getAdvisoryAuthnID()
245     */
246    public void setAdvisoryAuthnID(String advisoryAuthnID) {
247        this.advisoryAuthnID = advisoryAuthnID;
248    }
249
250    /**
251     * Sets value of <code>id</code> attribute.
252     * @param id value of <code>id</code> attribute
253     * @see #getId()
254     */
255    public void setId(String id) {
256        this.id = id;
257    }
258
259    /**
260     * Sets value of <code>messageID</code> attribute of
261     * <code>CorrelationHeader</code>.
262     * @param messageID value of <code>messageID</code> attribute
263     * @see #getMessageID()
264     */
265    public void setMessageID(String messageID) {
266        this.messageID = messageID;
267    }
268
269    /**
270     * Sets value of <code>refToMessageID</code> attribute of
271     * <code>CorrelationHeader</code>.
272     * @param refToMessageID value of <code>refToMessageID</code> attribute
273     * @see #getRefToMessageID()
274     */
275    public void setRefToMessageID(String refToMessageID) {
276        this.refToMessageID = refToMessageID;
277    }
278
279    /**
280     * Returns <code>SASLRequest</code> in <code>org.w3c.dom.Element</code>
281     * format.
282     *
283     * @return <code>SASLRequest</code> in <code>org.w3c.dom.Element</code>
284     *         format.
285     * @exception AuthnSvcException if an error occurs while creating the
286     *            <code>SASLRequest</code> element
287     */
288    public Element toElement() throws AuthnSvcException {
289        Document doc = null;
290        try {
291            doc = XMLUtils.newDocument();
292        } catch (Exception ex) {
293            AuthnSvcUtils.debug.error("SASLRequest:toElement", ex);
294            throw new AuthnSvcException(ex.getMessage());
295        }
296
297        Element saslReqE = doc.createElementNS(AuthnSvcConstants.NS_AUTHN_SVC,
298                                         AuthnSvcConstants.PTAG_SASL_REQUEST);
299        saslReqE.setAttributeNS(AuthnSvcConstants.NS_XML,
300                                AuthnSvcConstants.XMLNS_AUTHN_SVC,
301                                AuthnSvcConstants.NS_AUTHN_SVC);
302        saslReqE.setAttributeNS(AuthnSvcConstants.NS_XML,
303                                AuthnSvcConstants.XMLNS_PROTOCOLS_SCHEMA,
304                                AuthnSvcConstants.NS_PROTOCOLS_SCHEMA);
305
306        saslReqE.setAttributeNS(null,
307                                AuthnSvcConstants.ATTR_MECHANISM,
308                                mechanism);
309
310        if (authzID != null) {
311            saslReqE.setAttributeNS(null,
312                                    AuthnSvcConstants.ATTR_AUTHZ_ID,
313                                    authzID);
314        }
315
316        if (advisoryAuthnID != null) {
317            saslReqE.setAttributeNS(null,
318                                    AuthnSvcConstants.ATTR_ADVISORY_AUTHN_ID,
319                                    advisoryAuthnID);
320        }
321
322        if (id != null) {
323            saslReqE.setAttributeNS(null, AuthnSvcConstants.ATTR_id, id);
324        }
325
326        if (data != null) {
327            Element dataE = doc.createElementNS(AuthnSvcConstants.NS_AUTHN_SVC,
328                                         AuthnSvcConstants.PTAG_DATA);
329            dataE.appendChild(doc.createTextNode(Base64.encode(data)));
330            saslReqE.appendChild(dataE);
331        }
332
333        doc.appendChild(saslReqE);
334        return doc.getDocumentElement();
335    }
336}