use of org.dataportabilityproject.types.client.transfer.DataTransferResponse in project data-transfer-project by google.
the class SetupHandler method handle.
@Override
public void handle(HttpExchange exchange) throws IOException {
try {
logger.debug("Entering setup handler, exchange: {}", exchange);
Preconditions.checkArgument(ReferenceApiUtils.validateRequest(exchange, HttpMethods.GET, handlerUrlPath));
String encodedIdCookie = ReferenceApiUtils.getCookie(exchange.getRequestHeaders(), JsonKeys.ID_COOKIE_KEY);
Preconditions.checkArgument(!Strings.isNullOrEmpty(encodedIdCookie), "Encoded Id cookie required");
// Valid job must be present
UUID jobId = ReferenceApiUtils.decodeJobId(encodedIdCookie);
PortabilityJob job = store.findJob(jobId);
Preconditions.checkNotNull(job, "existing job not found for jobId: %s", jobId);
// This page is only valid after the oauth of the export service - export data should exist
// for all setup Modes.
String exportService = job.exportService();
Preconditions.checkState(!Strings.isNullOrEmpty(exportService), "Export service is invalid");
String importService = job.importService();
Preconditions.checkState(!Strings.isNullOrEmpty(importService), "Import service is invalid");
DataTransferResponse response;
if (mode == Mode.IMPORT) {
response = handleImportSetup(exchange.getRequestHeaders(), job, jobId);
} else {
response = handleCopySetup(exchange.getRequestHeaders(), job, jobId);
// Valid job is present, generate an XSRF token to pass back via cookie
String tokenStr = tokenManager.createNewToken(jobId);
HttpCookie token = new HttpCookie(JsonKeys.XSRF_TOKEN, tokenStr);
exchange.getResponseHeaders().add(HttpHeaders.SET_COOKIE, token.toString() + ReferenceApiUtils.COOKIE_ATTRIBUTES);
}
// Mark the response as type Json and send
exchange.getResponseHeaders().set(CONTENT_TYPE, "application/json; charset=" + StandardCharsets.UTF_8.name());
exchange.sendResponseHeaders(200, 0);
objectMapper.writeValue(exchange.getResponseBody(), response);
} catch (Exception e) {
logger.error("Error handling request", e);
throw e;
}
}
use of org.dataportabilityproject.types.client.transfer.DataTransferResponse in project data-transfer-project by google.
the class SimpleLoginSubmitHandler method handle.
public void handle(HttpExchange exchange) throws IOException {
Preconditions.checkArgument(ReferenceApiUtils.validateRequest(exchange, HttpMethods.POST, PATH));
logger.debug("received request: {}", exchange.getRequestURI());
DataTransferResponse response = handleExchange(exchange);
logger.debug("simpleLoginSubmit, redirecting to: {}", response.getNextUrl());
// Mark the response as type Json and send
exchange.getResponseHeaders().set(HttpHeaders.CONTENT_TYPE, "application/json; charset=" + StandardCharsets.UTF_8.name());
exchange.sendResponseHeaders(200, 0);
objectMapper.writeValue(exchange.getResponseBody(), response);
}
use of org.dataportabilityproject.types.client.transfer.DataTransferResponse in project data-transfer-project by google.
the class SimpleLoginSubmitHandler method handleExchange.
DataTransferResponse handleExchange(HttpExchange exchange) throws IOException {
Headers requestHeaders = exchange.getRequestHeaders();
try {
SimpleLoginRequest request = objectMapper.readValue(exchange.getRequestBody(), SimpleLoginRequest.class);
String encodedIdCookie = ReferenceApiUtils.getCookie(requestHeaders, JsonKeys.ID_COOKIE_KEY);
Preconditions.checkArgument(!Strings.isNullOrEmpty(encodedIdCookie), "Missing encodedIdCookie");
// Valid job must be present
Preconditions.checkArgument(!Strings.isNullOrEmpty(encodedIdCookie), "Encoded Id cookie required");
UUID jobId = ReferenceApiUtils.decodeJobId(encodedIdCookie);
PortabilityJob job = store.findJob(jobId);
Preconditions.checkNotNull(job, "existing job not found for jobId: %s", jobId);
// TODO: Determine service from job or from authUrl path?
AuthMode authMode = ReferenceApiUtils.getAuthMode(exchange.getRequestHeaders());
// TODO: Determine service from job or from authUrl path?
String service = (authMode == AuthMode.EXPORT) ? job.exportService() : job.importService();
Preconditions.checkState(!Strings.isNullOrEmpty(service), "service not found, service: %s authMode: %s, jobId: %s", service, authMode, jobId);
Preconditions.checkArgument(!Strings.isNullOrEmpty(request.getUsername()), "Missing valid username");
Preconditions.checkArgument(!Strings.isNullOrEmpty(request.getPassword()), "Missing password");
AuthDataGenerator generator = registry.getAuthDataGenerator(service, job.transferDataType(), AuthMode.EXPORT);
Preconditions.checkNotNull(generator, "Generator not found for type: %s, service: %s", job.transferDataType(), service);
// TODO: change signature to pass UUID
// Generate and store auth data
AuthData authData = generator.generateAuthData(baseApiUrl, request.getUsername(), jobId.toString(), null, request.getPassword());
Preconditions.checkNotNull(authData, "Auth data should not be null");
// Obtain the session key for this job
String encodedSessionKey = job.jobAuthorization().sessionSecretKey();
SecretKey key = symmetricKeyGenerator.parse(BaseEncoding.base64Url().decode(encodedSessionKey));
// Serialize and encrypt the auth data
String serialized = objectMapper.writeValueAsString(authData);
String encryptedAuthData = EncrypterFactory.create(key).encrypt(serialized);
// Set new cookie
ReferenceApiUtils.setCookie(exchange.getResponseHeaders(), encryptedAuthData, authMode);
return new DataTransferResponse(job.exportService(), job.importService(), job.transferDataType(), Status.INPROCESS, baseUrl + (authMode == AuthMode.EXPORT ? FrontendConstantUrls.URL_NEXT_PAGE : FrontendConstantUrls.URL_COPY_PAGE));
} catch (Exception e) {
logger.debug("Exception occurred while trying to handle request: {}", e);
throw e;
}
}
use of org.dataportabilityproject.types.client.transfer.DataTransferResponse in project data-transfer-project by google.
the class DataTransferHandler method handleError.
/**
* Handles error response. TODO: Determine whether to return user facing error message here.
*/
public void handleError(HttpExchange exchange, DataTransferRequest request) throws IOException {
DataTransferResponse dataTransferResponse = new DataTransferResponse(request.getSource(), request.getDestination(), request.getTransferDataType(), Status.ERROR, ERROR_PATH);
// Mark the response as type Json and send
exchange.getResponseHeaders().set(CONTENT_TYPE, "application/json; charset=" + StandardCharsets.UTF_8.name());
exchange.sendResponseHeaders(200, 0);
objectMapper.writeValue(exchange.getResponseBody(), dataTransferResponse);
}
use of org.dataportabilityproject.types.client.transfer.DataTransferResponse in project data-transfer-project by google.
the class SetupHandler method handleImportSetup.
private DataTransferResponse handleImportSetup(Headers headers, PortabilityJob job, UUID jobId) throws IOException {
String exportAuthCookie = ReferenceApiUtils.getCookie(headers, JsonKeys.EXPORT_AUTH_DATA_COOKIE_KEY);
Preconditions.checkArgument(!Strings.isNullOrEmpty(exportAuthCookie), "Export auth cookie required");
// Initial auth flow url
AuthDataGenerator generator = registry.getAuthDataGenerator(job.importService(), job.transferDataType(), AuthMode.IMPORT);
Preconditions.checkNotNull(generator, "Generator not found for type: %s, service: %s", job.transferDataType(), job.importService());
String encodedJobId = ReferenceApiUtils.encodeJobId(jobId);
AuthFlowConfiguration authFlowConfiguration = generator.generateConfiguration(baseApiUrl, encodedJobId);
Preconditions.checkNotNull(authFlowConfiguration, "AuthFlowConfiguration not found for type: %s, service: %s", job.transferDataType(), job.importService());
// If present, store initial auth data for export services, e.g. used for oauth1
if (authFlowConfiguration.getInitialAuthData() != null) {
// Retrieve and parse the session key from the job
String sessionKey = job.jobAuthorization().sessionSecretKey();
SecretKey key = symmetricKeyGenerator.parse(BaseEncoding.base64Url().decode(sessionKey));
// Ensure intial auth data for import has not already been set
Preconditions.checkState(Strings.isNullOrEmpty(job.jobAuthorization().encryptedInitialImportAuthData()));
// Serialize and encrypt the initial auth data
String serialized = objectMapper.writeValueAsString(authFlowConfiguration.getInitialAuthData());
String encryptedInitialAuthData = EncrypterFactory.create(key).encrypt(serialized);
// Add the serialized and encrypted initial auth data to the job authorization
JobAuthorization updatedJobAuthorization = job.jobAuthorization().toBuilder().setEncryptedInitialImportAuthData(encryptedInitialAuthData).build();
// Persist the updated PortabilityJob with the updated JobAuthorization
PortabilityJob updatedPortabilityJob = job.toBuilder().setAndValidateJobAuthorization(updatedJobAuthorization).build();
store.updateJob(jobId, updatedPortabilityJob);
}
return new DataTransferResponse(job.exportService(), job.importService(), job.transferDataType(), Status.INPROCESS, // Redirect to auth page of import service
authFlowConfiguration.getUrl());
}
Aggregations