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.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.exportimport.UserImporterUtil;
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 (!PrefsPropsUtil.getBoolean(
049                                    companyId, PropsKeys.SITEMINDER_AUTH_ENABLED,
050                                    PropsValues.SITEMINDER_AUTH_ENABLED)) {
051    
052                            return null;
053                    }
054    
055                    String siteMinderUserHeader = request.getHeader(
056                            PrefsPropsUtil.getString(
057                                    companyId, PropsKeys.SITEMINDER_USER_HEADER,
058                                    PropsValues.SITEMINDER_USER_HEADER));
059    
060                    if (Validator.isNull(siteMinderUserHeader)) {
061                            return null;
062                    }
063    
064                    String authType = company.getAuthType();
065    
066                    User user = null;
067    
068                    if (PrefsPropsUtil.getBoolean(
069                                    companyId, PropsKeys.SITEMINDER_IMPORT_FROM_LDAP,
070                                    PropsValues.SITEMINDER_IMPORT_FROM_LDAP)) {
071    
072                            try {
073                                    if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
074                                            user = UserImporterUtil.importUser(
075                                                    companyId, siteMinderUserHeader, StringPool.BLANK);
076                                    }
077                                    else {
078                                            user = UserImporterUtil.importUser(
079                                                    companyId, StringPool.BLANK, siteMinderUserHeader);
080                                    }
081                            }
082                            catch (SystemException se) {
083                            }
084                    }
085    
086                    if (user == null) {
087                            if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
088                                    user = UserLocalServiceUtil.fetchUserByEmailAddress(
089                                            companyId, siteMinderUserHeader);
090                            }
091                            else {
092                                    user = UserLocalServiceUtil.fetchUserByScreenName(
093                                            companyId, siteMinderUserHeader);
094                            }
095                    }
096    
097                    addRedirect(request);
098    
099                    String[] credentials = new String[3];
100    
101                    credentials[0] = String.valueOf(user.getUserId());
102                    credentials[1] = user.getPassword();
103                    credentials[2] = Boolean.TRUE.toString();
104    
105                    return credentials;
106            }
107    
108    }