1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.security.trust;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNull;
20
21 import java.util.List;
22
23 import javax.xml.parsers.DocumentBuilder;
24 import javax.xml.parsers.DocumentBuilderFactory;
25
26 import org.junit.Test;
27 import org.kuali.student.common.test.spring.AbstractServiceTest;
28 import org.kuali.student.common.test.spring.Client;
29 import org.kuali.student.security.exceptions.KSSecurityException;
30 import org.kuali.student.security.trust.dto.RequestSecurityTokenResponseCollectionType;
31 import org.kuali.student.security.trust.dto.RequestSecurityTokenResponseType;
32 import org.kuali.student.security.trust.dto.RequestSecurityTokenType;
33 import org.kuali.student.security.trust.service.SecurityTokenService;
34 import org.kuali.student.security.trust.service.SecurityTokenServiceImpl;
35 import org.w3c.dom.Document;
36 import org.w3c.dom.Element;
37
38
39 public class TestSecurityTokenService extends AbstractServiceTest {
40
41 @Client(value="org.kuali.student.security.trust.service.SecurityTokenServiceImpl",additionalContextFile="classpath:test-sts-context.xml")
42 public SecurityTokenService client;
43
44 @Test
45 public void requestSecurityToken() throws KSSecurityException {
46 RequestSecurityTokenType rst = new RequestSecurityTokenType();
47 rst.setContext("Optional URI specifies this request identifier");
48
49
50 Element tokenType = createElement("TokenType");
51 tokenType.setTextContent("urn:oasis:names:tc:SAML:1.0:assertion");
52 rst.getAny().add(tokenType);
53
54
55 Element requestType = createElement("RequestType");
56 requestType.setTextContent("/Issue");
57 rst.getAny().add(requestType);
58
59
60 Element casProxyTicket = createElement("CasProxyTicket");
61 casProxyTicket.setTextContent("ST-4-ZlNqDu6oukc1NcizlAbL-cas");
62 rst.getAny().add(casProxyTicket);
63
64
65 Element casProxyTargetService = createElement("CasProxyTargetService");
66 casProxyTargetService.setTextContent("http://localhost:18080/bar");
67 rst.getAny().add(casProxyTargetService);
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 }
97
98 @Test
99 public void requestSecurityToken2() throws KSSecurityException {
100 RequestSecurityTokenResponseCollectionType rstr = client.requestSecurityToken2(null);
101 assertNull(rstr);
102 }
103
104
105 @Test
106 public void requestSecurityTokenNonSoap() throws KSSecurityException {
107 SecurityTokenService client = new SecurityTokenServiceImpl();
108
109 RequestSecurityTokenType rst = new RequestSecurityTokenType();
110 rst.setContext("Optional URI specifies this request identifier");
111
112
113 Element tokenType = createElement("TokenType");
114 tokenType.setTextContent("urn:oasis:names:tc:SAML:1.0:assertion");
115 rst.getAny().add(tokenType);
116
117
118 Element requestType = createElement("RequestType");
119 requestType.setTextContent("/Issue");
120 rst.getAny().add(requestType);
121
122
123 Element casProxyTicket = createElement("CasProxyTicket");
124 casProxyTicket.setTextContent("ST-4-ZlNqDu6oukc1NcizlAbL-cas");
125 rst.getAny().add(casProxyTicket);
126
127
128 Element casProxyTargetService = createElement("CasProxyTargetService");
129 casProxyTargetService.setTextContent("http://localhost:18080/bar");
130 rst.getAny().add(casProxyTargetService);
131
132 RequestSecurityTokenResponseType rstr = client.requestSecurityToken(rst);
133
134
135 assertEquals("Optional URI specifies this request identifier", rstr.getContext());
136
137 List<Object> objects = rstr.getAny();
138 for(Object o : objects){
139 if(o instanceof Element){
140 Element e = (Element)o;
141 if( e.getLocalName().equalsIgnoreCase("TokenType")){
142 assertEquals("urn:oasis:names:tc:SAML:1.0:assertion", (String)e.getTextContent());
143 }
144 else if(e.getLocalName().equalsIgnoreCase("RequestedSecurityToken")) {
145 Element ksSecureToken = (Element)e.getFirstChild();
146 Element saml = (Element)ksSecureToken.getFirstChild();
147 assertEquals("Assertion", saml.getLocalName());
148 }
149 }
150 }
151 }
152
153 private Element createElement(String tagName){
154
155 Element element = null;
156 try{
157 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
158
159 DocumentBuilder db = dbf.newDocumentBuilder();
160 Document doc = db.newDocument();
161
162 element = doc.createElementNS("http://schemas.xmlsoap.org/ws/2005/02/trust", tagName);
163
164 } catch(Exception e){
165 e.printStackTrace();
166 }
167 return element;
168 }
169 }