001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.model.User;
020    import com.liferay.portal.service.UserLocalServiceUtil;
021    import com.liferay.portal.util.OpenIdUtil;
022    import com.liferay.portal.util.PortalUtil;
023    import com.liferay.portal.util.WebKeys;
024    
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    import javax.servlet.http.HttpSession;
028    
029    /**
030     * @author Jorge Ferrer
031     */
032    public class OpenIdAutoLogin implements AutoLogin {
033    
034            public String[] login(
035                    HttpServletRequest request, HttpServletResponse response) {
036    
037                    String[] credentials = null;
038    
039                    try {
040                            long companyId = PortalUtil.getCompanyId(request);
041    
042                            if (!OpenIdUtil.isEnabled(companyId)) {
043                                    return credentials;
044                            }
045    
046                            HttpSession session = request.getSession();
047    
048                            Long userId = (Long)session.getAttribute(WebKeys.OPEN_ID_LOGIN);
049    
050                            if (userId == null) {
051                                    return credentials;
052                            }
053    
054                            session.removeAttribute(WebKeys.OPEN_ID_LOGIN);
055    
056                            User user = UserLocalServiceUtil.getUserById(userId);
057    
058                            credentials = new String[3];
059    
060                            credentials[0] = String.valueOf(user.getUserId());
061                            credentials[1] = user.getPassword();
062                            credentials[2] = Boolean.TRUE.toString();
063                    }
064                    catch (Exception e) {
065                            _log.error(e, e);
066                    }
067    
068                    return credentials;
069            }
070    
071            private static Log _log = LogFactoryUtil.getLog(OpenIdAutoLogin.class);
072    
073    }