1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.ws.security.common;
21
22 import java.io.IOException;
23
24 import javax.security.auth.callback.Callback;
25 import javax.security.auth.callback.UnsupportedCallbackException;
26 import javax.xml.parsers.DocumentBuilderFactory;
27
28 import org.apache.ws.security.saml.ext.AssertionWrapper;
29 import org.apache.ws.security.saml.ext.SAMLCallback;
30 import org.apache.ws.security.saml.ext.SAMLParms;
31 import org.apache.ws.security.saml.ext.builder.SAML1Constants;
32 import org.w3c.dom.Element;
33
34
35
36
37
38
39 public class SAMLElementCallbackHandler extends AbstractSAMLCallbackHandler {
40
41 public SAMLElementCallbackHandler() {
42 subjectName = "uid=joe,ou=people,ou=saml-demo,o=example.com";
43 subjectQualifier = "www.example.com";
44 confirmationMethod = SAML1Constants.CONF_SENDER_VOUCHES;
45 }
46
47 public void handle(Callback[] callbacks)
48 throws IOException, UnsupportedCallbackException {
49 for (int i = 0; i < callbacks.length; i++) {
50 if (callbacks[i] instanceof SAMLCallback) {
51 SAMLCallback callback = (SAMLCallback) callbacks[i];
52 Element assertionElement;
53 try {
54 assertionElement = getSAMLAssertion();
55 } catch (Exception e) {
56 throw new IOException(e.getMessage());
57 }
58 callback.setAssertionElement(assertionElement);
59
60 } else {
61 throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
62 }
63 }
64 }
65
66
67
68
69
70 private Element getSAMLAssertion() throws Exception {
71 SAMLParms parms = new SAMLParms();
72 SAML1CallbackHandler callbackHandler = new SAML1CallbackHandler();
73 callbackHandler.setIssuer(issuer);
74 parms.setCallbackHandler(callbackHandler);
75 AssertionWrapper assertionWrapper = new AssertionWrapper(parms);
76
77 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
78 return assertionWrapper.toDOM(factory.newDocumentBuilder().newDocument());
79 }
80
81 }