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.StringPool;
024: import com.liferay.portal.model.User;
025: import com.liferay.portal.security.ldap.PortalLDAPUtil;
026: import com.liferay.portal.util.PortalUtil;
027: import com.liferay.portal.util.WebKeys;
028:
029: import javax.naming.directory.SearchResult;
030: import javax.naming.ldap.LdapContext;
031:
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="NtlmAutoLogin.java.html"><b><i>View Source</i></b></a>
040: *
041: * @author Bruno Farache
042: *
043: */
044: public class NtlmAutoLogin implements AutoLogin {
045:
046: public String[] login(HttpServletRequest req,
047: HttpServletResponse res) throws AutoLoginException {
048:
049: String[] credentials = null;
050:
051: try {
052: long companyId = PortalUtil.getCompanyId(req);
053:
054: if (!PortalLDAPUtil.isNtlmEnabled(companyId)) {
055: return credentials;
056: }
057:
058: String screenName = (String) req
059: .getAttribute(WebKeys.NTLM_REMOTE_USER);
060:
061: if (screenName == null) {
062: return credentials;
063: }
064:
065: req.removeAttribute(WebKeys.NTLM_REMOTE_USER);
066:
067: User user = getUser(companyId, screenName);
068:
069: if (user != null) {
070: credentials = new String[3];
071:
072: credentials[0] = String.valueOf(user.getUserId());
073: credentials[1] = user.getPassword();
074: credentials[2] = Boolean.TRUE.toString();
075: }
076: } catch (Exception e) {
077: _log.error(e.getMessage());
078: }
079:
080: return credentials;
081: }
082:
083: protected User getUser(long companyId, String screenName)
084: throws Exception {
085: SearchResult result = (SearchResult) PortalLDAPUtil.getUser(
086: companyId, screenName);
087:
088: if (result == null) {
089: if (_log.isWarnEnabled()) {
090: _log.warn("No user was found in LDAP with screenName "
091: + screenName);
092: }
093:
094: return null;
095: }
096:
097: LdapContext ctx = PortalLDAPUtil.getContext(companyId);
098:
099: User user = PortalLDAPUtil.importLDAPUser(companyId, ctx,
100: result.getAttributes(), StringPool.BLANK, false);
101:
102: ctx.close();
103:
104: return user;
105: }
106:
107: private static Log _log = LogFactory.getLog(NtlmAutoLogin.class);
108:
109: }
|