1 18 19 package cowsultants.itracker.web.util; 20 21 import javax.servlet.*; 22 import javax.servlet.http.*; 23 24 import cowsultants.itracker.ejb.client.util.*; 25 26 public class LoginUtilities { 27 28 public static boolean checkAutoLogin(HttpServletRequest request, boolean allowSaveLogin) { 29 boolean foundLogin = false; 30 31 if(request != null) { 32 int authType = getRequestAuthType(request); 33 34 if(! foundLogin) { 36 if(authType == AuthenticationConstants.AUTH_TYPE_REQUEST) { 37 String redirectURL = request.getRequestURI().substring(request.getContextPath().length()) + 38 (request.getQueryString() != null ? "?" + request.getQueryString() : ""); 39 request.setAttribute(Constants.AUTH_TYPE_KEY, new Integer (AuthenticationConstants.AUTH_TYPE_REQUEST)); 40 request.setAttribute(Constants.AUTH_REDIRECT_KEY, redirectURL); 41 request.setAttribute("processLogin", "true"); 42 foundLogin = true; 43 } 44 } 45 46 48 if(allowSaveLogin && ! foundLogin) { 51 Cookie[] cookies = request.getCookies(); 52 if(cookies != null) { 53 for(int i = 0; i < cookies.length; i++) { 54 if(Constants.COOKIE_NAME.equals(cookies[i].getName())) { 55 int seperator = cookies[i].getValue().indexOf('~'); 56 if(seperator > 0) { 57 if(Logger.isLoggingDebug()) { 58 Logger.logDebug("Attempting autologin for user " + cookies[i].getValue().substring(0,seperator) + "."); 59 } 60 61 String redirectURL = request.getRequestURI().substring(request.getContextPath().length()) + 62 (request.getQueryString() != null ? "?" + request.getQueryString() : ""); 63 request.setAttribute(Constants.AUTH_LOGIN_KEY, cookies[i].getValue().substring(0,seperator)); 64 request.setAttribute(Constants.AUTH_TYPE_KEY, new Integer (AuthenticationConstants.AUTH_TYPE_PASSWORD_ENC)); 65 request.setAttribute(Constants.AUTH_VALUE_KEY, cookies[i].getValue().substring(seperator + 1)); 66 request.setAttribute(Constants.AUTH_REDIRECT_KEY, redirectURL); 67 request.setAttribute("processLogin", "true"); 68 foundLogin = true; 69 } 70 } 71 } 72 } 73 } 74 75 87 } 88 89 return foundLogin; 90 } 91 92 public static int getRequestAuthType(HttpServletRequest request) { 93 int authType = AuthenticationConstants.AUTH_TYPE_UNKNOWN; 94 95 try { 96 if(request.getAttribute(Constants.AUTH_TYPE_KEY) != null) { 97 authType = ((Integer ) request.getAttribute(Constants.AUTH_TYPE_KEY)).intValue(); 98 } 99 if(request.getParameter(Constants.AUTH_TYPE_KEY) != null) { 100 authType = Integer.parseInt(request.getParameter(Constants.AUTH_TYPE_KEY)); 101 } 102 } catch(Exception e) { 103 Logger.logDebug("Error retrieving auth type while checking auto login. " + e.getMessage()); 104 } 105 106 return authType; 107 } 108 } 109 | Popular Tags |