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