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