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.exception.SystemException;
018    import com.liferay.portal.kernel.util.PropsKeys;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.Company;
022    import com.liferay.portal.model.CompanyConstants;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.security.ldap.PortalLDAPImporterUtil;
025    import com.liferay.portal.service.UserLocalServiceUtil;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portal.util.PrefsPropsUtil;
028    import com.liferay.portal.util.PropsValues;
029    
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletResponse;
032    
033    /**
034     * @author Mika Koivisto
035     * @author Wesley Gong
036     */
037    public class SiteMinderAutoLogin extends BaseAutoLogin {
038    
039            @Override
040            protected String[] doLogin(
041                            HttpServletRequest request, HttpServletResponse response)
042                    throws Exception {
043    
044                    Company company = PortalUtil.getCompany(request);
045    
046                    long companyId = company.getCompanyId();
047    
048                    if (!AuthSettingsUtil.isSiteMinderEnabled(companyId)) {
049                            return null;
050                    }
051    
052                    String siteMinderUserHeader = request.getHeader(
053                            PrefsPropsUtil.getString(
054                                    companyId, PropsKeys.SITEMINDER_USER_HEADER,
055                                    PropsValues.SITEMINDER_USER_HEADER));
056    
057                    if (Validator.isNull(siteMinderUserHeader)) {
058                            return null;
059                    }
060    
061                    String authType = company.getAuthType();
062    
063                    User user = null;
064    
065                    if (PrefsPropsUtil.getBoolean(
066                                    companyId, PropsKeys.SITEMINDER_IMPORT_FROM_LDAP,
067                                    PropsValues.SITEMINDER_IMPORT_FROM_LDAP)) {
068    
069                            try {
070                                    if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
071                                            user = PortalLDAPImporterUtil.importLDAPUser(
072                                                    companyId, siteMinderUserHeader, StringPool.BLANK);
073                                    }
074                                    else {
075                                            user = PortalLDAPImporterUtil.importLDAPUser(
076                                                    companyId, StringPool.BLANK, siteMinderUserHeader);
077                                    }
078                            }
079                            catch (SystemException se) {
080                            }
081                    }
082    
083                    if (user == null) {
084                            if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
085                                    user = UserLocalServiceUtil.fetchUserByEmailAddress(
086                                            companyId, siteMinderUserHeader);
087                            }
088                            else {
089                                    user = UserLocalServiceUtil.fetchUserByScreenName(
090                                            companyId, siteMinderUserHeader);
091                            }
092                    }
093    
094                    String[] credentials = new String[3];
095    
096                    credentials[0] = String.valueOf(user.getUserId());
097                    credentials[1] = user.getPassword();
098                    credentials[2] = Boolean.TRUE.toString();
099    
100                    return credentials;
101            }
102    
103    }