001    /**
002     * Copyright (c) 2000-2012 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.NoSuchUserException;
018    import com.liferay.portal.kernel.exception.SystemException;
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                            try {
086                                    if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
087                                            user = UserLocalServiceUtil.getUserByEmailAddress(
088                                                    companyId, siteMinderUserHeader);
089                                    }
090                                    else {
091                                            user = UserLocalServiceUtil.getUserByScreenName(
092                                                    companyId, siteMinderUserHeader);
093                                    }
094                            }
095                            catch (NoSuchUserException nsue) {
096                                    return null;
097                            }
098                    }
099    
100                    String[] credentials = new String[3];
101    
102                    credentials[0] = String.valueOf(user.getUserId());
103                    credentials[1] = user.getPassword();
104                    credentials[2] = Boolean.TRUE.toString();
105    
106                    return credentials;
107            }
108    
109    }