1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.action;
24  
25  import com.liferay.portal.NoSuchUserException;
26  import com.liferay.portal.kernel.servlet.SessionErrors;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.service.UserLocalServiceUtil;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.OpenIdUtil;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portal.util.WebKeys;
34  
35  import java.util.List;
36  
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  import javax.servlet.http.HttpSession;
40  
41  import org.apache.struts.action.Action;
42  import org.apache.struts.action.ActionForm;
43  import org.apache.struts.action.ActionForward;
44  import org.apache.struts.action.ActionMapping;
45  
46  import org.openid4java.consumer.ConsumerException;
47  import org.openid4java.consumer.ConsumerManager;
48  import org.openid4java.discovery.DiscoveryException;
49  import org.openid4java.discovery.DiscoveryInformation;
50  import org.openid4java.message.AuthRequest;
51  import org.openid4java.message.MessageException;
52  import org.openid4java.message.ax.FetchRequest;
53  import org.openid4java.message.sreg.SRegRequest;
54  
55  /**
56   * <a href="OpenIdRequestAction.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Jorge Ferrer
59   */
60  public class OpenIdRequestAction extends Action {
61  
62      public static void sendOpenIdRequest(
63              ThemeDisplay themeDisplay, HttpServletRequest request,
64              HttpServletResponse response, String openId)
65          throws Exception {
66  
67          if (!OpenIdUtil.isEnabled(themeDisplay.getCompanyId())) {
68              return;
69          }
70  
71          HttpSession session = request.getSession();
72  
73          String returnURL =
74              PortalUtil.getPortalURL(request) + themeDisplay.getPathMain() +
75                  "/portal/open_id_response";
76  
77          ConsumerManager manager = OpenIdUtil.getConsumerManager();
78  
79          List<DiscoveryInformation> discoveries = manager.discover(openId);
80  
81          DiscoveryInformation discovered = manager.associate(discoveries);
82  
83          session.setAttribute(WebKeys.OPEN_ID_DISCO, discovered);
84  
85          AuthRequest authRequest = manager.authenticate(discovered, returnURL);
86  
87          try {
88              UserLocalServiceUtil.getUserByOpenId(openId);
89          }
90          catch (NoSuchUserException nsue1) {
91              String screenName = OpenIdUtil.getScreenName(openId);
92  
93              try {
94                  User user = UserLocalServiceUtil.getUserByScreenName(
95                      themeDisplay.getCompanyId(), screenName);
96  
97                  UserLocalServiceUtil.updateOpenId(user.getUserId(), openId);
98              }
99              catch (NoSuchUserException nsue2) {
100                 FetchRequest fetch = FetchRequest.createFetchRequest();
101 
102                 fetch.addAttribute(
103                     "email", "http://schema.openid.net/contact/email", true);
104                 fetch.addAttribute(
105                     "firstName", "http://schema.openid.net/namePerson/first",
106                     true);
107                 fetch.addAttribute(
108                     "lastName", "http://schema.openid.net/namePerson/last",
109                     true);
110 
111                 authRequest.addExtension(fetch);
112 
113                 SRegRequest sregRequest = SRegRequest.createFetchRequest();
114 
115                 sregRequest.addAttribute("fullname", true);
116                 sregRequest.addAttribute("email", true);
117 
118                 authRequest.addExtension(sregRequest);
119             }
120         }
121 
122         response.sendRedirect(authRequest.getDestinationUrl(true));
123     }
124 
125     public ActionForward execute(
126             ActionMapping mapping, ActionForm form, HttpServletRequest request,
127             HttpServletResponse response)
128         throws Exception {
129 
130         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
131             WebKeys.THEME_DISPLAY);
132 
133         if (!OpenIdUtil.isEnabled(themeDisplay.getCompanyId())) {
134             return null;
135         }
136 
137         try {
138             String openId = ParamUtil.getString(request, "openId");
139 
140             sendOpenIdRequest(themeDisplay, request, response, openId);
141         }
142         catch (Exception e) {
143             if (e instanceof ConsumerException ||
144                 e instanceof DiscoveryException ||
145                 e instanceof MessageException) {
146 
147                 SessionErrors.add(request, e.getClass().getName());
148 
149                 return mapping.findForward("portal.login");
150             }
151             else {
152                 PortalUtil.sendError(e, request, response);
153 
154                 return null;
155             }
156         }
157 
158         return mapping.findForward("portal.login");
159     }
160 
161 }