KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > authentication > DefaultAuthenticator


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.ejb.authentication;
20
21 import java.rmi.*;
22 import java.rmi.server.*;
23 import java.util.*;
24 import javax.ejb.*;
25
26 import cowsultants.itracker.ejb.client.exceptions.*;
27 import cowsultants.itracker.ejb.client.interfaces.*;
28 import cowsultants.itracker.ejb.client.models.*;
29 import cowsultants.itracker.ejb.client.util.*;
30
31 /**
32   * This class provides a default authentication scheme for ITracker. It uses passwords
33   * in the user table provided by ITracker to authenticate users. This authenticator
34   * allows any user to self register if self registration is available in the system.
35   */

36 public class DefaultAuthenticator extends AbstractPluggableAuthenticator {
37
38     public DefaultAuthenticator() {
39     }
40
41     /**
42       * Checks the login of a user against the user profile provided in ITracker. This is
43       * the default authentication scheme provided by ITracker.
44       * @param login the login the user/client provided
45       * @param authentication the user's authentication information, if known
46       * @param authType the type of authentication information being provided
47       * @param reqSource the source of the request (eg web, api)
48       * @return a UserModel if the login is successful
49       * @throws AuthenticatorException an exception if the login is unsuccessful, or an error occurs
50       */

51     public UserModel checkLogin(String JavaDoc login, Object JavaDoc authentication, int authType, int reqSource) throws AuthenticatorException {
52         Logger.logDebug("Checking login for " + login + " using DefaultAuthenticator");
53
54         if(login != null && authentication != null && ! login.equals("")) {
55             UserModel user = getUserHandler().getUserByLogin(login);
56
57             if(user == null) {
58                 throw new AuthenticatorException(AuthenticatorException.UNKNOWN_USER);
59             }
60             if(user.getStatus() != UserUtilities.STATUS_ACTIVE) {
61                 throw new AuthenticatorException(AuthenticatorException.INACTIVE_ACCOUNT);
62             }
63
64             String JavaDoc userPassword = getUserHandler().getUserPasswordByLogin(login);
65             if(userPassword == null || userPassword.equals("")) {
66                 throw new AuthenticatorException(AuthenticatorException.INVALID_PASSWORD);
67             }
68
69             try {
70                 if(! userPassword.endsWith("=")) {
71                     Logger.logInfo("User " + login + " has old style password. Converting to SHA1 hash.");
72                     try {
73                         user.setPassword(UserUtilities.encryptPassword(userPassword));
74                         getUserHandler().updateUser(user);
75                     } catch(UserException ue) {
76                         Logger.logInfo("User password conversion failed for user " + login);
77                     }
78                 }
79
80                 if(authType == AUTH_TYPE_PASSWORD_PLAIN) {
81                     if(! userPassword.equals(UserUtilities.encryptPassword((String JavaDoc) authentication))) {
82                         throw new AuthenticatorException(AuthenticatorException.INVALID_PASSWORD);
83                     }
84                 } else if(authType == AUTH_TYPE_PASSWORD_ENC) {
85                     if(! userPassword.equals((String JavaDoc) authentication)) {
86                         throw new AuthenticatorException(AuthenticatorException.INVALID_PASSWORD);
87                     }
88                 } else {
89                     throw new AuthenticatorException(AuthenticatorException.INVALID_AUTHENTICATION_TYPE);
90                 }
91             } catch(ClassCastException JavaDoc cce) {
92                 Logger.logDebug("Authenticator was of wrong type.", cce);
93                 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
94             } catch(PasswordException pe) {
95                 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
96             }
97
98             return user;
99         }
100
101         throw new AuthenticatorException(AuthenticatorException.INVALID_DATA);
102     }
103
104     /**
105       * The DefaultAuthenticator returns a list of user permissions from the database.
106       * @param user a UserModel object that contains the user to retrieve permissions for
107       * @param reqSource the source of the request (eg web, api)
108       * @return an array of PermissionModels
109       * @throws AuthenticatorException an error occurs
110       */

111     public PermissionModel[] getUserPermissions(UserModel user, int reqSource) throws AuthenticatorException {
112         if(user == null || user.getId() == null) {
113             throw new AuthenticatorException(AuthenticatorException.INVALID_DATA);
114         }
115
116         PermissionModel[] permissionArray = new PermissionModel[0];
117         permissionArray = getUserHandler().getUserPermissionsLocal(user);
118
119         if(user.isSuperUser()) {
120             PermissionModel[] augmentedPermissions = new PermissionModel[permissionArray.length + 1];
121             augmentedPermissions[0] = new PermissionModel(new Integer JavaDoc(-1), -1, user.getLogin(), user.getId());
122             System.arraycopy(permissionArray, 0, augmentedPermissions, 1, permissionArray.length);
123             permissionArray = augmentedPermissions;
124         }
125
126         return permissionArray;
127     }
128
129     public UserModel[] getUsersWithProjectPermission(PermissionModel[] permissions, boolean requireAll, boolean activeOnly, int reqSource) throws AuthenticatorException {
130         UserModel[] userArray = new UserModel[0];
131
132         try {
133             HashMap userMap = new HashMap();
134
135             for(int i = 0; i < permissions.length; i++) {
136                 UserModel[] explicitUsers = getUserHandler().getUsersWithPermissionLocal(permissions[i]);
137                 if(! requireAll || permissions.length == 1) {
138                     for(int j = 0; j < explicitUsers.length; j++) {
139                         userMap.put(explicitUsers[j].getId(), explicitUsers[j]);
140                     }
141                 } else {
142                     if(i == 0) {
143                         for(int j = 0; j < explicitUsers.length; j++) {
144                             userMap.put(explicitUsers[j].getId(), explicitUsers[j]);
145                         }
146                     } else {
147                         for(Iterator iter = userMap.keySet().iterator(); iter.hasNext(); ) {
148                             boolean found = false;
149                             Integer JavaDoc userId = (Integer JavaDoc) iter.next();
150                             for(int j = 0; j < explicitUsers.length; j++) {
151                                 if(userId.equals(explicitUsers[j].getId())) {
152                                     found = true;
153                                     break;
154                                 }
155                             }
156                             if(! found) {
157                                 iter.remove();
158                             }
159                         }
160                     }
161                 }
162             }
163
164             UserModel[] superUsers = getUserHandler().getSuperUsers();
165             for(int i = 0; i < superUsers.length; i++) {
166                 if(! activeOnly || superUsers[i].getStatus() == UserUtilities.STATUS_ACTIVE) {
167                     userMap.put(superUsers[i].getId(), superUsers[i]);
168                 }
169             }
170
171             int i = 0;
172             userArray = new UserModel[userMap.size()];
173             for(Iterator iter = userMap.values().iterator(); iter.hasNext(); i++) {
174                 userArray[i] = (UserModel) iter.next();
175             }
176         } catch(Exception JavaDoc e) {
177             Logger.logDebug("Error retreiving users with permissions.", e);
178             throw new AuthenticatorException();
179         }
180
181         return userArray;
182     }
183
184     /**
185       * The DefaultAuthenticator always allows self registered users.
186       * @param user a UserModel object that contains the data the user submitted
187       * @param authentication the user's authentication information, if known
188       * @param authType the type of authentication information being provided
189       * @param reqSource the source of the request (eg web, api)
190       * @return true
191       */

192     public boolean allowRegistration(UserModel user, Object JavaDoc authentication, int authType, int reqSource) throws AuthenticatorException {
193         return true;
194     }
195
196
197     /**
198       * The DefaultAuthenticator always allows new user profiles.
199       * @param user a UserModel object that contains the data the user submitted
200       * @param authentication the user's authentication information, if known
201       * @param authType the type of authentication information being provided
202       * @return true
203       * @throws AuthenticatorException an exception if an error occurs
204       */

205     public boolean allowProfileCreation(UserModel user, Object JavaDoc authentication, int authType, int reqSource) throws AuthenticatorException {
206         return true;
207     }
208
209     /**
210       * The DefaultAuthenticator always allows profile updates.
211       * @param user a UserModel object that contains the data the user submitted
212       * @param authentication the user's authentication information, if known
213       * @param authType the type of authentication information being provided
214       * @param reqSource the source of the request (eg web, api)
215       * @return true
216       * @throws AuthenticatorException an exception if an error occurs
217       */

218     public boolean allowProfileUpdates(UserModel user, Object JavaDoc authentication, int authType, int reqSource) throws AuthenticatorException {
219         return true;
220     }
221
222     /**
223       * The DefaultAuthenticator always allows password updates.
224       * @param user a UserModel object that contains the data the user submitted
225       * @param authentication the user's authentication information, if known
226       * @param authType the type of authentication information being provided
227       * @param reqSource the source of the request (eg web, api)
228       * @return true
229       * @throws AuthenticatorException an exception if an error occurs
230       */

231     public boolean allowPasswordUpdates(UserModel user, Object JavaDoc authentication, int authType, int reqSource) throws AuthenticatorException {
232         return true;
233     }
234
235     /**
236       * The DefaultAuthenticator always allows permission updates.
237       * @param user a UserModel object that contains the data the user submitted
238       * @param authentication the user's authentication information, if known
239       * @param authType the type of authentication information being provided
240       * @param reqSource the source of the request (eg web, api)
241       * @return true
242       * @throws AuthenticatorException an exception if an error occurs
243       */

244     public boolean allowPermissionUpdates(UserModel user, Object JavaDoc authentication, int authType, int reqSource) throws AuthenticatorException {
245         return true;
246     }
247
248     /**
249       * The DefaultAuthenticator always allows preferences updates.
250       * @param user a UserModel object that contains the data the user submitted
251       * @param authentication the user's authentication information, if known
252       * @param authType the type of authentication information being provided
253       * @param reqSource the source of the request (eg web, api)
254       * @return true
255       * @throws AuthenticatorException an exception if an error occurs
256       */

257     public boolean allowPreferenceUpdates(UserModel user, Object JavaDoc authentication, int authType, int reqSource) throws AuthenticatorException {
258         return true;
259     }
260
261     /**
262       * The DefaultAuthenticator does not make any changes to a newly created profile.
263       * @param user a UserModel object that contains the newly created profile
264       * @param authentication the user's authentication information, if known
265       * @param authType the type of authentication information being provided
266       * @param reqSource the source of the request (eg web, api)
267       * @return boolean indicating whther changes to the user were made
268       * @throws AuthenticatorException an error occurs
269       */

270     public boolean createProfile(UserModel user, Object JavaDoc authentication, int authType, int reqSource) throws AuthenticatorException {
271         return false;
272     }
273
274     /**
275       * The DefaultAuthenticator does not make any changes to an updated profile.
276       * @param user a UserModel object that contains the updated profile
277       * @param updateType the type of information that is being updated
278       * @param authentication the user's authentication information, if known
279       * @param authType the type of authentication information being provided
280       * @param reqSource the source of the request (eg web, api)
281       * @return boolean indicating whther changes to the user were made
282       * @throws AuthenticatorException an exception if the login is unsuccessful, or an error occurs
283       */

284     public boolean updateProfile(UserModel user, int updateType, Object JavaDoc authentication, int authType, int reqSource) throws AuthenticatorException {
285         return false;
286     }
287 }
288
Popular Tags