1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.client.acegi;
17
18 import org.acegisecurity.Authentication;
19 import org.acegisecurity.AuthenticationException;
20 import org.acegisecurity.BadCredentialsException;
21 import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
22 import org.acegisecurity.providers.cas.CasAuthenticationProvider;
23 import org.acegisecurity.providers.cas.CasAuthenticationToken;
24 import org.acegisecurity.providers.cas.StatelessTicketCache;
25 import org.acegisecurity.ui.cas.CasProcessingFilter;
26 import org.acegisecurity.userdetails.UserDetails;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class KualiCasAuthenticationProvider extends CasAuthenticationProvider {
44
45 private static final Log logger = LogFactory.getLog(KualiCasAuthenticationProvider.class);
46
47
48
49
50
51
52
53
54 public Authentication authenticate(Authentication authentication) throws AuthenticationException {
55 StatelessTicketCache statelessTicketCache = this.getStatelessTicketCache();
56 String key = this.getKey();
57 if (!supports(authentication.getClass())) {
58 return null;
59 }
60
61 if (authentication instanceof UsernamePasswordAuthenticationToken
62 && (!CasProcessingFilter.CAS_STATEFUL_IDENTIFIER.equals(authentication.getPrincipal().toString())
63 && !CasProcessingFilter.CAS_STATELESS_IDENTIFIER.equals(authentication.getPrincipal().toString()))) {
64
65 return null;
66 }
67
68
69 if (authentication instanceof CasAuthenticationToken) {
70 if (key.hashCode() == ((CasAuthenticationToken) authentication).getKeyHash()) {
71 return authentication;
72 } else {
73 throw new BadCredentialsException(messages.getMessage("CasAuthenticationProvider.incorrectKey",
74 "The presented CasAuthenticationToken does not contain the expected key"));
75 }
76 }
77
78
79 if ((authentication.getCredentials() == null) || "".equals(authentication.getCredentials())) {
80 throw new BadCredentialsException(messages.getMessage("CasAuthenticationProvider.noServiceTicket",
81 "Failed to provide a CAS service ticket to validate"));
82 }
83
84 boolean stateless = false;
85
86 if (authentication instanceof UsernamePasswordAuthenticationToken
87 && CasProcessingFilter.CAS_STATELESS_IDENTIFIER.equals(authentication.getPrincipal())) {
88 stateless = true;
89 }
90
91 CasAuthenticationToken result = null;
92
93 if (stateless) {
94
95 result = statelessTicketCache.getByTicketId(authentication.getCredentials().toString());
96 }
97
98 if (result == null) {
99 result = this.authenticateNow(authentication);
100 result.setDetails(authentication.getDetails());
101 }
102
103 if (stateless) {
104
105 statelessTicketCache.putTicketInCache(result);
106 }
107
108 return result;
109 }
110
111
112
113
114
115
116
117 private CasAuthenticationToken authenticateNow(Authentication authentication) throws AuthenticationException {
118
119 KualiTicketResponse response = (KualiTicketResponse)this.getTicketValidator().confirmTicketValid(authentication.getCredentials().toString());
120
121
122 this.getCasProxyDecider().confirmProxyListTrusted(response.getProxyList());
123 if (logger.isDebugEnabled()) {
124 logger.debug("authenticationNOW:" + response);
125 }
126
127 logger.debug("\n\npopulating authorities\n\n");
128 UserDetails userDetails = ((KualiCasAuthoritiesPopulator)this.getCasAuthoritiesPopulator()).getUserDetails(response);
129
130
131 return new CasAuthenticationToken(this.getKey(), userDetails, authentication.getCredentials(),
132 userDetails.getAuthorities(), userDetails, response.getProxyList(), response.getProxyGrantingTicketIou());
133 }
134 }