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.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    }