1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.adapters.cas;
17
18 import org.acegisecurity.Authentication;
19 import org.acegisecurity.AuthenticationException;
20 import org.acegisecurity.AuthenticationManager;
21 import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import org.springframework.beans.factory.InitializingBean;
27
28 import javax.servlet.ServletRequest;
29
30
31 /***
32 * Provides actual CAS authentication by delegation to an
33 * <code>AuthenticationManager</code>.
34 *
35 * <P>
36 * Do not use this class directly. Instead configure CAS to use the {@link
37 * CasPasswordHandlerProxy}.
38 * </p>
39 *
40 * @author Ben Alex
41 * @version $Id: CasPasswordHandler.java,v 1.2 2005/11/17 00:56:28 benalex Exp $
42 */
43 public final class CasPasswordHandler implements InitializingBean {
44
45
46 private static final Log logger = LogFactory.getLog(CasPasswordHandler.class);
47
48
49
50 private AuthenticationManager authenticationManager;
51
52
53
54 public void setAuthenticationManager(
55 AuthenticationManager authenticationManager) {
56 this.authenticationManager = authenticationManager;
57 }
58
59 public AuthenticationManager getAuthenticationManager() {
60 return authenticationManager;
61 }
62
63 public void afterPropertiesSet() throws Exception {
64 if (this.authenticationManager == null) {
65 throw new IllegalArgumentException(
66 "An AuthenticationManager is required");
67 }
68 }
69
70 /***
71 * Called by <code>CasPasswordHandlerProxy</code> for individual
72 * authentication requests.
73 *
74 * <P>
75 * Delegates to the configured <code>AuthenticationManager</code>.
76 * </p>
77 *
78 * @param servletRequest as provided by CAS
79 * @param username provided to CAS
80 * @param password provided to CAS
81 *
82 * @return whether authentication was successful or not
83 */
84 public boolean authenticate(ServletRequest servletRequest, String username,
85 String password) {
86 if ((username == null) || "".equals(username)) {
87 return false;
88 }
89
90 if (password == null) {
91 password = "";
92 }
93
94 Authentication request = new UsernamePasswordAuthenticationToken(username
95 .toString(), password.toString());
96 Authentication response = null;
97
98 try {
99 response = authenticationManager.authenticate(request);
100 } catch (AuthenticationException failed) {
101 if (logger.isDebugEnabled()) {
102 logger.debug("Authentication request for user: " + username
103 + " failed: " + failed.toString());
104 }
105
106 return false;
107 }
108
109 if (logger.isDebugEnabled()) {
110 logger.debug("Authentication request for user: " + username
111 + " successful");
112 }
113
114 return true;
115 }
116 }