Search in sources :

Example 1 with ServiceAuthData

use of org.datatransferproject.types.client.transfer.ServiceAuthData 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

IOException (java.io.IOException)1 UUID (java.util.UUID)1 SecretKey (javax.crypto.SecretKey)1 AuthDataGenerator (org.datatransferproject.spi.api.auth.AuthDataGenerator)1 AuthMode (org.datatransferproject.spi.api.auth.AuthServiceProviderRegistry.AuthMode)1 PortabilityJob (org.datatransferproject.spi.cloud.types.PortabilityJob)1 GenerateServiceAuthData (org.datatransferproject.types.client.transfer.GenerateServiceAuthData)1 ServiceAuthData (org.datatransferproject.types.client.transfer.ServiceAuthData)1 AuthData (org.datatransferproject.types.transfer.auth.AuthData)1