1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
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  /**
37   * <a href="RememberMeAutoLogin.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
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              // LEP-5188
58  
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 }