001    /**
002     * Copyright (c) 2000-2013 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            @Override
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 (!AuthSettingsUtil.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    }