use of org.datatransferproject.spi.api.auth.AuthServiceProviderRegistry.AuthMode 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