View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
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  	    // "TokenType"
50  	    Element tokenType = createElement("TokenType");
51  	    tokenType.setTextContent("urn:oasis:names:tc:SAML:1.0:assertion");
52  	    rst.getAny().add(tokenType);
53  	    
54  	    // "RequestType"
55          Element requestType = createElement("RequestType");
56          requestType.setTextContent("/Issue");
57          rst.getAny().add(requestType);
58  	    
59          // "CasProxyTicket"
60          Element casProxyTicket = createElement("CasProxyTicket");
61          casProxyTicket.setTextContent("ST-4-ZlNqDu6oukc1NcizlAbL-cas");
62          rst.getAny().add(casProxyTicket);
63          
64          // "CasProxyTargetService"
65          Element casProxyTargetService = createElement("CasProxyTargetService");
66          casProxyTargetService.setTextContent("http://localhost:18080/bar");
67          rst.getAny().add(casProxyTargetService);
68          
69          /* 
70           * This test has been disbled on purpose, there is error when running it with mvn test.
71           * If its run as a Junit test without maven it works. Its Jaxb binding error and it has
72           * something to do with the SAML Assertion, I think changing to SAML 2 might solve this.
73           * */
74          
75          //RequestSecurityTokenResponseType rstr = client.requestSecurityToken(rst);
76          
77          // Assert
78          /*assertEquals("Optional URI specifies this request identifier", rstr.getContext());
79          
80          List<Object> objects = rstr.getAny();
81          for(Object o : objects){
82              if(o instanceof JAXBElement){
83                  JAXBElement<?> e = (JAXBElement<?>)o;
84                  
85                  if( e.getName().getLocalPart().equalsIgnoreCase("TokenType")){
86                      assertEquals("urn:oasis:names:tc:SAML:1.0:assertion", (String)e.getValue());
87                  }
88                  else if(e.getName().getLocalPart().equalsIgnoreCase("RequestedSecurityToken")) {
89                      RequestedSecurityTokenType requestedToken = (RequestedSecurityTokenType)e.getValue();
90                      Element ksSecureToken = (Element)requestedToken.getAny();
91                      Element saml = (Element)ksSecureToken.getFirstChild();
92                      assertEquals("Assertion", saml.getLocalName());
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     // Test the filthy impl client, no SOAP.
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         // "TokenType"
113         Element tokenType = createElement("TokenType");
114         tokenType.setTextContent("urn:oasis:names:tc:SAML:1.0:assertion");
115         rst.getAny().add(tokenType);
116         
117         // "RequestType"
118         Element requestType = createElement("RequestType");
119         requestType.setTextContent("/Issue");
120         rst.getAny().add(requestType);
121         
122         // "CasProxyTicket"
123         Element casProxyTicket = createElement("CasProxyTicket");
124         casProxyTicket.setTextContent("ST-4-ZlNqDu6oukc1NcizlAbL-cas");
125         rst.getAny().add(casProxyTicket);
126         
127         // "CasProxyTargetService"
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         // Assert
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 }