001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.security.auth;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.security.ldap.LDAPSettingsUtil;
023    import com.liferay.portal.security.ldap.PortalLDAPImporterUtil;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portal.util.WebKeys;
026    
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    /**
031     * @author Bruno Farache
032     */
033    public class NtlmAutoLogin implements AutoLogin {
034    
035            public String[] login(
036                    HttpServletRequest request, HttpServletResponse response) {
037    
038                    String[] credentials = null;
039    
040                    try {
041                            long companyId = PortalUtil.getCompanyId(request);
042    
043                            if (!LDAPSettingsUtil.isNtlmEnabled(companyId)) {
044                                    return credentials;
045                            }
046    
047                            String screenName = (String)request.getAttribute(
048                                    WebKeys.NTLM_REMOTE_USER);
049    
050                            if (screenName == null) {
051                                    return credentials;
052                            }
053    
054                            request.removeAttribute(WebKeys.NTLM_REMOTE_USER);
055    
056                            User user = PortalLDAPImporterUtil.importLDAPUserByScreenName(
057                                    companyId, screenName);
058    
059                            if (user != null) {
060                                    String redirect = ParamUtil.getString(request, "redirect");
061    
062                                    if (Validator.isNotNull(redirect)) {
063                                            request.setAttribute(
064                                                    AutoLogin.AUTO_LOGIN_REDIRECT_AND_CONTINUE, redirect);
065                                    }
066    
067                                    credentials = new String[3];
068    
069                                    credentials[0] = String.valueOf(user.getUserId());
070                                    credentials[1] = user.getPassword();
071                                    credentials[2] = Boolean.TRUE.toString();
072                            }
073                    }
074                    catch (Exception e) {
075                            _log.error(e, e);
076                    }
077    
078                    return credentials;
079            }
080    
081            private static Log _log = LogFactoryUtil.getLog(NtlmAutoLogin.class);
082    
083    }