1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ksb.security.credentials;
17
18 import java.lang.reflect.Field;
19 import java.util.ArrayList;
20 import java.util.Map;
21
22 import junit.framework.TestCase;
23
24 import org.acegisecurity.GrantedAuthority;
25 import org.acegisecurity.GrantedAuthorityImpl;
26 import org.acegisecurity.context.SecurityContextHolder;
27 import org.acegisecurity.context.SecurityContextImpl;
28 import org.acegisecurity.providers.cas.CasAuthenticationToken;
29 import org.acegisecurity.ui.cas.CasProcessingFilter;
30 import org.acegisecurity.userdetails.User;
31 import org.kuali.rice.core.security.credentials.Credentials;
32 import org.kuali.rice.core.security.credentials.CredentialsSource.CredentialsType;
33 import org.kuali.rice.ksb.security.credentials.CasProxyTicketCredentialsSource;
34 import org.kuali.rice.ksb.security.credentials.UsernamePasswordCredentials;
35 import org.springframework.mock.web.MockHttpServletRequest;
36 import org.springframework.mock.web.MockServletConfig;
37
38 import edu.yale.its.tp.cas.proxy.ExtendedProxyGrantingTicket;
39 import edu.yale.its.tp.cas.proxy.ProxyTicketReceptor;
40
41
42
43
44
45
46
47 public class CasProxyTicketCredentialsSourceTest extends TestCase {
48
49 private CasProxyTicketCredentialsSource credentialsSource;
50
51 private ProxyTicketReceptor receptor = new ProxyTicketReceptor();
52
53 private final String proxyUrl = "https://localhost:8080/cas/proxy";
54
55 protected void setUp() throws Exception {
56 this.credentialsSource = new CasProxyTicketCredentialsSource();
57 final CasAuthenticationToken token = new CasAuthenticationToken("test", "cas_user", "ticketId", new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_USER")}, new User("cas_user", "password", true, true, true, true, new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_USER")}), new ArrayList<String>(), "PGT-IOU");
58 final SecurityContextImpl impl = new SecurityContextImpl();
59 impl.setAuthentication(token);
60
61 SecurityContextHolder.setContext(impl);
62
63 final MockHttpServletRequest request = new MockHttpServletRequest();
64 request.setParameter("pgtIou", "PGT-IOU");
65 request.setParameter("pgtId", "PGT-ID");
66
67 final MockServletConfig config = new MockServletConfig();
68 config.addInitParameter(ProxyTicketReceptor.CAS_PROXYURL_INIT_PARAM, proxyUrl);
69
70 this.receptor.init(config);
71
72 final ExtendedProxyGrantingTicket ticket = new ExtendedProxyGrantingTicket("test", "test");
73
74 final Field field = receptor.getClass().getDeclaredField("pgtMap");
75 field.setAccessible(true);
76 final Map map = (Map) field.get(receptor);
77
78 map.put("PGT-IOU", ticket);
79 }
80
81 public void testCredentialsType() {
82 assertEquals(CredentialsType.CAS, this.credentialsSource.getSupportedCredentialsType());
83 }
84
85 public void testGetterWithCasServerInstance() {
86 final Credentials c = this.credentialsSource.getCredentials("http://www.cnn.com");
87 assertNotNull(c);
88 assertTrue(c instanceof UsernamePasswordCredentials);
89 final UsernamePasswordCredentials upc = (UsernamePasswordCredentials) c;
90 assertEquals(CasProcessingFilter.CAS_STATELESS_IDENTIFIER, upc.getUsername());
91 assertEquals("PT", upc.getPassword());
92 }
93 }