KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > auth > LoginModule


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.jmanage.core.auth;
17
18 import javax.security.auth.Subject JavaDoc;
19 import javax.security.auth.login.LoginException JavaDoc;
20 import javax.security.auth.callback.*;
21 import java.util.Map JavaDoc;
22
23 /**
24  *
25  * date: Jun 27, 2004
26  * @author Rakesh Kalra, Shashank Bellary
27  */

28 public class LoginModule implements javax.security.auth.spi.LoginModule JavaDoc {
29
30     private Subject JavaDoc subject;
31     private CallbackHandler callbackHandler;
32     private boolean loginStatus;
33     private Map JavaDoc options;
34     private Map JavaDoc sharedState;
35     private User loggedInUser = null;
36
37     /**
38      * Initialize this LoginModule.
39      *
40      * <p> This method is called by the <code>LoginContext</code>
41      * after this <code>LoginModule</code> has been instantiated.
42      * The purpose of this method is to initialize this
43      * <code>LoginModule</code> with the relevant information.
44      * If this <code>LoginModule</code> does not understand
45      * any of the data stored in <code>sharedState</code> or
46      * <code>options</code> parameters, they can be ignored.
47      *
48      * <p>
49      *
50      * @param subject the <code>Subject</code> to be authenticated. <p>
51      *
52      * @param callbackHandler a <code>CallbackHandler</code> for communicating
53      * with the end user (prompting for usernames and
54      * passwords, for example). <p>
55      *
56      * @param sharedState state shared with other configured LoginModules. <p>
57      *
58      * @param options options specified in the login
59      * <code>Configuration</code> for this particular
60      * <code>LoginModule</code>.
61      */

62     public void initialize(Subject JavaDoc subject, CallbackHandler callbackHandler,
63                            Map JavaDoc sharedState, Map JavaDoc options) {
64         this.subject = subject;
65         this.callbackHandler = callbackHandler;
66         this.sharedState = sharedState;
67         this.options = options;
68         loginStatus = false;
69     }
70
71     /**
72      * Method to authenticate a <code>Subject</code> (phase 1).
73      *
74      * <p> The implementation of this method authenticates
75      * a <code>Subject</code>. For example, it may prompt for
76      * <code>Subject</code> information such
77      * as a username and password and then attempt to verify the password.
78      * This method saves the result of the authentication attempt
79      * as private state within the LoginModule.
80      *
81      * <p>
82      *
83      * @exception LoginException if the authentication fails
84      *
85      * @return true if the authentication succeeded, or false if this
86      * <code>LoginModule</code> should be ignored.
87      */

88     public boolean login() throws LoginException JavaDoc {
89         /* Authenticate user */
90         if (callbackHandler == null) {
91             throw new LoginException JavaDoc("No callback handler is available");
92         }
93         NameCallback nameCallback = new NameCallback("username");
94         PasswordCallback pwdCallback = new PasswordCallback("password", false);
95         Callback[] callbacks = new Callback[] {nameCallback, pwdCallback};
96
97         String JavaDoc username = null;
98         char[] password = null;
99         try {
100             callbackHandler.handle(callbacks);
101             username = nameCallback.getName();
102             password = pwdCallback.getPassword();
103         } catch (java.io.IOException JavaDoc ioe) {
104             throw new LoginException JavaDoc(ioe.toString());
105         } catch (UnsupportedCallbackException ce) {
106             throw new LoginException JavaDoc("Error: " + ce.getCallback().toString());
107         }
108         /* check username and password */
109         UserManager userManager = UserManager.getInstance();
110         loggedInUser = userManager.verifyUsernamePassword(username, password);
111         loginStatus = loggedInUser != null;
112         return loginStatus;
113     }
114
115     /**
116      * Method to commit the authentication process (phase 2).
117      *
118      * <p> This method is called if the LoginContext's
119      * overall authentication succeeded
120      * (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules
121      * succeeded).
122      *
123      * <p> If this LoginModule's own authentication attempt
124      * succeeded (checked by retrieving the private state saved by the
125      * <code>login</code> method), then this method associates relevant
126      * Principals and Credentials with the <code>Subject</code> located in the
127      * <code>LoginModule</code>. If this LoginModule's own
128      * authentication attempted failed, then this method removes/destroys
129      * any state that was originally saved.
130      *
131      * <p>
132      *
133      * @exception LoginException if the commit fails
134      *
135      * @return true if this method succeeded, or false if this
136      * <code>LoginModule</code> should be ignored.
137      */

138     public boolean commit() throws LoginException JavaDoc {
139         if(loginStatus){
140             subject.getPrincipals().add(loggedInUser);
141             return true;
142         }
143         return false;
144     }
145
146     /**
147      * Method to abort the authentication process (phase 2).
148      *
149      * <p> This method is called if the LoginContext's
150      * overall authentication failed.
151      * (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules
152      * did not succeed).
153      *
154      * <p> If this LoginModule's own authentication attempt
155      * succeeded (checked by retrieving the private state saved by the
156      * <code>login</code> method), then this method cleans up any state
157      * that was originally saved.
158      *
159      * <p>
160      *
161      * @exception LoginException if the abort fails
162      *
163      * @return true if this method succeeded, or false if this
164      * <code>LoginModule</code> should be ignored.
165      */

166     public boolean abort() throws LoginException JavaDoc {
167         return true;
168     }
169
170     /**
171      * Method which logs out a <code>Subject</code>.
172      *
173      * <p>An implementation of this method might remove/destroy a Subject's
174      * Principals and Credentials.
175      *
176      * <p>
177      *
178      * @exception LoginException if the logout fails
179      *
180      * @return true if this method succeeded, or false if this
181      * <code>LoginModule</code> should be ignored.
182      */

183     public boolean logout() throws LoginException JavaDoc {
184         subject = null;
185         loginStatus = false;
186         return true;
187     }
188 }
Popular Tags