View Javadoc

1   /* Copyright 2004 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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      //~ Static fields/initializers =============================================
45  
46      private static final Log logger = LogFactory.getLog(CasPasswordHandler.class);
47  
48      //~ Instance fields ========================================================
49  
50      private AuthenticationManager authenticationManager;
51  
52      //~ Methods ================================================================
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 }