RequestSecurityToken_Impl.java revision 4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1c
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest/**
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest *
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest * Copyright (c) 2008 Sun Microsystems Inc. All Rights Reserved
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest *
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest * The contents of this file are subject to the terms
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest * of the Common Development and Distribution License
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest * (the License). You may not use this file except in
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest * compliance with the License.
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest *
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest * You can obtain a copy of the License at
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest * https://opensso.dev.java.net/public/CDDLv1.0.html or
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest * opensso/legal/CDDLv1.0.txt
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest * See the License for the specific language governing
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest * permission and limitations under the License.
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest *
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest * When distributing Covered Code, include this CDDL
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest * Header Notice in each file and include the License file
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest * at opensso/legal/CDDLv1.0.txt.
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest * If applicable, add the following below the CDDL Header,
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest * with the fields enclosed by brackets [] replaced by
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest * your own identifying information:
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest * "Portions Copyrighted [year] [name of copyright owner]"
ea65b6bac936db133a660beef9d75f044dd7ce84Andrew Forrest *
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest * $Id: RequestSecurityToken_Impl.java,v 1.3 2009/10/13 23:19:50 mallas Exp $
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest *
ea65b6bac936db133a660beef9d75f044dd7ce84Andrew Forrest */
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrestpackage com.sun.identity.wss.trust.wst10;
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrestimport org.w3c.dom.Element;
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrestimport org.w3c.dom.NodeList;
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrestimport org.w3c.dom.Node;
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrestimport java.util.Iterator;
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrestimport java.util.ArrayList;
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrestimport com.sun.identity.wss.trust.RequestSecurityToken;
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrestimport com.sun.identity.wss.trust.WSTException;
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrestimport com.sun.identity.wss.sts.STSConstants;
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrestimport com.sun.identity.wss.sts.STSUtils;
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrestimport com.sun.identity.wss.trust.ClaimType;
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrestimport com.sun.identity.shared.xml.XMLUtils;
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrestpublic class RequestSecurityToken_Impl extends RequestSecurityToken {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest private Element rstE = null;
ea65b6bac936db133a660beef9d75f044dd7ce84Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest public RequestSecurityToken_Impl() {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest // Constructor
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest }
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest public RequestSecurityToken_Impl(Element element) throws WSTException {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest //TODO Schema checking
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest if(element == null) {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest throw new WSTException("NullElement");
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest }
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest if(!REQUEST_SECURITY_TOKEN.equals(element.getLocalName())) {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest throw new WSTException("InvalidElement");
ea65b6bac936db133a660beef9d75f044dd7ce84Andrew Forrest }
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest if(!STSConstants.WST10_NAMESPACE.equals(element.getNamespaceURI())) {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest throw new WSTException("InvalidNameSpace");
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest }
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest this.rstE = element;
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest NodeList nl = element.getChildNodes();
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest for (int i=0; i < nl.getLength(); i++) {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest Node node = nl.item(i);
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest if(node.getNodeType() != Node.ELEMENT_NODE) {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest continue;
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest }
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest Element child = (Element)node;
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest String localName = child.getLocalName();
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest if(TOKEN_TYPE.equals(localName)) {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest tokenType = XMLUtils.getElementValue(child);
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest } else if(REQUEST_TYPE.equals(localName)) {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest requestType = XMLUtils.getElementValue(child);
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest } else if(APPLIES_TO.equals(localName)) {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest appliesTo = STSUtils.getAppliesTo(child);
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest } else if(ON_BEHALF_OF.equals(localName)) {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest oboToken = (Element)child.getFirstChild();
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest } else if(KEY_TYPE.equals(localName)) {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest keyType = XMLUtils.getElementValue(child);
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest } else if(CLAIMS.equals(localName)) {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest NodeList claimNodes = child.getChildNodes();
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest claimTypes = new ArrayList();
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest for (int j =0; j < claimNodes.getLength(); j++) {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest Node n = claimNodes.item(j);
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest if(n.getNodeType() != Node.ELEMENT_NODE) {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest continue;
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest }
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest ClaimType claimType =
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest new ClaimType((Element)n);
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest claimTypes.add(claimType);
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest }
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest }
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest }
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest }
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest public Element toDOMElement() throws WSTException {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest if(rstE != null) {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest return rstE;
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest }
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest return XMLUtils.toDOMDocument(
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest toXMLString(), STSUtils.debug).getDocumentElement();
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest }
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest public String toXMLString() throws WSTException {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest StringBuffer sb = new StringBuffer(300);
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest sb.append("<").append(STSConstants.WST_PREFIX)
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(REQUEST_SECURITY_TOKEN).append(" ")
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(STSConstants.WST_XMLNS).append("=\"")
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(STSConstants.WST10_NAMESPACE).append("\"")
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(" ").append(STSConstants.WSP_XMLNS).append("=")
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append("\"").append(STSConstants.WSP_NS).append("\"")
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(" ").append(STSConstants.WSA_XMLNS).append("=")
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append("\"").append(STSConstants.WSA_NS).append("\"")
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(">");
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest if(tokenType != null) {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest sb.append("<").append(STSConstants.WST_PREFIX).append(TOKEN_TYPE)
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(">").append(tokenType).append("</")
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(STSConstants.WST_PREFIX)
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(TOKEN_TYPE).append(">");
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest }
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest if(requestType == null || requestType.length() == 0) {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest throw new WSTException("RequestType is null");
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest }
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest sb.append("<").append(STSConstants.WST_PREFIX).append(REQUEST_TYPE)
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(">").append(requestType).append("</")
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(STSConstants.WST_PREFIX)
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(REQUEST_TYPE).append(">");
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest if(appliesTo != null) {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest sb.append("<").append(STSConstants.WSP_PREFIX)
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(APPLIES_TO).append(">")
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append("<").append(STSConstants.WSA_PREFIX)
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(EP_REFERENCE).append(">").append("<")
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(STSConstants.WSA_PREFIX)
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(ADDRESS).append(">").append(appliesTo)
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append("</").append(STSConstants.WSA_PREFIX)
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(ADDRESS).append(">")
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append("</").append(STSConstants.WSA_PREFIX)
ea65b6bac936db133a660beef9d75f044dd7ce84Andrew Forrest .append(EP_REFERENCE).append(">")
ea65b6bac936db133a660beef9d75f044dd7ce84Andrew Forrest .append("</").append(STSConstants.WSP_PREFIX)
ea65b6bac936db133a660beef9d75f044dd7ce84Andrew Forrest .append(APPLIES_TO).append(">");
ea65b6bac936db133a660beef9d75f044dd7ce84Andrew Forrest }
ea65b6bac936db133a660beef9d75f044dd7ce84Andrew Forrest
ea65b6bac936db133a660beef9d75f044dd7ce84Andrew Forrest if(claimTypes != null && !claimTypes.isEmpty()) {
ea65b6bac936db133a660beef9d75f044dd7ce84Andrew Forrest sb.append("<").append(STSConstants.WST_PREFIX)
ea65b6bac936db133a660beef9d75f044dd7ce84Andrew Forrest .append(CLAIMS).append(" Dialect=").append("\"")
ea65b6bac936db133a660beef9d75f044dd7ce84Andrew Forrest .append(ClaimType.IDENTITY_NS).append("\"").append(">");
6202ba4d983650b484ebe9607707003fef5e09c6Andrew Forrest for (Iterator iter = claimTypes.iterator(); iter.hasNext();) {
6202ba4d983650b484ebe9607707003fef5e09c6Andrew Forrest ClaimType ct = (ClaimType)iter.next();
6202ba4d983650b484ebe9607707003fef5e09c6Andrew Forrest sb.append(ct.toXMLString());
ea65b6bac936db133a660beef9d75f044dd7ce84Andrew Forrest }
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest sb.append("</").append(STSConstants.WST_PREFIX)
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(CLAIMS).append(">");
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest }
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest if(oboToken != null) {
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest sb.append("<").append(STSConstants.WST_PREFIX)
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(ON_BEHALF_OF).append(">")
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(XMLUtils.print(oboToken)).append("</")
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(STSConstants.WST_PREFIX)
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(ON_BEHALF_OF).append(">");
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest }
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest if(keyType != null) {
ea65b6bac936db133a660beef9d75f044dd7ce84Andrew Forrest sb.append("<").append(STSConstants.WST_PREFIX)
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest .append(KEY_TYPE).append(">")
6202ba4d983650b484ebe9607707003fef5e09c6Andrew Forrest .append(keyType).append("</")
6202ba4d983650b484ebe9607707003fef5e09c6Andrew Forrest .append(STSConstants.WST_PREFIX)
6202ba4d983650b484ebe9607707003fef5e09c6Andrew Forrest .append(KEY_TYPE).append(">");
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest }
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest sb.append("</").append(STSConstants.WST_PREFIX)
ea65b6bac936db133a660beef9d75f044dd7ce84Andrew Forrest .append(REQUEST_SECURITY_TOKEN).append(">");
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest return sb.toString();
48dc7c385d64e5d3906d6979100f106e857b4c02Andrew Forrest }
6202ba4d983650b484ebe9607707003fef5e09c6Andrew Forrest
6202ba4d983650b484ebe9607707003fef5e09c6Andrew Forrest}
6202ba4d983650b484ebe9607707003fef5e09c6Andrew Forrest