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;
}
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);
}
}
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);
}
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);
}
}
Aggregations