Search in sources :

Example 1 with TokenAuthData

use of org.dataportabilityproject.types.transfer.auth.TokenAuthData in project data-transfer-project by google.

the class MicrosoftAuthDataGenerator method generateAuthData.

public TokenAuthData generateAuthData(String callbackBaseUrl, String authCode, String id, AuthData initialAuthData, String extra) {
    String redirectUrl = callbackBaseUrl + redirectPath;
    String params = constructTokenParams(authCode, redirectUrl, "user.read", "Contacts.ReadWrite");
    Request.Builder tokenReqBuilder = new Request.Builder().url(TOKEN_URL);
    tokenReqBuilder.header("Content-Type", "application/x-www-form-urlencoded");
    tokenReqBuilder.post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), params));
    try (Response tokenResponse = httpClient.newCall(tokenReqBuilder.build()).execute()) {
        ResponseBody tokenBody = tokenResponse.body();
        if (tokenBody == null) {
            // return type
            throw new RuntimeException("Token body was null:" + tokenResponse.code() + ":" + "<empty body>");
        }
        String token = new String(tokenBody.bytes());
        Map<String, String> map = mapper.reader().forType(Map.class).readValue(token);
        String accessToken = map.get("access_token");
        if (accessToken == null) {
            // return type
            throw new RuntimeException("Token body was null:" + tokenResponse.code() + ":" + "Not in params");
        }
        return new TokenAuthData(accessToken);
    } catch (IOException e) {
        // type
        throw new RuntimeException(e);
    }
}
Also used : Response(okhttp3.Response) TokenAuthData(org.dataportabilityproject.types.transfer.auth.TokenAuthData) Request(okhttp3.Request) IOException(java.io.IOException) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ResponseBody(okhttp3.ResponseBody)

Example 2 with TokenAuthData

use of org.dataportabilityproject.types.transfer.auth.TokenAuthData in project data-transfer-project by google.

the class LocalExportTestRunner method main.

@SuppressWarnings("unchecked")
public static void main(String... args) throws Exception {
    AuthTestDriver authTestDriver = new AuthTestDriver();
    MicrosoftTransferExtension serviceProvider = new MicrosoftTransferExtension();
    TokenAuthData token = authTestDriver.getOAuthTokenCode();
    Exporter<TokenAuthData, ContactsModelWrapper> contacts = (Exporter<TokenAuthData, ContactsModelWrapper>) serviceProvider.getExporter("contacts");
    ExportResult<ContactsModelWrapper> wrapper = contacts.export(UUID.randomUUID(), token);
}
Also used : TokenAuthData(org.dataportabilityproject.types.transfer.auth.TokenAuthData) MicrosoftTransferExtension(org.dataportabilityproject.transfer.microsoft.MicrosoftTransferExtension) ContactsModelWrapper(org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper) AuthTestDriver(org.dataportabilityproject.auth.microsoft.harness.AuthTestDriver) Exporter(org.dataportabilityproject.spi.transfer.provider.Exporter)

Example 3 with TokenAuthData

use of org.dataportabilityproject.types.transfer.auth.TokenAuthData in project data-transfer-project by google.

the class MicrosoftCalendarExportTest method setUp.

@Before
public void setUp() {
    client = new OkHttpClient.Builder().build();
    mapper = new ObjectMapper();
    transformerService = new TransformerServiceImpl();
    token = new TokenAuthData("token123");
    server = new MockWebServer();
}
Also used : TokenAuthData(org.dataportabilityproject.types.transfer.auth.TokenAuthData) MockWebServer(com.squareup.okhttp.mockwebserver.MockWebServer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TransformerServiceImpl(org.dataportabilityproject.transfer.microsoft.transformer.TransformerServiceImpl) Before(org.junit.Before)

Example 4 with TokenAuthData

use of org.dataportabilityproject.types.transfer.auth.TokenAuthData in project data-transfer-project by google.

the class AuthTestDriver method getOAuthTokenCode.

/**
 * Performs an OAuth flow using the MicrosoftAuthDataGenerator, returning a token.
 *
 * @return the token
 */
public TokenAuthData getOAuthTokenCode() throws Exception {
    OkHttpClient client = TestHelper.createTestBuilder(callbackHost).build();
    ObjectMapper mapper = new ObjectMapper();
    MicrosoftAuthDataGenerator dataGenerator = new MicrosoftAuthDataGenerator("/response", () -> clientId, () -> secret, client, mapper, "contacts", AuthMode.EXPORT);
    AuthFlowConfiguration configuration = dataGenerator.generateConfiguration(callbackBase, "1");
    Desktop desktop = Desktop.getDesktop();
    desktop.browse(new URI(configuration.getUrl()));
    // Execute the request and retrieve the auth code.
    String authCode = retrieveAuthCode(client);
    // get the token
    TokenAuthData tokenData = dataGenerator.generateAuthData(callbackBase, authCode, "1", configuration.getInitialAuthData(), null);
    // System.out.println("TOKEN: " + tokenData.getToken());
    return tokenData;
}
Also used : AuthFlowConfiguration(org.dataportabilityproject.spi.gateway.types.AuthFlowConfiguration) OkHttpClient(okhttp3.OkHttpClient) Desktop(java.awt.Desktop) TokenAuthData(org.dataportabilityproject.types.transfer.auth.TokenAuthData) MicrosoftAuthDataGenerator(org.dataportabilityproject.auth.microsoft.MicrosoftAuthDataGenerator) URI(java.net.URI) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 5 with TokenAuthData

use of org.dataportabilityproject.types.transfer.auth.TokenAuthData in project data-transfer-project by google.

the class LocalImportTestRunner method main.

@SuppressWarnings("unchecked")
public static void main(String... args) throws Exception {
    AuthTestDriver authTestDriver = new AuthTestDriver();
    MicrosoftTransferExtension serviceProvider = new MicrosoftTransferExtension();
    TokenAuthData token = authTestDriver.getOAuthTokenCode();
    Importer<TokenAuthData, ContactsModelWrapper> contacts = (Importer<TokenAuthData, ContactsModelWrapper>) serviceProvider.getImporter("contacts");
    ContactsModelWrapper wrapper = new ContactsModelWrapper(createCards());
    ImportResult result = contacts.importItem(UUID.randomUUID(), token, wrapper);
}
Also used : ImportResult(org.dataportabilityproject.spi.transfer.provider.ImportResult) TokenAuthData(org.dataportabilityproject.types.transfer.auth.TokenAuthData) MicrosoftTransferExtension(org.dataportabilityproject.transfer.microsoft.MicrosoftTransferExtension) ContactsModelWrapper(org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper) AuthTestDriver(org.dataportabilityproject.auth.microsoft.harness.AuthTestDriver) Importer(org.dataportabilityproject.spi.transfer.provider.Importer)

Aggregations

TokenAuthData (org.dataportabilityproject.types.transfer.auth.TokenAuthData)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 ContactsModelWrapper (org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper)3 MockWebServer (com.squareup.okhttp.mockwebserver.MockWebServer)2 IOException (java.io.IOException)2 Map (java.util.Map)2 OkHttpClient (okhttp3.OkHttpClient)2 AuthTestDriver (org.dataportabilityproject.auth.microsoft.harness.AuthTestDriver)2 ImportResult (org.dataportabilityproject.spi.transfer.provider.ImportResult)2 Importer (org.dataportabilityproject.spi.transfer.provider.Importer)2 MicrosoftTransferExtension (org.dataportabilityproject.transfer.microsoft.MicrosoftTransferExtension)2 TransformerServiceImpl (org.dataportabilityproject.transfer.microsoft.transformer.TransformerServiceImpl)2 Before (org.junit.Before)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 VCard (ezvcard.VCard)1 JCardReader (ezvcard.io.json.JCardReader)1 Desktop (java.awt.Desktop)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1