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