1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package uk.ac.soton.itinnovation.components.security;
30
31 import java.io.StringReader;
32 import java.util.HashMap;
33
34 import javax.xml.namespace.QName;
35 import javax.xml.parsers.DocumentBuilder;
36 import javax.xml.parsers.DocumentBuilderFactory;
37 import javax.xml.soap.SOAPElement;
38
39 import nextgrid.api.env.STSFailureException;
40 import nextgrid.api.env.STSModule;
41
42 import org.apache.axis.message.MessageElement;
43 import org.apache.axis.message.SOAPHeaderElement;
44 import org.apache.axis.message.addressing.EndpointReference;
45 import org.apache.commons.logging.Log;
46 import org.apache.commons.logging.LogFactory;
47 import org.w3c.dom.Document;
48 import org.w3c.dom.Element;
49 import org.xml.sax.InputSource;
50
51 import uk.ac.soton.itinnovation.utils.EPRUtils;
52 import uk.ac.soton.itinnovation.wsdlutils.WSDLOperation;
53 import uk.ac.soton.itinnovation.wsdlutils.WSDLService;
54
55
56
57
58
59 public class WSTrustModule implements STSModule {
60
61
62
63
64 private static final Log LOG = LogFactory.getLog(WSTrustModule.class);
65
66
67
68
69 private static final String WSTRUST_OPERATION = "RequestSecurityToken";
70
71
72
73
74 private static final String WSTRUST_NS = "http://schemas.xmlsoap.org/ws/2005/02/trust";
75
76
77
78
79 public static final String WSTRUST_CONFIG_FILE = "sts-client-config.wsdd";
80
81
82
83
84 public static final String WSTRUST_TOKEN_TYPE
85 = "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1";
86
87
88
89
90 public static final String NO_TOKEN = "http://localhost/nosecurity";
91
92
93
94
95 private static final String WSTRUST_REQUEST_TYPE
96 = "http://schemas.xmlsoap.org/ws/2005/02/trust/Issue";
97
98
99
100
101 private static final String REQUEST_SEC_TOKEN = "<?xml version='1.0'?>\n"
102 + "<RequestSecurityToken xmlns='" + WSTRUST_NS + "'>"
103
104 + "<TokenType>" + WSTRUST_TOKEN_TYPE + "</TokenType>"
105
106 + "<RequestType>" + WSTRUST_REQUEST_TYPE + "</RequestType>"
107 + "</RequestSecurityToken>";
108
109
110
111
112 private HashMap<String, Element> securityTokens;
113
114
115
116
117 private String strServiceGroupEPR;
118
119
120
121
122 private EndpointReference serviceGroupEPR;
123
124
125
126
127 private String uri;
128
129
130
131
132
133 public WSTrustModule() {
134 securityTokens = new HashMap<String, Element>();
135 }
136
137
138
139
140 public void setMembershipGroup(String epr) {
141 strServiceGroupEPR = epr;
142
143 serviceGroupEPR = new EndpointReference(
144 EPRUtils.decodeAndDeserialise(strServiceGroupEPR));
145
146
147 uri = serviceGroupEPR.getAddress().getValue().toString();
148 }
149
150
151
152
153 public Element requestSecurityToken() throws STSFailureException {
154 WSDLOperation op = null;
155 Element token = null;
156
157
158
159 if (strServiceGroupEPR != null) {
160 token = lookup(strServiceGroupEPR);
161 } else {
162 throw new STSFailureException("No Group Reference Found");
163 }
164
165 if (token != null) {
166 return token;
167 }
168
169
170 try {
171
172
173 MessageElement me = (MessageElement)serviceGroupEPR.getParameters().get(0);
174 QName serviceParameterName = me.getQName();
175 SOAPHeaderElement param = new SOAPHeaderElement(serviceParameterName);
176
177 param.addTextNode(me.getValue());
178
179 WSDLService sts = new WSDLService(new java.net.URI(uri), WSTRUST_CONFIG_FILE);
180
181 Document doc = createDocument(REQUEST_SEC_TOKEN);
182
183 op = sts.getOperation(WSTRUST_OPERATION);
184 op.addHeader(param);
185 op.setOperationStyle(org.apache.axis.constants.Style.DOCUMENT);
186 op.setRequestDocument(doc);
187
188 op.invoke();
189 } catch (IllegalArgumentException ex) {
190 throw new STSFailureException(ex.getMessage());
191 } catch (Exception e) {
192 throw new STSFailureException(e.getMessage());
193 }
194
195
196 java.util.Iterator<?> out = op.getResponse();
197 SOAPElement outputValue = null;
198 while (out.hasNext()) {
199 outputValue = (SOAPElement)out.next();
200 }
201 if (outputValue == null) {
202 throw new STSFailureException("Value of security token response is null");
203 }
204
205 token = (Element)outputValue.getFirstChild();
206 LOG.debug("The token: " + token);
207 this.securityTokens.put(strServiceGroupEPR, token);
208 return token;
209 }
210
211
212
213
214
215
216
217
218 public Element lookup(String ref) {
219 return securityTokens.get(ref);
220 }
221
222
223
224
225
226
227
228
229 private static Document createDocument(String xmlString) throws Exception {
230 Document doc;
231 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
232 factory.setNamespaceAware(true);
233 DocumentBuilder docBuilder = factory.newDocumentBuilder();
234 return docBuilder.parse(new InputSource(new StringReader(xmlString)));
235 }
236 }