001    /**
002     * Copyright (c) 2000-2010 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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.HttpHeaders;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.service.UserLocalServiceUtil;
025    import com.liferay.portal.util.PortalUtil;
026    import com.liferay.portal.util.PrefsPropsUtil;
027    import com.liferay.portal.util.PropsValues;
028    
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.http.HttpServletResponse;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Wesley Gong
035     */
036    public class RequestHeaderAutoLogin extends CASAutoLogin {
037    
038            public String[] login(
039                    HttpServletRequest request, HttpServletResponse response) {
040    
041                    String[] credentials = null;
042    
043                    try {
044                            long companyId = PortalUtil.getCompanyId(request);
045    
046                            String screenName = request.getHeader(
047                                    HttpHeaders.LIFERAY_SCREEN_NAME);
048    
049                            if (Validator.isNull(screenName)) {
050                                    return credentials;
051                            }
052    
053                            User user = null;
054    
055                            if (PrefsPropsUtil.getBoolean(
056                                            companyId, PropsKeys.REQUEST_HEADER_AUTH_IMPORT_FROM_LDAP,
057                                            PropsValues.REQUEST_HEADER_AUTH_IMPORT_FROM_LDAP)) {
058    
059                                    try {
060                                            user = importLDAPUser(
061                                                    companyId, StringPool.BLANK, screenName);
062                                    }
063                                    catch (Exception e) {
064                                    }
065                            }
066    
067                            if (user == null) {
068                                    user = UserLocalServiceUtil.getUserByScreenName(
069                                            companyId, screenName);
070                            }
071    
072                            credentials = new String[3];
073    
074                            credentials[0] = String.valueOf(user.getUserId());
075                            credentials[1] = user.getPassword();
076                            credentials[2] = Boolean.TRUE.toString();
077    
078                            return credentials;
079                    }
080                    catch (Exception e) {
081                            _log.error(e, e);
082                    }
083    
084                    return credentials;
085            }
086    
087            private static Log _log = LogFactoryUtil.getLog(
088                    RequestHeaderAutoLogin.class);
089    
090    }