1   /**
2    * Copyright (c) 2000-2008 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   */
61  public class OpenIdRequestAction extends Action {
62  
63      public static void sendOpenIdRequest(
64              ThemeDisplay themeDisplay, HttpServletRequest request,
65              HttpServletResponse response, String openId)
66          throws Exception {
67  
68          if (!OpenIdUtil.isEnabled(themeDisplay.getCompanyId())) {
69              return;
70          }
71  
72          HttpSession session = request.getSession();
73  
74          String returnURL =
75              PortalUtil.getPortalURL(request) + themeDisplay.getPathMain() +
76                  "/portal/open_id_response";
77  
78          ConsumerManager manager = OpenIdUtil.getConsumerManager();
79  
80          List<DiscoveryInformation> discoveries = manager.discover(openId);
81  
82          DiscoveryInformation discovered = manager.associate(discoveries);
83  
84          session.setAttribute(WebKeys.OPEN_ID_DISCO, discovered);
85  
86          AuthRequest authRequest = manager.authenticate(discovered, returnURL);
87  
88          try {
89              UserLocalServiceUtil.getUserByOpenId(openId);
90          }
91          catch (NoSuchUserException nsue1) {
92              String screenName = OpenIdUtil.getScreenName(openId);
93  
94              try {
95                  User user = UserLocalServiceUtil.getUserByScreenName(
96                      themeDisplay.getCompanyId(), screenName);
97  
98                  UserLocalServiceUtil.updateOpenId(user.getUserId(), openId);
99              }
100             catch (NoSuchUserException nsue2) {
101                 FetchRequest fetch = FetchRequest.createFetchRequest();
102 
103                 fetch.addAttribute(
104                     "email", "http://schema.openid.net/contact/email", true);
105                 fetch.addAttribute(
106                     "firstName", "http://schema.openid.net/namePerson/first",
107                     true);
108                 fetch.addAttribute(
109                     "lastName", "http://schema.openid.net/namePerson/last",
110                     true);
111 
112                 authRequest.addExtension(fetch);
113 
114                 SRegRequest sregRequest = SRegRequest.createFetchRequest();
115 
116                 sregRequest.addAttribute("fullname", true);
117                 sregRequest.addAttribute("email", true);
118 
119                 authRequest.addExtension(sregRequest);
120             }
121         }
122 
123         response.sendRedirect(authRequest.getDestinationUrl(true));
124     }
125 
126     public ActionForward execute(
127             ActionMapping mapping, ActionForm form, HttpServletRequest request,
128             HttpServletResponse response)
129         throws Exception {
130 
131         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
132             WebKeys.THEME_DISPLAY);
133 
134         if (!OpenIdUtil.isEnabled(themeDisplay.getCompanyId())) {
135             return null;
136         }
137 
138         try {
139             String openId = ParamUtil.getString(request, "openId");
140 
141             sendOpenIdRequest(themeDisplay, request, response, openId);
142         }
143         catch (Exception e) {
144             if (e instanceof ConsumerException ||
145                 e instanceof DiscoveryException ||
146                 e instanceof MessageException) {
147 
148                 SessionErrors.add(request, e.getClass().getName());
149 
150                 return mapping.findForward("portal.login");
151             }
152             else {
153                 PortalUtil.sendError(e, request, response);
154 
155                 return null;
156             }
157         }
158 
159         return mapping.findForward("portal.login");
160     }
161 
162 }