001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.action;
022:
023: import com.liferay.portal.NoSuchUserException;
024: import com.liferay.portal.kernel.util.ParamUtil;
025: import com.liferay.portal.service.UserLocalServiceUtil;
026: import com.liferay.portal.struts.ActionConstants;
027: import com.liferay.portal.theme.ThemeDisplay;
028: import com.liferay.portal.util.OpenIdUtil;
029: import com.liferay.portal.util.PortalUtil;
030: import com.liferay.portal.util.WebKeys;
031: import com.liferay.util.servlet.SessionErrors;
032:
033: import java.util.List;
034:
035: import javax.servlet.http.HttpServletRequest;
036: import javax.servlet.http.HttpServletResponse;
037: import javax.servlet.http.HttpSession;
038: import javax.servlet.jsp.PageContext;
039:
040: import org.apache.struts.action.Action;
041: import org.apache.struts.action.ActionForm;
042: import org.apache.struts.action.ActionForward;
043: import org.apache.struts.action.ActionMapping;
044:
045: import org.openid4java.consumer.ConsumerException;
046: import org.openid4java.consumer.ConsumerManager;
047: import org.openid4java.discovery.DiscoveryException;
048: import org.openid4java.discovery.DiscoveryInformation;
049: import org.openid4java.message.AuthRequest;
050: import org.openid4java.message.MessageException;
051: import org.openid4java.message.ax.FetchRequest;
052: import org.openid4java.message.sreg.SRegRequest;
053:
054: /**
055: * <a href="OpenIdRequestAction.java.html"><b><i>View Source</i></b></a>
056: *
057: * @author Jorge Ferrer
058: *
059: */
060: public class OpenIdRequestAction extends Action {
061:
062: public static void sendOpenIdRequest(ThemeDisplay themeDisplay,
063: HttpServletRequest req, HttpServletResponse res,
064: String openId) throws Exception {
065:
066: if (!OpenIdUtil.isEnabled(themeDisplay.getCompanyId())) {
067: return;
068: }
069:
070: HttpSession ses = req.getSession();
071:
072: String returnURL = PortalUtil.getPortalURL(req)
073: + themeDisplay.getPathMain()
074: + "/portal/open_id_response";
075:
076: ConsumerManager manager = OpenIdUtil.getConsumerManager();
077:
078: List discoveries = manager.discover(openId);
079:
080: DiscoveryInformation discovered = manager
081: .associate(discoveries);
082:
083: ses.setAttribute(WebKeys.OPEN_ID_DISCO, discovered);
084:
085: AuthRequest authReq = manager.authenticate(discovered,
086: returnURL);
087:
088: String screenName = OpenIdUtil.getScreenName(openId);
089:
090: try {
091: UserLocalServiceUtil.getUserByScreenName(themeDisplay
092: .getCompanyId(), screenName);
093: } catch (NoSuchUserException nsue) {
094: FetchRequest fetch = FetchRequest.createFetchRequest();
095:
096: fetch.addAttribute("email",
097: "http://schema.openid.net/contact/email", true);
098: fetch.addAttribute("firstName",
099: "http://schema.openid.net/namePerson/first", true);
100: fetch.addAttribute("lastName",
101: "http://schema.openid.net/namePerson/last", true);
102:
103: authReq.addExtension(fetch);
104:
105: SRegRequest sregReq = SRegRequest.createFetchRequest();
106:
107: sregReq.addAttribute("fullname", true);
108: sregReq.addAttribute("email", true);
109:
110: authReq.addExtension(sregReq);
111: }
112:
113: res.sendRedirect(authReq.getDestinationUrl(true));
114: }
115:
116: public ActionForward execute(ActionMapping mapping,
117: ActionForm form, HttpServletRequest req,
118: HttpServletResponse res) throws Exception {
119:
120: ThemeDisplay themeDisplay = (ThemeDisplay) req
121: .getAttribute(WebKeys.THEME_DISPLAY);
122:
123: if (!OpenIdUtil.isEnabled(themeDisplay.getCompanyId())) {
124: return null;
125: }
126:
127: try {
128: String openId = ParamUtil.getString(req, "openId");
129:
130: sendOpenIdRequest(themeDisplay, req, res, openId);
131: } catch (Exception e) {
132: if (e instanceof ConsumerException
133: || e instanceof DiscoveryException
134: || e instanceof MessageException) {
135:
136: SessionErrors.add(req, e.getClass().getName());
137:
138: return mapping.findForward("portal.login");
139: } else {
140: req.setAttribute(PageContext.EXCEPTION, e);
141:
142: return mapping
143: .findForward(ActionConstants.COMMON_ERROR);
144: }
145: }
146:
147: return mapping.findForward("portal.login");
148: }
149:
150: }
|