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.PortalException;
018    import com.liferay.portal.kernel.facebook.FacebookConnectUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.service.UserLocalServiceUtil;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portal.util.WebKeys;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletResponse;
028    import javax.servlet.http.HttpSession;
029    
030    /**
031     * @author Wilson Man
032     */
033    public class FacebookAutoLogin extends BaseAutoLogin {
034    
035            @Override
036            protected String[] doLogin(
037                            HttpServletRequest request, HttpServletResponse response)
038                    throws Exception {
039    
040                    long companyId = PortalUtil.getCompanyId(request);
041    
042                    if (!FacebookConnectUtil.isEnabled(companyId)) {
043                            return null;
044                    }
045    
046                    User user = getUser(request, companyId);
047    
048                    if (user == null) {
049                            return null;
050                    }
051    
052                    String[] credentials = new String[3];
053    
054                    credentials[0] = String.valueOf(user.getUserId());
055                    credentials[1] = user.getPassword();
056                    credentials[2] = Boolean.FALSE.toString();
057    
058                    return credentials;
059            }
060    
061            protected User getUser(HttpServletRequest request, long companyId)
062                    throws PortalException {
063    
064                    HttpSession session = request.getSession();
065    
066                    String emailAddress = (String)session.getAttribute(
067                            WebKeys.FACEBOOK_USER_EMAIL_ADDRESS);
068    
069                    if (Validator.isNotNull(emailAddress)) {
070                            session.removeAttribute(WebKeys.FACEBOOK_USER_EMAIL_ADDRESS);
071    
072                            return UserLocalServiceUtil.getUserByEmailAddress(
073                                    companyId, emailAddress);
074                    }
075                    else {
076                            long facebookId = GetterUtil.getLong(
077                                    (String)session.getAttribute(WebKeys.FACEBOOK_USER_ID));
078    
079                            if (facebookId > 0) {
080                                    return UserLocalServiceUtil.getUserByFacebookId(
081                                            companyId, facebookId);
082                            }
083                    }
084    
085                    return null;
086            }
087    
088    }