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