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