001    /**
002     * Copyright (c) 2000-2013 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.util.ParamUtil;
018    import com.liferay.portal.kernel.util.Validator;
019    import com.liferay.portal.model.User;
020    import com.liferay.portal.security.ldap.PortalLDAPImporterUtil;
021    import com.liferay.portal.util.PortalUtil;
022    import com.liferay.portal.util.WebKeys;
023    
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.http.HttpServletResponse;
026    
027    /**
028     * @author Bruno Farache
029     */
030    public class NtlmAutoLogin extends BaseAutoLogin {
031    
032            @Override
033            protected String[] doLogin(
034                            HttpServletRequest request, HttpServletResponse response)
035                    throws Exception {
036    
037                    long companyId = PortalUtil.getCompanyId(request);
038    
039                    if (!AuthSettingsUtil.isNtlmEnabled(companyId)) {
040                            return null;
041                    }
042    
043                    String screenName = (String)request.getAttribute(
044                            WebKeys.NTLM_REMOTE_USER);
045    
046                    if (screenName == null) {
047                            return null;
048                    }
049    
050                    request.removeAttribute(WebKeys.NTLM_REMOTE_USER);
051    
052                    User user = PortalLDAPImporterUtil.importLDAPUserByScreenName(
053                            companyId, screenName);
054    
055                    if (user == null) {
056                            return null;
057                    }
058    
059                    String redirect = ParamUtil.getString(request, "redirect");
060    
061                    if (Validator.isNotNull(redirect)) {
062                            request.setAttribute(
063                                    AutoLogin.AUTO_LOGIN_REDIRECT_AND_CONTINUE,
064                                    PortalUtil.escapeRedirect(redirect));
065                    }
066    
067                    String[] 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                    return credentials;
074            }
075    
076    }