001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020:
021: package com.sun.xml.wss.saml.util;
022:
023: import com.sun.xml.ws.security.opt.impl.util.StreamUtil;
024: import com.sun.xml.wss.impl.SecurableSoapMessage;
025: import com.sun.xml.wss.logging.LogDomainConstants;
026:
027: import com.sun.xml.wss.XWSSecurityException;
028: import com.sun.xml.wss.impl.MessageConstants;
029: import java.util.logging.Level;
030: import java.util.logging.Logger;
031: import javax.xml.bind.Marshaller;
032: import javax.xml.parsers.DocumentBuilder;
033: import javax.xml.parsers.DocumentBuilderFactory;
034: import javax.xml.transform.dom.DOMResult;
035:
036: import org.w3c.dom.Document;
037: import org.w3c.dom.Element;
038: import org.w3c.dom.Node;
039: import org.w3c.dom.NodeList;
040:
041: import javax.xml.bind.JAXBContext;
042: import javax.xml.stream.XMLOutputFactory;
043: import javax.xml.stream.XMLStreamException;
044: import javax.xml.stream.XMLStreamReader;
045: import javax.xml.stream.XMLStreamWriter;
046:
047: public class SAMLUtil {
048: private static Logger logger = Logger.getLogger(
049: LogDomainConstants.SAML_API_DOMAIN,
050: LogDomainConstants.SAML_API_DOMAIN_BUNDLE);
051:
052: public static Element locateSamlAssertion(String assertionId,
053: Document soapDocument) throws XWSSecurityException {
054:
055: //System.out.println("\n\n--------SOAP DOCUMENT : " + soapDocument + "--------\n\n");
056:
057: NodeList nodeList = null;
058:
059: // try {
060: nodeList = soapDocument.getElementsByTagNameNS(
061: MessageConstants.SAML_v1_0_NS,
062: MessageConstants.SAML_ASSERTION_LNAME);
063: if ((nodeList.item(0)) == null) {
064: nodeList = soapDocument.getElementsByTagNameNS(
065: MessageConstants.SAML_v2_0_NS,
066: MessageConstants.SAML_ASSERTION_LNAME);
067: }
068:
069: int nodeListLength = nodeList.getLength();
070: if (nodeListLength == 0) {
071: logger.log(Level.SEVERE, "WSS001.SAML_ASSERTION_NOT_FOUND",
072: new Object[] { assertionId });
073: throw SecurableSoapMessage.newSOAPFaultException(
074: MessageConstants.WSSE_SECURITY_TOKEN_UNAVAILABLE,
075: "Referenced Security Token could not be retrieved",
076: null);
077: //throw new XWSSecurityException(
078: //"No SAML Assertion found with AssertionID:" + assertionId );
079: }
080:
081: for (int i = 0; i < nodeListLength; i++) {
082: Element assertion = (Element) nodeList.item(i);
083: String aId = assertion
084: .getAttribute(MessageConstants.SAML_ASSERTIONID_LNAME);
085: String id = assertion
086: .getAttribute(MessageConstants.SAML_ID_LNAME);
087: if (aId.equals(assertionId) || id.equals(assertionId)) {
088: //return XMLUtil.convertToSoapElement(soapDocument, assertion);
089: return assertion;
090: }
091: }
092: logger.log(Level.SEVERE, "WSS001.SAML_ASSERTION_NOT_FOUND",
093: new Object[] { assertionId });
094: throw SecurableSoapMessage.newSOAPFaultException(
095: MessageConstants.WSSE_SECURITY_TOKEN_UNAVAILABLE,
096: "Referenced Security Token could not be retrieved",
097: null);
098: //throw new XWSSecurityException("Could not locate SAML assertion with AssertionId:" + assertionId);
099: }
100:
101: public static Element toElement(Node doc, Object element)
102: throws XWSSecurityException {
103:
104: DOMResult result = null;
105: Document document = null;
106: //TODO : If DOC is SUPPLIED then this code is not working
107: if (doc != null) {
108:
109: result = new DOMResult(doc);
110: } else {
111:
112: try {
113: DocumentBuilderFactory factory = DocumentBuilderFactory
114: .newInstance();
115: DocumentBuilder builder = factory.newDocumentBuilder();
116: document = builder.newDocument();
117: } catch (Exception ex) {
118: logger.log(Level.SEVERE,
119: "WSS002.failed.create.document", ex);
120: throw new XWSSecurityException(
121: "Unable to create Document : "
122: + ex.getMessage());
123: }
124: result = new DOMResult(document);
125: }
126:
127: try {
128: JAXBContext jc = null;
129:
130: if (System.getProperty("com.sun.xml.wss.saml.binding.jaxb") == null) {
131: if (element instanceof com.sun.xml.wss.saml.assertion.saml20.jaxb20.Assertion) {
132: jc = SAML20JAXBUtil.getJAXBContext();
133: } else {
134: jc = SAMLJAXBUtil.getJAXBContext();
135: }
136: } else {
137: jc = SAMLJAXBUtil.getJAXBContext();
138: }
139: Marshaller m = jc.createMarshaller();
140:
141: if (element == null) {
142: if (logger.isLoggable(Level.FINE)) {
143: logger.log(Level.FINE,
144: "Element is Null in SAMLUtil.toElement()");
145: }
146: }
147:
148: m.setProperty("com.sun.xml.bind.namespacePrefixMapper",
149: new WSSNamespacePrefixMapper());
150: m.marshal(element, result);
151:
152: } catch (Exception ex) {
153: logger.log(Level.SEVERE, "WSS003.failedto.marshal", ex);
154: throw new XWSSecurityException("Not able to Marshal "
155: + element.getClass().getName()
156: + ", got exception: " + ex.getMessage());
157: }
158:
159: if (doc != null) {
160: //return ((Document)doc).getDocumentElement();
161:
162: if (doc.getNodeType() == Node.ELEMENT_NODE) {
163: if (doc.getFirstChild().getNamespaceURI().equals(
164: MessageConstants.SAML_v2_0_NS)) {
165: Element el = (Element) ((Element) doc)
166: .getElementsByTagNameNS(
167: MessageConstants.SAML_v2_0_NS,
168: "Assertion").item(0);
169: return el;
170: } else {
171: Element el = (Element) ((Element) doc)
172: .getElementsByTagNameNS(
173: MessageConstants.SAML_v1_0_NS,
174: "Assertion").item(0);
175: return el;
176: }
177: } else {
178: if (doc.getFirstChild().getNamespaceURI().equals(
179: MessageConstants.SAML_v2_0_NS)) {
180: Element el = (Element) ((Document) doc)
181: .getElementsByTagNameNS(
182: MessageConstants.SAML_v2_0_NS,
183: "Assertion").item(0);
184: return el;
185: } else {
186: Element el = (Element) ((Document) doc)
187: .getElementsByTagNameNS(
188: MessageConstants.SAML_v1_0_NS,
189: "Assertion").item(0);
190: return el;
191: }
192: }
193:
194: } else {
195: if (document.getFirstChild().getNamespaceURI().equals(
196: MessageConstants.SAML_v2_0_NS)) {
197: Element el = (Element) document.getElementsByTagNameNS(
198: MessageConstants.SAML_v2_0_NS, "Assertion")
199: .item(0);
200: return el;
201: } else {
202: Element el = (Element) document.getElementsByTagNameNS(
203: MessageConstants.SAML_v1_0_NS, "Assertion")
204: .item(0);
205: return el;
206: }
207: }
208: }
209:
210: public static Element createSAMLAssertion(XMLStreamReader reader)
211: throws XWSSecurityException, XMLStreamException {
212: XMLOutputFactory xof = XMLOutputFactory.newInstance();
213: Document doc = null;
214: try {
215: DocumentBuilderFactory dbf = DocumentBuilderFactory
216: .newInstance();
217: DocumentBuilder db = dbf.newDocumentBuilder();
218: doc = db.newDocument();
219: XMLStreamWriter writer = xof
220: .createXMLStreamWriter(new DOMResult(doc));
221: while (!(XMLStreamReader.END_DOCUMENT == reader
222: .getEventType())) {
223: com.sun.xml.ws.security.opt.impl.util.StreamUtil
224: .writeCurrentEvent(reader, writer);
225: reader.next();
226: }
227: return doc.getDocumentElement();
228: } catch (XMLStreamException xe) {
229: throw new XMLStreamException(
230: "Error occurred while trying to convert SAMLAssertion stream into DOM Element",
231: xe);
232: } catch (Exception xe) {
233: throw new XWSSecurityException(
234: "Error occurred while trying to convert SAMLAssertion stream into DOM Element",
235: xe);
236: }
237: }
238: }
|