1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.services.authentication;
20
21 import org.apache.log4j.Logger;
22 import org.itracker.model.Permission;
23 import org.itracker.model.PermissionType;
24 import org.itracker.model.User;
25 import org.itracker.UserException;
26 import org.itracker.services.exceptions.AuthenticatorException;
27 import org.itracker.PasswordException;
28 import org.itracker.model.util.UserUtilities;
29
30 import org.springframework.dao.DataAccessException;
31
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37
38
39
40
41
42
43 public class DefaultAuthenticator extends AbstractPluggableAuthenticator {
44
45 private static final Logger logger = Logger.getLogger(DefaultAuthenticator.class);
46
47
48
49
50
51
52
53
54
55
56
57
58 public User checkLogin(final String login, final Object authentication, final int authType, final int reqSource) throws AuthenticatorException {
59 if (logger.isDebugEnabled()) {
60 logger.debug("Checking login for " + login + " using DefaultAuthenticator");
61 }
62
63 if (login != null && authentication != null && !login.equals("")) {
64 User user;
65 try {
66 user = getUserService().getUserByLogin(login);
67 } catch (DataAccessException e) {
68 logger.error("checkLogin: failed to get user by login: " + login, e);
69 throw new AuthenticatorException(AuthenticatorException.UNKNOWN_USER, e.getMessage());
70 }
71
72 if (user.getStatus() != UserUtilities.STATUS_ACTIVE) {
73 AuthenticatorExceptionticatorException.html#AuthenticatorException">AuthenticatorException e = new AuthenticatorException(AuthenticatorException.INACTIVE_ACCOUNT);
74 logger.info("checkLogin: user is inactive, user: " + user, e);
75 throw e;
76 }
77
78 String userPassword;
79 try {
80 userPassword = getUserService().getUserPasswordByLogin(login);
81 } catch (DataAccessException e) {
82 AuthenticatorExceptionicatorException.html#AuthenticatorException">AuthenticatorException ex = new AuthenticatorException(e.getMessage(), authType);
83 logger.info("checkLogin: user is inactive, user: " + user, ex);
84 throw e;
85 }
86 if (userPassword == null || userPassword.equals("")) {
87 AuthenticatorExceptionticatorException.html#AuthenticatorException">AuthenticatorException e = new AuthenticatorException(AuthenticatorException.INVALID_PASSWORD);
88 logger.info("checkLogin: user has no password, user: " + user, e);
89 throw e;
90 }
91
92 try {
93 if (!userPassword.endsWith("=")) {
94 logger.info("checkLogin: User " + login + " has old style password. Converting to SHA1 hash.");
95 try {
96 user.setPassword(UserUtilities.encryptPassword(userPassword));
97 getUserService().updateUser(user);
98 } catch (UserException ue) {
99 logger.error("checkLogin: User password conversion failed for user " + user, ue);
100 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
101 }
102 }
103
104 if (authType == AUTH_TYPE_PASSWORD_PLAIN) {
105 if (!userPassword.equals(UserUtilities.encryptPassword((String) authentication))) {
106 throw new AuthenticatorException(AuthenticatorException.INVALID_PASSWORD);
107 }
108 } else if (authType == AUTH_TYPE_PASSWORD_ENC) {
109 if (!userPassword.equals(authentication)) {
110 throw new AuthenticatorException(AuthenticatorException.INVALID_PASSWORD);
111 }
112 } else {
113 logger.info("checkLogin: invalid authenticator type: " + authType);
114 throw new AuthenticatorException(AuthenticatorException.INVALID_AUTHENTICATION_TYPE);
115 }
116 } catch (ClassCastException cce) {
117 logger.error("checkLogin: Authenticator was of wrong type.", cce);
118 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
119 } catch (PasswordException pe) {
120 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
121 } catch (AuthenticatorException ae) {
122 if (logger.isDebugEnabled()) {
123 logger.debug("checkLogin: failed to authenticate " + login, ae);
124 }
125 throw ae;
126 }
127
128 return user;
129 }
130
131 logger.info("checkLogin: no login was supplied: " + login + ", type: " + authType + ", source: " + reqSource);
132 throw new AuthenticatorException(AuthenticatorException.INVALID_DATA);
133 }
134
135
136
137
138
139
140
141
142
143 public List<Permission> getUserPermissions(User user, int reqSource) throws AuthenticatorException {
144 if (user == null || user.getId() == null) {
145 throw new AuthenticatorException(AuthenticatorException.INVALID_DATA);
146 }
147
148 List<Permission> permissionList;
149 try {
150 permissionList = getUserService().getUserPermissionsLocal(user);
151 } catch (DataAccessException e) {
152 throw new AuthenticatorException(e.getMessage(), reqSource);
153 }
154
155 if (user.isSuperUser()) {
156 List<Permission> augmentedPermissions = new ArrayList<Permission>();
157
158
159 Permission permission = new Permission(PermissionType.USER_ADMIN, user, null);
160 augmentedPermissions.add(permission);
161 augmentedPermissions.addAll(permissionList);
162 return augmentedPermissions;
163
164 } else {
165 return permissionList;
166 }
167
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182 @Override
183 public List<User> getUsersWithProjectPermission(Integer projectId, PermissionType[] permissionTypes, boolean requireAll, boolean activeOnly, int reqSource) throws AuthenticatorException {
184
185 List<User> users;
186
187 try {
188 Map<Integer, User> userMap = new HashMap<Integer, User>();
189
190 if (requireAll) {
191
192 List<User> explicitUsers = getUserService().findUsersForProjectByPermissionTypeList(projectId, permissionTypes);
193
194 for (User user : explicitUsers) {
195 userMap.put(user.getId(), user);
196 }
197 } else {
198
199 for (int i = 0; i < permissionTypes.length; i++) {
200 List<User> explicitUsers = getUserService().getUsersWithPermissionLocal(projectId, permissionTypes[i]);
201
202 for (User user : explicitUsers) {
203 userMap.put(user.getId(), user);
204 }
205
206 }
207
208 }
209
210 List<User> superUsers = getUserService().getSuperUsers();
211 for (User superUser : superUsers) {
212 userMap.put(superUser.getId(), superUser);
213 }
214
215 users = new ArrayList<User>();
216 for (User user : userMap.values()) {
217 if (activeOnly) {
218 if (user.getStatus() == UserUtilities.STATUS_ACTIVE) {
219 users.add(user);
220 }
221 } else {
222 users.add(user);
223 }
224 }
225
226 } catch (Exception e) {
227 logger.error("Error retreiving users with permissions.", e);
228 throw new AuthenticatorException();
229 }
230
231 return users;
232 }
233 @Override
234 @Deprecated
235 public List<User> getUsersWithProjectPermission(Integer projectId,
236 int[] permissionTypes,
237 boolean requireAll,
238 boolean activeOnly,
239 int reqSource)
240 throws AuthenticatorException {
241
242 return getUsersWithProjectPermission(projectId,
243 PermissionType.valueOf(permissionTypes),
244 requireAll,
245 activeOnly,
246 reqSource);
247
248 }
249
250
251
252
253
254
255
256
257
258
259 public boolean allowRegistration(User user, Object authentication, int authType, int reqSource) throws AuthenticatorException {
260 return true;
261 }
262
263
264
265
266
267
268
269
270
271
272
273 public boolean allowProfileCreation(User user, Object authentication, int authType, int reqSource) throws AuthenticatorException {
274 return true;
275 }
276
277
278
279
280
281
282
283
284
285
286
287 public boolean allowProfileUpdates(User user, Object authentication, int authType, int reqSource) throws AuthenticatorException {
288 return true;
289 }
290
291
292
293
294
295
296
297
298
299
300
301 public boolean allowPasswordUpdates(User user, Object authentication, int authType, int reqSource) throws AuthenticatorException {
302 return true;
303 }
304
305
306
307
308
309
310
311
312
313
314
315 public boolean allowPermissionUpdates(User user, Object authentication, int authType, int reqSource) throws AuthenticatorException {
316 return true;
317 }
318
319
320
321
322
323
324
325
326
327
328
329 public boolean allowPreferenceUpdates(User user, Object authentication, int authType, int reqSource) throws AuthenticatorException {
330 return true;
331 }
332
333
334
335
336
337
338
339
340
341
342
343 public boolean createProfile(User user, Object authentication, int authType, int reqSource) throws AuthenticatorException {
344 return false;
345 }
346
347
348
349
350
351
352
353
354
355
356
357
358 public boolean updateProfile(User user, int updateType, Object authentication, int authType, int reqSource) throws AuthenticatorException {
359 return false;
360 }
361
362 }