KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > services > AuthServiceImpl


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.services;
17
18 import org.jmanage.core.auth.*;
19 import org.jmanage.core.util.JManageProperties;
20 import org.jmanage.core.util.ErrorCodes;
21 import org.jmanage.core.util.UserActivityLogger;
22
23 import javax.security.auth.login.LoginContext JavaDoc;
24 import javax.security.auth.login.LoginException JavaDoc;
25 import java.util.*;
26
27 /**
28  *
29  * date: Feb 4, 2005
30  * @author Rakesh Kalra, Shashank Bellary
31  */

32 public class AuthServiceImpl implements AuthService {
33
34     private static int MAX_LOGIN_ATTEMPTS_ALLOWED =
35             Integer.parseInt(JManageProperties.getInstance().
36             getProperty(JManageProperties.LOGIN_MAX_ATTEMPTS));
37
38     /**
39      * @see AuthService login()
40      */

41     public void login(ServiceContext context,
42                       String JavaDoc username,
43                       String JavaDoc password) throws ServiceException{
44
45         LoginCallbackHandler callbackHandler =
46                 new LoginCallbackHandler(username, password);
47         User user = null;
48         UserManager userManager = UserManager.getInstance();
49         UserActivityLogger logger = UserActivityLogger.getInstance();
50         try{
51             final LoginContext JavaDoc loginContext =
52                     new LoginContext JavaDoc(AuthConstants.AUTH_CONFIG_INDEX,
53                             callbackHandler);
54             loginContext.login();
55             /* Need this for external login modules, user is really
56             authenticated after this step */

57
58             Set principals = loginContext.getSubject().getPrincipals();
59             Object JavaDoc obj = null;
60             for(Iterator principalIt = principals.iterator(); principalIt.hasNext();){
61                 if((obj = principalIt.next()) instanceof User){
62                     user = (User)obj;
63                     break;
64                 }
65             }
66
67             /* Successful login:
68                 - Add new users authenticated through external LoginModules.
69                 - Update the lock count and status of existing users */

70             if(user == null){
71                 user = new User();
72                 user.setUsername(username); user.setExternalUser(true);
73                 List roles = new ArrayList();
74                 roles.add(new Role(org.jmanage.core.auth.ExternalUserRolesConfig.getInstance().getUserRole(username)));
75                 user.setRoles(roles);
76             }else{
77                 user = userManager.getUser(user.getName());
78                 user.setLockCount(0);
79                 user.setStatus(User.STATUS_ACTIVE);
80                 userManager.updateUser(user);
81             }
82             /* set Subject in session */
83             context._setUser(user);
84             logger.logActivity(user.getName(), "logged in successfully");
85         }catch(LoginException JavaDoc lex){
86             user = userManager.getUser(username);
87             String JavaDoc errorCode = ErrorCodes.UNKNOWN_ERROR;
88             Object JavaDoc[] values = null;
89             /* Conditionalize the error message */
90             if(user == null){
91                 errorCode = ErrorCodes.INVALID_CREDENTIALS;
92             }else if(User.STATUS_LOCKED.equals(user.getStatus())){
93                 errorCode = ErrorCodes.ACCOUNT_LOCKED;
94             }else if(user.getLockCount() < MAX_LOGIN_ATTEMPTS_ALLOWED){
95                 int thisAttempt = user.getLockCount()+1;
96                 user.setLockCount(thisAttempt);
97                 if(thisAttempt == MAX_LOGIN_ATTEMPTS_ALLOWED){
98                     user.setStatus(User.STATUS_LOCKED);
99                     userManager.updateUser(user);
100                     errorCode = ErrorCodes.ACCOUNT_LOCKED;
101                 }else{
102                     userManager.updateUser(user);
103                     errorCode = ErrorCodes.INVALID_LOGIN_ATTEMPTS;
104                     values = new Object JavaDoc[]{
105                         String.valueOf(MAX_LOGIN_ATTEMPTS_ALLOWED - thisAttempt)};
106                 }
107             }
108             if(user != null)
109                 logger.logActivity(username, user.getName()+" failed to login");
110             throw new ServiceException(errorCode, values);
111         }
112     }
113
114     /**
115      *
116      * @param context
117      * @throws ServiceException
118      */

119     public void logout(ServiceContext context, User user)throws ServiceException{
120
121         // TODO: loginContext needs to be held in the session, so that we
122
// can use the right object to do logout
123
//loginContext.logout();
124
UserActivityLogger.getInstance().logActivity(user.getName(),
125                 "logged out successfully");
126     }
127 }
Popular Tags