/** * Copyright (c) 2000-present Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.liferay.portal.security.sso.ntlm.internal.auto.login; import com.liferay.portal.kernel.model.User; import com.liferay.portal.kernel.module.configuration.ConfigurationProvider; import com.liferay.portal.kernel.security.auto.login.AutoLogin; import com.liferay.portal.kernel.security.auto.login.BaseAutoLogin; import com.liferay.portal.kernel.settings.CompanyServiceSettingsLocator; import com.liferay.portal.kernel.util.Portal; import com.liferay.portal.security.exportimport.UserImporter; import com.liferay.portal.security.sso.ntlm.configuration.NtlmConfiguration; import com.liferay.portal.security.sso.ntlm.constants.NtlmConstants; import com.liferay.portal.security.sso.ntlm.constants.NtlmWebKeys; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; /** * Participates in every unauthenticated HTTP request to Liferay Portal. * * <p> * This class looks for the <code>NTLM_REMOTE_USER</code> request attribute * which contains the user's screen name if authentication took place via NTLM. * If found, the user is imported from LDAP and logged in. * </p> * * @author Bruno Farache */ @Component( configurationPid = "com.liferay.portal.security.sso.ntlm.configuration.NtlmConfiguration", immediate = true, service = AutoLogin.class ) public class NtlmAutoLogin extends BaseAutoLogin { @Override protected String[] doLogin( HttpServletRequest request, HttpServletResponse response) throws Exception { long companyId = _portal.getCompanyId(request); NtlmConfiguration ntlmConfiguration = _configurationProvider.getConfiguration( NtlmConfiguration.class, new CompanyServiceSettingsLocator( companyId, NtlmConstants.SERVICE_NAME)); if (!ntlmConfiguration.enabled()) { return null; } String screenName = (String)request.getAttribute( NtlmWebKeys.NTLM_REMOTE_USER); if (screenName == null) { return null; } request.removeAttribute(NtlmWebKeys.NTLM_REMOTE_USER); User user = _userImporter.importUserByScreenName(companyId, screenName); if (user == null) { return null; } addRedirect(request); String[] credentials = new String[3]; credentials[0] = String.valueOf(user.getUserId()); credentials[1] = user.getPassword(); credentials[2] = Boolean.TRUE.toString(); return credentials; } @Reference(unbind = "-") protected void setConfigurationProvider( ConfigurationProvider configurationProvider) { _configurationProvider = configurationProvider; } @Reference(unbind = "-") protected void setUserImporter(UserImporter userImporter) { _userImporter = userImporter; } private ConfigurationProvider _configurationProvider; @Reference private Portal _portal; private UserImporter _userImporter; }