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