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 * Vladimir Pasquier <vpasquier@nuxeo.com> 016 */ 017package org.nuxeo.salesforce; 018 019import java.io.IOException; 020import java.io.StringWriter; 021import java.util.HashMap; 022import java.util.Map; 023 024import javax.ws.rs.GET; 025import javax.ws.rs.Path; 026import javax.ws.rs.PathParam; 027import javax.ws.rs.Produces; 028 029import org.codehaus.jackson.map.ObjectMapper; 030import org.nuxeo.ecm.core.api.DocumentModel; 031import org.nuxeo.ecm.directory.Session; 032import org.nuxeo.ecm.directory.api.DirectoryService; 033import org.nuxeo.ecm.webengine.model.WebObject; 034import org.nuxeo.ecm.webengine.model.impl.DefaultObject; 035import org.nuxeo.runtime.api.Framework; 036 037/** 038 * Endpoint used to retrieve informations from OAuth2 Provider directory (except crucial ones). 039 * 040 * @since 7.4 041 */ 042@Path("/api/v1/oauth2directory") 043@Produces("application/json") 044@WebObject(type = "oauth2directory") 045public class OAuth2DirectoryObject extends DefaultObject { 046 047 public static final String OAUTH2CLIENT_DIRECTORY_NAME = "oauth2ServiceProviders"; 048 049 public static final String USER_AUTHORIZATION_URL = "userAuthorizationURL"; 050 051 public static final String CLIENT_ID = "clientId"; 052 053 @Override 054 protected void initialize(Object... args) { 055 super.initialize(args); 056 } 057 058 @GET 059 @Path("{serviceName}") 060 public Object doGetOAuth2ProviderInformations(@PathParam("serviceName") String serviceName) throws IOException { 061 DirectoryService service = Framework.getLocalService(DirectoryService.class); 062 try (Session session = service.open(OAUTH2CLIENT_DIRECTORY_NAME)) { 063 for (DocumentModel entry : session.getEntries()) { 064 String name = (String) entry.getPropertyValue("oauth2ServiceProvider:serviceName"); 065 if (serviceName.equals(name)) { 066 ObjectMapper mapper = new ObjectMapper(); 067 StringWriter writer = new StringWriter(); 068 Map<String, String> values = new HashMap<>(); 069 values.put(CLIENT_ID, (String) entry.getProperty("oauth2ServiceProvider", CLIENT_ID)); 070 values.put(USER_AUTHORIZATION_URL, 071 (String) entry.getProperty("oauth2ServiceProvider" + "", USER_AUTHORIZATION_URL)); 072 mapper.writeValue(writer, values); 073 return writer.toString(); 074 } 075 } 076 } 077 return null; 078 } 079 080}