1
19
20 package com.liferay.portal.security.auth;
21
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24 import com.liferay.portal.kernel.util.KeyValuePair;
25 import com.liferay.portal.kernel.util.StringPool;
26 import com.liferay.portal.kernel.util.Validator;
27 import com.liferay.portal.model.Company;
28 import com.liferay.portal.service.UserLocalServiceUtil;
29 import com.liferay.portal.util.CookieKeys;
30 import com.liferay.portal.util.PortalUtil;
31
32 import javax.servlet.http.Cookie;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35
36
42 public class RememberMeAutoLogin implements AutoLogin {
43
44 public String[] login(
45 HttpServletRequest request, HttpServletResponse response)
46 throws AutoLoginException {
47
48 try {
49 String[] credentials = null;
50
51 String autoUserId = CookieKeys.getCookie(request, CookieKeys.ID);
52 String autoPassword = CookieKeys.getCookie(
53 request, CookieKeys.PASSWORD);
54 String rememberMe = CookieKeys.getCookie(
55 request, CookieKeys.REMEMBER_ME);
56
57
59 if (!PortalUtil.getPathContext().equals(request.getContextPath())) {
60 rememberMe = Boolean.TRUE.toString();
61 }
62
63 if (Validator.isNotNull(autoUserId) &&
64 Validator.isNotNull(autoPassword) &&
65 Validator.isNotNull(rememberMe)) {
66
67 Company company = PortalUtil.getCompany(request);
68
69 KeyValuePair kvp = null;
70
71 if (company.isAutoLogin()) {
72 kvp = UserLocalServiceUtil.decryptUserId(
73 company.getCompanyId(), autoUserId, autoPassword);
74
75 credentials = new String[3];
76
77 credentials[0] = kvp.getKey();
78 credentials[1] = kvp.getValue();
79 credentials[2] = Boolean.FALSE.toString();
80 }
81 }
82
83 return credentials;
84 }
85 catch (Exception e) {
86 _log.warn(e, e);
87
88 Cookie cookie = new Cookie(CookieKeys.ID, StringPool.BLANK);
89
90 cookie.setMaxAge(0);
91 cookie.setPath(StringPool.SLASH);
92
93 CookieKeys.addCookie(request, response, cookie);
94
95 cookie = new Cookie(CookieKeys.PASSWORD, StringPool.BLANK);
96
97 cookie.setMaxAge(0);
98 cookie.setPath(StringPool.SLASH);
99
100 CookieKeys.addCookie(request, response, cookie);
101
102 throw new AutoLoginException(e);
103 }
104 }
105
106 private static Log _log = LogFactoryUtil.getLog(RememberMeAutoLogin.class);
107
108 }