001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.security.auth;
022:
023: import com.liferay.portal.kernel.util.KeyValuePair;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.kernel.util.Validator;
026: import com.liferay.portal.model.Company;
027: import com.liferay.portal.service.UserLocalServiceUtil;
028: import com.liferay.portal.util.CookieKeys;
029: import com.liferay.portal.util.PortalUtil;
030:
031: import javax.servlet.http.Cookie;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034:
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037:
038: /**
039: * <a href="RememberMeAutoLogin.java.html"><b><i>View Source</i></b></a>
040: *
041: * @author Brian Wing Shun Chan
042: *
043: */
044: public class RememberMeAutoLogin implements AutoLogin {
045:
046: public String[] login(HttpServletRequest req,
047: HttpServletResponse res) throws AutoLoginException {
048:
049: try {
050: String[] credentials = null;
051:
052: String autoUserId = CookieKeys
053: .getCookie(req, CookieKeys.ID);
054: String autoPassword = CookieKeys.getCookie(req,
055: CookieKeys.PASSWORD);
056: String rememberMe = CookieKeys.getCookie(req,
057: CookieKeys.REMEMBER_ME);
058:
059: // LEP-5188
060:
061: if (!PortalUtil.getPathContext().equals(
062: req.getContextPath())) {
063: rememberMe = Boolean.TRUE.toString();
064: }
065:
066: if (Validator.isNotNull(autoUserId)
067: && Validator.isNotNull(autoPassword)
068: && Validator.isNotNull(rememberMe)) {
069:
070: Company company = PortalUtil.getCompany(req);
071:
072: KeyValuePair kvp = null;
073:
074: if (company.isAutoLogin()) {
075: kvp = UserLocalServiceUtil.decryptUserId(company
076: .getCompanyId(), autoUserId, autoPassword);
077:
078: credentials = new String[3];
079:
080: credentials[0] = kvp.getKey();
081: credentials[1] = kvp.getValue();
082: credentials[2] = Boolean.FALSE.toString();
083: }
084: }
085:
086: return credentials;
087: } catch (Exception e) {
088: _log.warn(e, e);
089:
090: Cookie cookie = new Cookie(CookieKeys.ID, StringPool.BLANK);
091:
092: cookie.setMaxAge(0);
093: cookie.setPath(StringPool.SLASH);
094:
095: CookieKeys.addCookie(res, cookie);
096:
097: cookie = new Cookie(CookieKeys.PASSWORD, StringPool.BLANK);
098:
099: cookie.setMaxAge(0);
100: cookie.setPath(StringPool.SLASH);
101:
102: CookieKeys.addCookie(res, cookie);
103:
104: throw new AutoLoginException(e);
105: }
106: }
107:
108: private static Log _log = LogFactory
109: .getLog(RememberMeAutoLogin.class);
110:
111: }
|