KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > interceptor > authenticator > UserPasswordAuthenticator


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * UserPasswordAuthenticator.java
20  *
21  * The user password authenticator object.
22  */

23
24 // package imports
25
package com.rift.coad.lib.interceptor.authenticator;
26
27 // logging import
28
import org.apache.log4j.Logger;
29
30 // coadunation imports
31
import com.rift.coad.lib.interceptor.InterceptorAuthenticator;
32 import com.rift.coad.lib.interceptor.InterceptorException;
33 import com.rift.coad.lib.interceptor.credentials.Credential;
34 import com.rift.coad.lib.interceptor.credentials.Login;
35 import com.rift.coad.lib.security.AuthorizationException;
36 import com.rift.coad.lib.security.UserSession;
37 import com.rift.coad.lib.security.login.SessionLogin;
38 import com.rift.coad.lib.security.login.handlers.PasswordInfoHandler;
39
40 /**
41  * The user password authenticator object.
42  *
43  * @author Brett Chaldecott
44  */

45 public class UserPasswordAuthenticator implements InterceptorAuthenticator {
46     
47     // the class log variable
48
protected Logger log =
49             Logger.getLogger(UserPasswordAuthenticator.class.getName());
50     
51     
52     /** Creates a new instance of UserPasswordAuthenticator */
53     public UserPasswordAuthenticator() {
54     }
55     
56     /**
57      * This method returns a valid user session object for the supplied
58      * credentials
59      *
60      * @return UserSession The new user session for the given credentials.
61      * @param credentials The credentials for the user session.
62      * @exception InterceptorException
63      * @exception AuthorizationException
64      */

65     public UserSession authenticate(Credential credential) throws
66             InterceptorException, AuthorizationException {
67         if (!(credential instanceof Login)) {
68             throw new InterceptorException(
69                     "The User Password Authenticator is only capable of " +
70                     "dealling with login credentials.");
71         }
72         try {
73             Login loginInfo = (Login)credential;
74             SessionLogin sessionLogin = new SessionLogin(
75                     new PasswordInfoHandler(loginInfo.getUsername(),
76                     loginInfo.getPassword()));
77             
78             sessionLogin.login();
79             
80             return sessionLogin.getUser();
81         } catch (Exception JavaDoc ex) {
82             log.error("Failed to authenticate the user because : " +
83                     ex.getMessage(),ex);
84             throw new InterceptorException(
85                     "Failed to authenticate the user because : " +
86                     ex.getMessage(),ex);
87         }
88     }
89     
90     
91 }
92
Popular Tags