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.util.ParamUtil;
27  import com.liferay.portal.service.UserLocalServiceUtil;
28  import com.liferay.portal.struts.ActionConstants;
29  import com.liferay.portal.theme.ThemeDisplay;
30  import com.liferay.portal.util.OpenIdUtil;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.util.servlet.SessionErrors;
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  import javax.servlet.jsp.PageContext;
41  
42  import org.apache.struts.action.Action;
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  import org.openid4java.consumer.ConsumerException;
48  import org.openid4java.consumer.ConsumerManager;
49  import org.openid4java.discovery.DiscoveryException;
50  import org.openid4java.discovery.DiscoveryInformation;
51  import org.openid4java.message.AuthRequest;
52  import org.openid4java.message.MessageException;
53  import org.openid4java.message.ax.FetchRequest;
54  import org.openid4java.message.sreg.SRegRequest;
55  
56  /**
57   * <a href="OpenIdRequestAction.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Jorge Ferrer
60   *
61   */
62  public class OpenIdRequestAction extends Action {
63  
64      public static void sendOpenIdRequest(
65              ThemeDisplay themeDisplay, HttpServletRequest req,
66              HttpServletResponse res, String openId)
67          throws Exception {
68  
69          if (!OpenIdUtil.isEnabled(themeDisplay.getCompanyId())) {
70              return;
71          }
72  
73          HttpSession ses = req.getSession();
74  
75          String returnURL =
76              PortalUtil.getPortalURL(req) + themeDisplay.getPathMain() +
77                  "/portal/open_id_response";
78  
79          ConsumerManager manager = OpenIdUtil.getConsumerManager();
80  
81          List<DiscoveryInformation> discoveries = manager.discover(openId);
82  
83          DiscoveryInformation discovered = manager.associate(discoveries);
84  
85          ses.setAttribute(WebKeys.OPEN_ID_DISCO, discovered);
86  
87          AuthRequest authReq = manager.authenticate(discovered, returnURL);
88  
89          String screenName = OpenIdUtil.getScreenName(openId);
90  
91          try {
92              UserLocalServiceUtil.getUserByScreenName(
93                  themeDisplay.getCompanyId(), screenName);
94          }
95          catch (NoSuchUserException nsue) {
96              FetchRequest fetch = FetchRequest.createFetchRequest();
97  
98              fetch.addAttribute(
99                  "email", "http://schema.openid.net/contact/email", true);
100             fetch.addAttribute(
101                 "firstName", "http://schema.openid.net/namePerson/first", true);
102             fetch.addAttribute(
103                 "lastName", "http://schema.openid.net/namePerson/last", true);
104 
105             authReq.addExtension(fetch);
106 
107             SRegRequest sregReq = SRegRequest.createFetchRequest();
108 
109             sregReq.addAttribute("fullname", true);
110             sregReq.addAttribute("email", true);
111 
112             authReq.addExtension(sregReq);
113         }
114 
115         res.sendRedirect(authReq.getDestinationUrl(true));
116     }
117 
118     public ActionForward execute(
119             ActionMapping mapping, ActionForm form, HttpServletRequest req,
120             HttpServletResponse res)
121         throws Exception {
122 
123         ThemeDisplay themeDisplay =
124             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
125 
126         if (!OpenIdUtil.isEnabled(themeDisplay.getCompanyId())) {
127             return null;
128         }
129 
130         try {
131             String openId = ParamUtil.getString(req, "openId");
132 
133             sendOpenIdRequest(themeDisplay, req, res, openId);
134         }
135         catch (Exception e) {
136             if (e instanceof ConsumerException ||
137                 e instanceof DiscoveryException ||
138                 e instanceof MessageException) {
139 
140                 SessionErrors.add(req, e.getClass().getName());
141 
142                 return mapping.findForward("portal.login");
143             }
144             else {
145                 req.setAttribute(PageContext.EXCEPTION, e);
146 
147                 return mapping.findForward(ActionConstants.COMMON_ERROR);
148             }
149         }
150 
151         return mapping.findForward("portal.login");
152     }
153 
154 }