View Javadoc

1   /*
2    * The MIT License
3    *
4    * Original work sponsored and donated by National Board of e-Health (NSI), Denmark (http://www.nsi.dk)
5    *
6    * Copyright (C) 2011 National Board of e-Health (NSI), Denmark (http://www.nsi.dk)
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   *
26   * $HeadURL: https://svn.softwareborsen.dk/sosi/trunk/modules/seal/src/main/java/dk/sosi/seal/model/dombuilders/WSTrustUtil.java $
27   * $Id: WSTrustUtil.java 8697 2011-09-02 10:33:55Z chg@lakeside.dk $
28   */
29  package dk.sosi.seal.model.dombuilders;
30  
31  import dk.sosi.seal.model.IDCard;
32  import dk.sosi.seal.model.ModelException;
33  import dk.sosi.seal.model.constants.NameSpaces;
34  import org.w3c.dom.Document;
35  import org.w3c.dom.Element;
36  
37  /**
38   * Utility method for handling WSTrust.
39   *
40   * @author Peter Buus
41   * @author $LastChangedBy: chg@lakeside.dk $
42   * @since 1.0
43   */
44  public class WSTrustUtil { // NOPMD
45  
46  	public static final String VERSION = "2.0";
47  	private static final String urn = "urn:oasis:names:tc:SAML:2.0:assertion:";
48  	private static final String Issue = "http://schemas.xmlsoap.org/ws/2005/02/trust/Issue";
49  	private static final String Valid = "http://schemas.xmlsoap.org/ws/2005/02/trust/status/valid";
50  
51  	public Element createSecurityTokenRequest(Document document, IDCard idCard) {
52  
53  		if (idCard == null) throw new ModelException("No IDCard present in SecurityTokenRequest");
54  
55  		Element elmWsTrust = document.createElementNS(NameSpaces.WST_SCHEMA, NameSpaces.NS_WST + ":RequestSecurityToken");
56  		elmWsTrust.setAttribute("Context", "www.sosi.dk");
57  
58  		Element elmRequestType = document.createElementNS(NameSpaces.WST_SCHEMA, NameSpaces.NS_WST + ":TokenType");
59  		elmRequestType.appendChild(document.createTextNode(urn));
60  		elmWsTrust.appendChild(elmRequestType);
61  
62  		Element elmTokenType = document.createElementNS(NameSpaces.WST_SCHEMA, NameSpaces.NS_WST + ":RequestType");
63  		elmTokenType.appendChild(document.createTextNode(Issue));
64  		elmWsTrust.appendChild(elmTokenType);
65  
66  
67  		Element elmClaims = document.createElementNS(NameSpaces.WST_SCHEMA, NameSpaces.NS_WST + ":Claims");
68  		//TODO fixme sosifactory instead of null
69          elmClaims.appendChild(idCard.serialize2DOMDocument(null, document));
70  		elmWsTrust.appendChild(elmClaims);
71  
72  		Element elmIssuer = document.createElementNS(NameSpaces.WST_SCHEMA, NameSpaces.NS_WST + ":Issuer");
73  		Element elmAddress = document.createElementNS(NameSpaces.WSA_SCHEMA, NameSpaces.NS_WSA + ":Address");
74  		elmAddress.appendChild(document.createTextNode(idCard.getIssuer()));
75  		elmIssuer.appendChild(elmAddress);
76  		elmWsTrust.appendChild(elmIssuer);
77  
78  		return elmWsTrust;
79  
80  	}
81  
82  	public Element createSecurityTokenResponse(Document document, IDCard idCard) {
83  
84  		if (idCard == null) throw new ModelException("No idCard present in SecurityTokenResponse");
85  
86  		Element elmWsTrust = document.createElementNS(NameSpaces.WST_SCHEMA, NameSpaces.NS_WST + ":RequestSecurityTokenResponse");
87  		elmWsTrust.setAttribute("Context", "www.sosi.dk");
88  
89  		Element elmTokenType = document.createElementNS(NameSpaces.WST_SCHEMA, NameSpaces.NS_WST + ":TokenType");
90  		elmTokenType.appendChild(document.createTextNode(urn));
91  		elmWsTrust.appendChild(elmTokenType);
92  
93  		Element elmRequestedSecurityTokens = document.createElementNS(NameSpaces.WST_SCHEMA, NameSpaces.NS_WST + ":RequestedSecurityToken");
94          //TODO fixme sosifactory instead of null
95  		elmRequestedSecurityTokens.appendChild(idCard.serialize2DOMDocument(null,document));
96  		elmWsTrust.appendChild(elmRequestedSecurityTokens);
97  
98  		Element elmStatus = document.createElementNS(NameSpaces.WST_SCHEMA, NameSpaces.NS_WST + ":Status");
99  		Element elmStatusCode = document.createElementNS(NameSpaces.WST_SCHEMA, NameSpaces.NS_WST + ":Code");
100 		elmStatusCode.appendChild(document.createTextNode(Valid));
101 		elmStatus.appendChild(elmStatusCode);
102 		elmWsTrust.appendChild(elmStatus);
103 
104 		Element elmIssuer = document.createElementNS(NameSpaces.WST_SCHEMA, NameSpaces.NS_WST + ":Issuer");
105 		Element elmAddress = document.createElementNS(NameSpaces.WSA_SCHEMA, NameSpaces.NS_WSA + ":Address");
106 		elmAddress.appendChild(document.createTextNode(idCard.getIssuer()));
107 		elmIssuer.appendChild(elmAddress);
108 		elmWsTrust.appendChild(elmIssuer);
109 
110 		return elmWsTrust;
111 
112 	}
113 
114 }