001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *      Nelson Silva
018 */
019package org.nuxeo.ecm.webengine.oauth2;
020
021import java.io.IOException;
022import java.util.Map;
023import java.util.HashMap;
024
025import javax.servlet.http.HttpServletRequest;
026import javax.servlet.http.HttpServletResponse;
027import javax.ws.rs.GET;
028import javax.ws.rs.Path;
029import javax.ws.rs.PathParam;
030import javax.ws.rs.Produces;
031import javax.ws.rs.core.Context;
032import javax.ws.rs.core.Response;
033
034import com.google.api.client.auth.oauth2.Credential;
035import org.apache.commons.logging.Log;
036import org.apache.commons.logging.LogFactory;
037import org.nuxeo.ecm.core.api.NuxeoException;
038import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
039import org.nuxeo.ecm.platform.oauth2.providers.OAuth2ServiceProvider;
040import org.nuxeo.ecm.platform.oauth2.providers.OAuth2ServiceProviderRegistry;
041import org.nuxeo.ecm.webengine.model.WebObject;
042import org.nuxeo.ecm.webengine.model.impl.ModuleRoot;
043import org.nuxeo.runtime.api.Framework;
044
045/**
046 * WebEngine module to handle the OAuth2 callback
047 */
048@Path("/oauth2")
049@Produces("text/html;charset=UTF-8")
050@WebObject(type = "oauth2")
051public class OAuth2Callback extends ModuleRoot {
052
053    @Context
054    private HttpServletRequest request;
055
056    Credential credential;
057
058    private static final Log log = LogFactory.getLog(OAuth2Callback.class);
059
060    /**
061     * @param serviceProviderName
062     * @return the rendered page.
063     */
064    @GET
065    @Path("{serviceProviderName}/callback")
066    public Object doGet(@PathParam("serviceProviderName") String serviceProviderName)
067            throws IOException {
068
069        OAuth2ServiceProviderRegistry registry = Framework.getService(OAuth2ServiceProviderRegistry.class);
070        OAuth2ServiceProvider provider = registry.getProvider(serviceProviderName);
071        if (provider == null) {
072            return Response.status(HttpServletResponse.SC_NOT_FOUND).entity(
073                    "No service provider called: \"" + serviceProviderName + "\".").build();
074        }
075
076        Map<String, Object> args = new HashMap<>();
077
078        new UnrestrictedSessionRunner(ctx.getCoreSession()) {
079            @Override
080            public void run() {
081                try {
082                    credential = provider.handleAuthorizationCallback(request);
083                } catch (NuxeoException e) {
084                    log.error("Authorization request failed", e);
085                    args.put("error", "Authorization request failed");
086                }
087            }
088        }.runUnrestricted();
089
090        String token = (credential == null) ? "" : credential.getAccessToken();
091        args.put("token", token);
092        return getView("index").args(args);
093    }
094}