Search in sources :

Example 1 with AuthDataGenerator

use of org.datatransferproject.spi.api.auth.AuthDataGenerator in project data-transfer-project by google.

the class AuthTestDriver method getOAuthTokenCode.

/**
 * Performs an OAuth flow using the general AuthDataGenerator, returning a token.
 *
 * @return the token
 */
public TokenAuthData getOAuthTokenCode() throws Exception {
    OkHttpClient client = TestHelper.createTestBuilder(callbackHost).build();
    AuthDataGenerator authDataGenerator = new MicrosoftAuthServiceExtension().getAuthDataGenerator("CONTACTS", AuthMode.EXPORT);
    AuthFlowConfiguration configuration = authDataGenerator.generateConfiguration(callbackBase, "1");
    Desktop desktop = Desktop.getDesktop();
    desktop.browse(new URI(configuration.getAuthUrl()));
    // Execute the request and retrieve the auth code.
    String authCode = retrieveAuthCode(client);
    // get the token
    TokenAuthData tokenData = (TokenAuthData) authDataGenerator.generateAuthData(callbackBase, authCode, "1", configuration.getInitialAuthData(), null);
    // System.out.println("TOKEN: " + tokenData.getToken());
    return tokenData;
}
Also used : MicrosoftAuthServiceExtension(org.datatransferproject.auth.microsoft.MicrosoftAuthServiceExtension) AuthFlowConfiguration(org.datatransferproject.spi.api.types.AuthFlowConfiguration) AuthDataGenerator(org.datatransferproject.spi.api.auth.AuthDataGenerator) OkHttpClient(okhttp3.OkHttpClient) Desktop(java.awt.Desktop) TokenAuthData(org.datatransferproject.types.transfer.auth.TokenAuthData) URI(java.net.URI)

Example 2 with AuthDataGenerator

use of org.datatransferproject.spi.api.auth.AuthDataGenerator in project data-transfer-project by google.

the class CreateTransferJobAction method handle.

@Override
public TransferJob handle(CreateTransferJob request) {
    String dataType = request.getDataType();
    String exportService = request.getExportService();
    String importService = request.getImportService();
    Optional<ExportInformation> exportInformation = Optional.ofNullable(request.getExportInformation());
    String exportCallbackUrl = request.getExportCallbackUrl();
    String importCallbackUrl = request.getImportCallbackUrl();
    // Create a new job and persist
    UUID jobId = UUID.randomUUID();
    SecretKey sessionKey = symmetricKeyGenerator.generate();
    String encodedSessionKey = BaseEncoding.base64Url().encode(sessionKey.getEncoded());
    String encryptionScheme = request.getEncryptionScheme();
    PortabilityJob job;
    try {
        job = createJob(encodedSessionKey, dataType, exportService, importService, exportInformation, encryptionScheme);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    AuthDataGenerator exportGenerator = registry.getAuthDataGenerator(job.exportService(), job.transferDataType(), EXPORT);
    Preconditions.checkNotNull(exportGenerator, "Generator not found for type: %s, service: %s", job.transferDataType(), job.exportService());
    AuthDataGenerator importGenerator = registry.getAuthDataGenerator(job.importService(), job.transferDataType(), IMPORT);
    Preconditions.checkNotNull(importGenerator, "Generator not found for type: %s, service: %s", job.transferDataType(), job.importService());
    try {
        String encodedJobId = encodeJobId(jobId);
        AuthFlowConfiguration exportConfiguration = exportGenerator.generateConfiguration(exportCallbackUrl, encodedJobId);
        AuthFlowConfiguration importConfiguration = importGenerator.generateConfiguration(importCallbackUrl, encodedJobId);
        job = setInitialAuthDataOnJob(sessionKey, job, exportConfiguration, importConfiguration);
        jobStore.createJob(jobId, job);
        monitor.debug(() -> format("Created new transfer of type '%s' from '%s' to '%s' with jobId: %s", dataType, exportService, importService, jobId), jobId, EventCode.API_JOB_CREATED);
        return new TransferJob(encodedJobId, job.exportService(), job.importService(), job.transferDataType(), exportConfiguration.getAuthUrl(), importConfiguration.getAuthUrl(), exportConfiguration.getTokenUrl(), importConfiguration.getTokenUrl(), exportConfiguration.getAuthProtocol(), importConfiguration.getAuthProtocol());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : AuthFlowConfiguration(org.datatransferproject.spi.api.types.AuthFlowConfiguration) ExportInformation(org.datatransferproject.types.common.ExportInformation) PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob) AuthDataGenerator(org.datatransferproject.spi.api.auth.AuthDataGenerator) SecretKey(javax.crypto.SecretKey) IOException(java.io.IOException) UUID(java.util.UUID) TransferJob(org.datatransferproject.types.client.transfer.TransferJob) CreateTransferJob(org.datatransferproject.types.client.transfer.CreateTransferJob)

Example 3 with AuthDataGenerator

use of org.datatransferproject.spi.api.auth.AuthDataGenerator in project data-transfer-project by google.

the class PortabilityAuthServiceExtensionRegistryTest method testGetAuthDataGenerator.

@Test
public void testGetAuthDataGenerator() {
    List<String> supportedServiceTypes = ImmutableList.of("PHOTOS", "CONTACTS");
    AuthServiceExtension mockAuthProvider = getMockedAuthProvider(supportedServiceTypes, supportedServiceTypes, "mockAuthProvider");
    when(mockAuthProvider.getAuthDataGenerator("CONTACTS", AuthServiceProviderRegistry.AuthMode.EXPORT)).thenReturn(mock(AuthDataGenerator.class));
    AuthServiceProviderRegistry registry = new PortabilityAuthServiceProviderRegistry(ImmutableMap.of("mockServiceProvider", mockAuthProvider));
    AuthDataGenerator actual = registry.getAuthDataGenerator("mockServiceProvider", "CONTACTS", AuthServiceProviderRegistry.AuthMode.EXPORT);
    Assert.assertNotNull(actual);
}
Also used : AuthDataGenerator(org.datatransferproject.spi.api.auth.AuthDataGenerator) AuthServiceProviderRegistry(org.datatransferproject.spi.api.auth.AuthServiceProviderRegistry) AuthServiceExtension(org.datatransferproject.spi.api.auth.extension.AuthServiceExtension) Test(org.junit.Test)

Example 4 with AuthDataGenerator

use of org.datatransferproject.spi.api.auth.AuthDataGenerator in project data-transfer-project by google.

the class GenerateServiceAuthDataAction method handle.

public ServiceAuthData handle(GenerateServiceAuthData request) {
    try {
        String id = request.getId();
        Preconditions.checkNotNull(id, "transfer job ID required for GenerateServiceAuthDataAction");
        UUID jobId = decodeJobId(id);
        Preconditions.checkNotNull(request.getAuthToken(), "Auth token required for GenerateServiceAuthDataAction, transfer job ID: %s", jobId);
        PortabilityJob job = jobStore.findJob(jobId);
        Preconditions.checkNotNull(job, "existing job not found for transfer job ID: %s", jobId);
        // TODO: Determine service from job or from authUrl path?
        AuthMode authMode = GenerateServiceAuthData.Mode.EXPORT == request.getMode() ? AuthMode.EXPORT : AuthMode.IMPORT;
        String service = (authMode == AuthMode.EXPORT) ? job.exportService() : job.importService();
        AuthDataGenerator generator = registry.getAuthDataGenerator(service, job.transferDataType(), authMode);
        // Obtain the session key for this job
        String encodedSessionKey = job.jobAuthorization().sessionSecretKey();
        SecretKey key = symmetricKeyGenerator.parse(BaseEncoding.base64Url().decode(encodedSessionKey));
        // Retrieve initial auth data, if it existed
        AuthData initialAuthData = null;
        String encryptedInitialAuthData = (authMode == AuthMode.EXPORT) ? job.jobAuthorization().encryptedInitialExportAuthData() : job.jobAuthorization().encryptedInitialImportAuthData();
        if (encryptedInitialAuthData != null) {
            // Retrieve and parse the session key from the job
            // Decrypt and deserialize the object
            String serialized = decrypterFactory.create(key).decrypt(encryptedInitialAuthData);
            initialAuthData = objectMapper.readValue(serialized, AuthData.class);
        }
        // TODO: Use UUID instead of UUID.toString()
        // Generate auth data
        AuthData authData = generator.generateAuthData(request.getCallbackUrl(), request.getAuthToken(), jobId.toString(), initialAuthData, null);
        Preconditions.checkNotNull(authData, "Auth data should not be null");
        monitor.debug(() -> format("Generated auth data in mode '%s' for job: %s", authMode, jobId), jobId, EventCode.API_GENERATED_AUTH_DATA);
        // Serialize the auth data
        String serialized = objectMapper.writeValueAsString(authData);
        return new ServiceAuthData(serialized);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob) AuthDataGenerator(org.datatransferproject.spi.api.auth.AuthDataGenerator) SecretKey(javax.crypto.SecretKey) ServiceAuthData(org.datatransferproject.types.client.transfer.ServiceAuthData) GenerateServiceAuthData(org.datatransferproject.types.client.transfer.GenerateServiceAuthData) AuthData(org.datatransferproject.types.transfer.auth.AuthData) ServiceAuthData(org.datatransferproject.types.client.transfer.ServiceAuthData) GenerateServiceAuthData(org.datatransferproject.types.client.transfer.GenerateServiceAuthData) IOException(java.io.IOException) UUID(java.util.UUID) AuthMode(org.datatransferproject.spi.api.auth.AuthServiceProviderRegistry.AuthMode)

Aggregations

AuthDataGenerator (org.datatransferproject.spi.api.auth.AuthDataGenerator)4 IOException (java.io.IOException)2 UUID (java.util.UUID)2 SecretKey (javax.crypto.SecretKey)2 AuthFlowConfiguration (org.datatransferproject.spi.api.types.AuthFlowConfiguration)2 PortabilityJob (org.datatransferproject.spi.cloud.types.PortabilityJob)2 Desktop (java.awt.Desktop)1 URI (java.net.URI)1 OkHttpClient (okhttp3.OkHttpClient)1 MicrosoftAuthServiceExtension (org.datatransferproject.auth.microsoft.MicrosoftAuthServiceExtension)1 AuthServiceProviderRegistry (org.datatransferproject.spi.api.auth.AuthServiceProviderRegistry)1 AuthMode (org.datatransferproject.spi.api.auth.AuthServiceProviderRegistry.AuthMode)1 AuthServiceExtension (org.datatransferproject.spi.api.auth.extension.AuthServiceExtension)1 CreateTransferJob (org.datatransferproject.types.client.transfer.CreateTransferJob)1 GenerateServiceAuthData (org.datatransferproject.types.client.transfer.GenerateServiceAuthData)1 ServiceAuthData (org.datatransferproject.types.client.transfer.ServiceAuthData)1 TransferJob (org.datatransferproject.types.client.transfer.TransferJob)1 ExportInformation (org.datatransferproject.types.common.ExportInformation)1 AuthData (org.datatransferproject.types.transfer.auth.AuthData)1 TokenAuthData (org.datatransferproject.types.transfer.auth.TokenAuthData)1