001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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.PortalLDAPImporterUtil;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portal.util.WebKeys;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletResponse;
028    
029    /**
030     * @author Bruno Farache
031     */
032    public class NtlmAutoLogin implements AutoLogin {
033    
034            public String[] login(
035                    HttpServletRequest request, HttpServletResponse response) {
036    
037                    String[] credentials = null;
038    
039                    try {
040                            long companyId = PortalUtil.getCompanyId(request);
041    
042                            if (!AuthSettingsUtil.isNtlmEnabled(companyId)) {
043                                    return credentials;
044                            }
045    
046                            String screenName = (String)request.getAttribute(
047                                    WebKeys.NTLM_REMOTE_USER);
048    
049                            if (screenName == null) {
050                                    return credentials;
051                            }
052    
053                            request.removeAttribute(WebKeys.NTLM_REMOTE_USER);
054    
055                            User user = PortalLDAPImporterUtil.importLDAPUserByScreenName(
056                                    companyId, screenName);
057    
058                            if (user != null) {
059                                    String redirect = ParamUtil.getString(request, "redirect");
060    
061                                    if (Validator.isNotNull(redirect)) {
062                                            request.setAttribute(
063                                                    AutoLogin.AUTO_LOGIN_REDIRECT_AND_CONTINUE, redirect);
064                                    }
065    
066                                    credentials = new String[3];
067    
068                                    credentials[0] = String.valueOf(user.getUserId());
069                                    credentials[1] = user.getPassword();
070                                    credentials[2] = Boolean.TRUE.toString();
071                            }
072                    }
073                    catch (Exception e) {
074                            _log.error(e, e);
075                    }
076    
077                    return credentials;
078            }
079    
080            private static Log _log = LogFactoryUtil.getLog(NtlmAutoLogin.class);
081    
082    }