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.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    }