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