001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nelson Silva 016 */ 017package org.nuxeo.ecm.webengine.oauth2; 018 019import java.io.IOException; 020import java.util.Map; 021import java.util.HashMap; 022 023import javax.servlet.http.HttpServletRequest; 024import javax.servlet.http.HttpServletResponse; 025import javax.ws.rs.GET; 026import javax.ws.rs.Path; 027import javax.ws.rs.PathParam; 028import javax.ws.rs.Produces; 029import javax.ws.rs.core.Context; 030import javax.ws.rs.core.Response; 031 032import com.google.api.client.auth.oauth2.Credential; 033import org.apache.commons.logging.Log; 034import org.apache.commons.logging.LogFactory; 035import org.nuxeo.ecm.core.api.NuxeoException; 036import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner; 037import org.nuxeo.ecm.platform.oauth2.providers.OAuth2ServiceProvider; 038import org.nuxeo.ecm.platform.oauth2.providers.OAuth2ServiceProviderRegistry; 039import org.nuxeo.ecm.webengine.model.WebObject; 040import org.nuxeo.ecm.webengine.model.impl.ModuleRoot; 041import org.nuxeo.runtime.api.Framework; 042 043/** 044 * WebEngine module to handle the OAuth2 callback 045 */ 046@Path("/oauth2") 047@Produces("text/html;charset=UTF-8") 048@WebObject(type = "oauth2") 049public class OAuth2Callback extends ModuleRoot { 050 051 @Context 052 private HttpServletRequest request; 053 054 Credential credential; 055 056 private static final Log log = LogFactory.getLog(OAuth2Callback.class); 057 058 /** 059 * @param serviceProviderName 060 * @return the rendered page. 061 */ 062 @GET 063 @Path("{serviceProviderName}/callback") 064 public Object doGet(@PathParam("serviceProviderName") String serviceProviderName) 065 throws IOException { 066 067 OAuth2ServiceProviderRegistry registry = Framework.getService(OAuth2ServiceProviderRegistry.class); 068 OAuth2ServiceProvider provider = registry.getProvider(serviceProviderName); 069 if (provider == null) { 070 return Response.status(HttpServletResponse.SC_NOT_FOUND).entity( 071 "No service provider called: \"" + serviceProviderName + "\".").build(); 072 } 073 074 Map<String, Object> args = new HashMap<>(); 075 076 new UnrestrictedSessionRunner(ctx.getCoreSession()) { 077 @Override 078 public void run() { 079 try { 080 credential = provider.handleAuthorizationCallback(request); 081 } catch (NuxeoException e) { 082 log.error("Authorization request failed", e); 083 args.put("error", "Authorization request failed"); 084 } 085 } 086 }.runUnrestricted(); 087 088 String token = (credential == null) ? "" : credential.getAccessToken(); 089 args.put("token", token); 090 return getView("index").args(args); 091 } 092}