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: ProxySubject.java,v 1.2 2008/06/25 05:47:20 qcheng Exp $
026 *
027 */
028
029
030package com.sun.identity.liberty.ws.security;
031
032import com.sun.identity.saml.common.SAMLUtils;
033import com.sun.identity.saml.common.SAMLException;
034import com.sun.identity.saml.common.SAMLRequesterException;
035
036import com.sun.identity.saml.assertion.NameIdentifier;
037import com.sun.identity.saml.assertion.SubjectConfirmation;
038import com.sun.identity.saml.assertion.Subject;
039
040import com.sun.identity.liberty.ws.common.wsse.WSSEConstants;
041
042import org.w3c.dom.Element; 
043import org.w3c.dom.Node;
044import org.w3c.dom.NodeList;
045
046/** 
047 * The <code>ProxySubject</code> class represents the identity of a proxy,
048 * the confirmation key and confirmation obligation the proxy must posess and
049 * demonstrate for authentication purpose.
050 *
051 * @supported.all.api
052 */
053public class ProxySubject extends Subject {
054    
055    /**
056     * Constructs a <code>ProxySubject</code> object from a
057     * <code>NameIdentifier</code> object and a
058     * <code>SubjectConfirmation</code> object.
059     *
060     * @param nameIdentifier <code>NameIdentifier</code> object.
061     * @param subjectConfirmation <code>SubjectConfirmation</code> object.
062     * @throws SAMLException if <code>nameIdentifier</code> and
063     *            <code>SubjectConfirmation</code> are null;
064     */
065    public ProxySubject(NameIdentifier nameIdentifier, SubjectConfirmation
066            subjectConfirmation)  throws SAMLException {
067        super(nameIdentifier, subjectConfirmation);
068    }
069    
070    /**
071     * Checks for equality between this object and the <code>ProxySubject</code>
072     * passed down as parameter.
073     *
074     * @param subject <code>ProxySubject</code> to be checked
075     * @return true if the two are EXACTLY equal.
076     */
077    public boolean equals(ProxySubject subject) {
078        return super.equals(subject);
079    }
080    
081    /**
082     * Constructs a <code>ProxySubject</code> object from a
083     * <code>NameIdentifier</code> object.
084     *
085     * @param nameIdentifier <code>NameIdentifier</code> object.
086     * @throws SAMLException if <code>nameIdentifier</code> is null.
087     */
088    public ProxySubject(NameIdentifier nameIdentifier)  throws SAMLException {
089        super(nameIdentifier);
090    }
091    
092    /**
093     * Constructs a <code>ProxySubject</code> object from a DOM Element.
094     * which has already been built into a DOM.
095     *
096     * @param subjectElement An Element representing DOM tree for
097     *        <code>ProxySubject</code> object.
098     * @throws SAMLException if it could not process the
099     *            Element properly, implying that there is an error in the
100     *            sender or in the element definition.
101     */
102    public ProxySubject(org.w3c.dom.Element subjectElement)
103    throws SAMLException {
104        int elementCount=0;
105        Element elt = (Element)subjectElement;
106        String eltName = elt.getLocalName();
107        if (eltName == null)  {
108            if (SAMLUtils.debug.messageEnabled()) {
109                SAMLUtils.debug.message("ProxySubject: local name missing");
110            }
111            throw new SAMLRequesterException(SAMLUtils.bundle.getString
112                    ("nullInput")) ;
113        }
114        if (!(eltName.equals("ProxySubject")))  {
115            if (SAMLUtils.debug.messageEnabled()) {
116                SAMLUtils.debug.message("ProxySubject: invalid root element");
117            }
118            throw new SAMLRequesterException(SAMLUtils.bundle.getString(
119                    "invalidElement")) ;
120        }
121        NodeList nl = subjectElement.getChildNodes();
122        int length = nl.getLength();
123        if (length == 0 ) {
124            if (SAMLUtils.debug.messageEnabled()) {
125                SAMLUtils.debug.message("Subject: No sub elements found");
126            }
127            throw new SAMLRequesterException(SAMLUtils.bundle.getString(
128                    "emptyElement")) ;
129        }
130        // TODO: sequence is not checked as yet
131        for (int n=0; n < length; n++) {
132            Node child = (Node)nl.item(n);
133            if (child.getNodeType() != Node.ELEMENT_NODE) {
134                continue;
135            }
136            String childName = child.getLocalName();
137            if (childName.equals("NameIdentifier"))  {
138                setNameIdentifier(new NameIdentifier((Element)child));
139                elementCount++;
140            } else if (childName.equals("SubjectConfirmation"))  {
141                setSubjectConfirmation(new SubjectConfirmation((Element)child));
142                elementCount++;
143            } else {
144                if (SAMLUtils.debug.messageEnabled()) {
145                    SAMLUtils.debug.message("Subject: Invalid element "
146                            + "encountered.");
147                }
148                throw new SAMLRequesterException(SAMLUtils.bundle.getString(
149                        "invalidElement")) ;
150            }
151        }
152        if (elementCount > 2 ) {
153            if (SAMLUtils.debug.messageEnabled()) {
154                SAMLUtils.debug.message("Subject: more than allowed elements "
155                        + "passed");
156            }
157            throw new SAMLRequesterException(SAMLUtils.bundle.getString(
158                    "moreElement")) ;
159        }
160    }
161    
162    /**
163     * Constructs a <code>ProxySubject</code> object from a
164     * <code>SubjectConfirmation</code> object.
165     *
166     * @param subjectConfirmation <code>SubjectConfirmation</code> object to be
167     *        added to the object.
168     * @throws SAMLException if <code>subjectConfirmation</code> is null.
169     */
170    public ProxySubject(SubjectConfirmation subjectConfirmation)
171    throws SAMLException {
172        super(subjectConfirmation);
173    }
174    
175    /**
176     * Creates a String representation of the element.
177     *
178     * @return A string containing the valid XML for this element.
179     *         By default name space name is prepended to the element name
180     *         example <code>&lt;saml:Subject&gt;</code>
181     */
182    public java.lang.String toString() {
183        // call toString() with includeNS true by default and declareNS false
184        String xml = this.toString(true, false);
185        return xml;
186    }
187    
188    /**
189     * Creates a String representation of the <code>&lt;Subject&gt;</code>
190     * element.
191     *
192     * @param includeNS if true prepends all elements by their Namespace
193     *        name example <code>&lt;saml:Subject&gt;</code>.
194     * @param declareNS if true includes the namespace within the
195     *        generated XML.
196     * @return String containing the valid XML for this element.
197     */
198    public java.lang.String toString(boolean includeNS, boolean declareNS) {
199        StringBuffer xml = new StringBuffer(3000);
200        String secprefix = "";
201        String secNS = "";
202        String secNSString = "";
203        
204        if (includeNS) {
205            secprefix = WSSEConstants.TAG_SEC + ":";
206        }
207        
208        if (declareNS) {
209            secNS = WSSEConstants.NS_SEC;
210            secNSString = " " + WSSEConstants.TAG_XMLNS + ":" +
211                    WSSEConstants.TAG_SEC + "=" + "\"" + secNS + "\"";
212        }
213        
214        xml.append("<").append(secprefix).
215                append(WSSEConstants.TAG_PROXYSUBJECT).
216                append(secNSString).append(">");
217        
218        if (getNameIdentifier() != null ) {
219            xml.append(getNameIdentifier().toString(includeNS, declareNS));
220        }
221        if (getSubjectConfirmation() != null)  {
222            xml.append(getSubjectConfirmation().toString(includeNS, declareNS));
223        }
224        xml.append("</").append(secprefix).
225                append(WSSEConstants.TAG_PROXYSUBJECT).append(">");
226        return xml.toString();
227    }
228}
229




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.