1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.action;
16  
17  import com.liferay.portal.NoSuchUserException;
18  import com.liferay.portal.kernel.servlet.SessionErrors;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.model.User;
21  import com.liferay.portal.service.UserLocalServiceUtil;
22  import com.liferay.portal.theme.ThemeDisplay;
23  import com.liferay.portal.util.OpenIdUtil;
24  import com.liferay.portal.util.PortalUtil;
25  import com.liferay.portal.util.WebKeys;
26  
27  import java.util.List;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import javax.servlet.http.HttpSession;
32  
33  import org.apache.struts.action.Action;
34  import org.apache.struts.action.ActionForm;
35  import org.apache.struts.action.ActionForward;
36  import org.apache.struts.action.ActionMapping;
37  
38  import org.openid4java.consumer.ConsumerException;
39  import org.openid4java.consumer.ConsumerManager;
40  import org.openid4java.discovery.DiscoveryException;
41  import org.openid4java.discovery.DiscoveryInformation;
42  import org.openid4java.message.AuthRequest;
43  import org.openid4java.message.MessageException;
44  import org.openid4java.message.ax.FetchRequest;
45  import org.openid4java.message.sreg.SRegRequest;
46  
47  /**
48   * <a href="OpenIdRequestAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Jorge Ferrer
51   */
52  public class OpenIdRequestAction extends Action {
53  
54      public static void sendOpenIdRequest(
55              ThemeDisplay themeDisplay, HttpServletRequest request,
56              HttpServletResponse response, String openId)
57          throws Exception {
58  
59          if (!OpenIdUtil.isEnabled(themeDisplay.getCompanyId())) {
60              return;
61          }
62  
63          HttpSession session = request.getSession();
64  
65          String returnURL =
66              PortalUtil.getPortalURL(request) + themeDisplay.getPathMain() +
67                  "/portal/open_id_response";
68  
69          ConsumerManager manager = OpenIdUtil.getConsumerManager();
70  
71          List<DiscoveryInformation> discoveries = manager.discover(openId);
72  
73          DiscoveryInformation discovered = manager.associate(discoveries);
74  
75          session.setAttribute(WebKeys.OPEN_ID_DISCO, discovered);
76  
77          AuthRequest authRequest = manager.authenticate(discovered, returnURL);
78  
79          try {
80              UserLocalServiceUtil.getUserByOpenId(openId);
81          }
82          catch (NoSuchUserException nsue1) {
83              String screenName = OpenIdUtil.getScreenName(openId);
84  
85              try {
86                  User user = UserLocalServiceUtil.getUserByScreenName(
87                      themeDisplay.getCompanyId(), screenName);
88  
89                  UserLocalServiceUtil.updateOpenId(user.getUserId(), openId);
90              }
91              catch (NoSuchUserException nsue2) {
92                  FetchRequest fetch = FetchRequest.createFetchRequest();
93  
94                  fetch.addAttribute(
95                      "email", "http://schema.openid.net/contact/email", true);
96                  fetch.addAttribute(
97                      "firstName", "http://schema.openid.net/namePerson/first",
98                      true);
99                  fetch.addAttribute(
100                     "lastName", "http://schema.openid.net/namePerson/last",
101                     true);
102 
103                 authRequest.addExtension(fetch);
104 
105                 SRegRequest sregRequest = SRegRequest.createFetchRequest();
106 
107                 sregRequest.addAttribute("fullname", true);
108                 sregRequest.addAttribute("email", true);
109 
110                 authRequest.addExtension(sregRequest);
111             }
112         }
113 
114         response.sendRedirect(authRequest.getDestinationUrl(true));
115     }
116 
117     public ActionForward execute(
118             ActionMapping mapping, ActionForm form, HttpServletRequest request,
119             HttpServletResponse response)
120         throws Exception {
121 
122         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
123             WebKeys.THEME_DISPLAY);
124 
125         if (!OpenIdUtil.isEnabled(themeDisplay.getCompanyId())) {
126             return null;
127         }
128 
129         try {
130             String openId = ParamUtil.getString(request, "openId");
131 
132             sendOpenIdRequest(themeDisplay, request, response, openId);
133         }
134         catch (Exception e) {
135             if (e instanceof ConsumerException ||
136                 e instanceof DiscoveryException ||
137                 e instanceof MessageException) {
138 
139                 SessionErrors.add(request, e.getClass().getName());
140 
141                 return mapping.findForward("portal.login");
142             }
143             else {
144                 PortalUtil.sendError(e, request, response);
145 
146                 return null;
147             }
148         }
149 
150         return mapping.findForward("portal.login");
151     }
152 
153 }