1 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 ; 24 import javax.security.auth.login.LoginException ; 25 import java.util.*; 26 27 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 41 public void login(ServiceContext context, 42 String username, 43 String 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 loginContext = 52 new LoginContext (AuthConstants.AUTH_CONFIG_INDEX, 53 callbackHandler); 54 loginContext.login(); 55 57 58 Set principals = loginContext.getSubject().getPrincipals(); 59 Object 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 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 83 context._setUser(user); 84 logger.logActivity(user.getName(), "logged in successfully"); 85 }catch(LoginException lex){ 86 user = userManager.getUser(username); 87 String errorCode = ErrorCodes.UNKNOWN_ERROR; 88 Object [] values = null; 89 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 []{ 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 119 public void logout(ServiceContext context, User user)throws ServiceException{ 120 121 UserActivityLogger.getInstance().logActivity(user.getName(), 125 "logged out successfully"); 126 } 127 } | Popular Tags |