001    /**
002     * Copyright (c) 2000-present 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.PropsKeys;
018    import com.liferay.portal.model.User;
019    import com.liferay.portal.security.exportimport.UserImporterUtil;
020    import com.liferay.portal.util.PortalUtil;
021    import com.liferay.portal.util.PrefsPropsUtil;
022    import com.liferay.portal.util.PropsValues;
023    import com.liferay.portal.util.WebKeys;
024    
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    
028    /**
029     * @author Bruno Farache
030     */
031    public class NtlmAutoLogin extends BaseAutoLogin {
032    
033            @Override
034            protected String[] doLogin(
035                            HttpServletRequest request, HttpServletResponse response)
036                    throws Exception {
037    
038                    long companyId = PortalUtil.getCompanyId(request);
039    
040                    if (!PrefsPropsUtil.getBoolean(
041                                    companyId, PropsKeys.NTLM_AUTH_ENABLED,
042                                    PropsValues.NTLM_AUTH_ENABLED)) {
043    
044                            return null;
045                    }
046    
047                    String screenName = (String)request.getAttribute(
048                            WebKeys.NTLM_REMOTE_USER);
049    
050                    if (screenName == null) {
051                            return null;
052                    }
053    
054                    request.removeAttribute(WebKeys.NTLM_REMOTE_USER);
055    
056                    User user = UserImporterUtil.importUserByScreenName(
057                            companyId, screenName);
058    
059                    if (user == null) {
060                            return null;
061                    }
062    
063                    addRedirect(request);
064    
065                    String[] credentials = new String[3];
066    
067                    credentials[0] = String.valueOf(user.getUserId());
068                    credentials[1] = user.getPassword();
069                    credentials[2] = Boolean.TRUE.toString();
070    
071                    return credentials;
072            }
073    
074    }