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 * Andre Justo 016 */ 017package org.nuxeo.ecm.liveconnect.dropbox; 018 019import com.google.api.client.http.GenericUrl; 020import com.google.api.client.http.HttpRequest; 021import com.google.api.client.http.HttpRequestFactory; 022import com.google.api.client.http.HttpRequestInitializer; 023import com.google.api.client.http.HttpResponse; 024import com.google.api.client.json.GenericJson; 025import com.google.api.client.json.JsonObjectParser; 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.platform.oauth2.providers.AbstractOAuth2UserEmailProvider; 030import org.nuxeo.ecm.platform.oauth2.tokens.NuxeoOAuth2Token; 031 032import java.io.IOException; 033import java.io.Serializable; 034import java.util.HashMap; 035import java.util.List; 036import java.util.Map; 037 038/** 039 * @since 7.3 040 */ 041public class DropboxOAuth2ServiceProvider extends AbstractOAuth2UserEmailProvider { 042 043 protected static final Log log = LogFactory.getLog(DropboxOAuth2ServiceProvider.class); 044 045 private static final String ACCOUNT_INFO_URL = "https://api.dropbox.com/1/account/info"; 046 047 private static final HttpRequestFactory requestFactory = 048 HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() { 049 @Override 050 public void initialize(HttpRequest request) throws IOException { 051 request.setParser(new JsonObjectParser(JSON_FACTORY)); 052 } 053 }); 054 055 protected String getUserEmail(String accessToken) throws IOException { 056 GenericUrl url = new GenericUrl(ACCOUNT_INFO_URL); 057 url.set("access_token", accessToken); 058 059 HttpResponse response = requestFactory.buildGetRequest(url).execute(); 060 GenericJson json = response.parseAs(GenericJson.class); 061 return json.get("email").toString(); 062 } 063 064 public String getServiceUser(String username) { 065 Map<String, Serializable> filter = new HashMap<>(); 066 filter.put("serviceName", serviceName); 067 filter.put(NuxeoOAuth2Token.KEY_NUXEO_LOGIN, username); 068 List<DocumentModel> entries = getCredentialDataStore().query(filter); 069 if (entries == null || entries.size() == 0) { 070 return null; 071 } 072 if (entries.size() > 1) { 073 log.error("Found multiple " + serviceName + " accounts for " + username); 074 } 075 return (String) entries.get(0).getProperty(NuxeoOAuth2Token.SCHEMA, NuxeoOAuth2Token.KEY_SERVICE_LOGIN); 076 } 077 078 protected HttpRequestFactory getRequestFactory() { 079 return requestFactory; 080 } 081}