001    /**
002     * Copyright (c) 2000-2013 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            @Override
035            public String[] login(
036                    HttpServletRequest request, HttpServletResponse response) {
037    
038                    String[] credentials = null;
039    
040                    try {
041                            long companyId = PortalUtil.getCompanyId(request);
042    
043                            if (!OpenIdUtil.isEnabled(companyId)) {
044                                    return credentials;
045                            }
046    
047                            HttpSession session = request.getSession();
048    
049                            Long userId = (Long)session.getAttribute(WebKeys.OPEN_ID_LOGIN);
050    
051                            if (userId == null) {
052                                    return credentials;
053                            }
054    
055                            session.removeAttribute(WebKeys.OPEN_ID_LOGIN);
056    
057                            User user = UserLocalServiceUtil.getUserById(userId);
058    
059                            credentials = new String[3];
060    
061                            credentials[0] = String.valueOf(user.getUserId());
062                            credentials[1] = user.getPassword();
063                            credentials[2] = Boolean.TRUE.toString();
064                    }
065                    catch (Exception e) {
066                            _log.error(e, e);
067                    }
068    
069                    return credentials;
070            }
071    
072            private static Log _log = LogFactoryUtil.getLog(OpenIdAutoLogin.class);
073    
074    }