Search in sources :

Example 1 with AuthData

use of org.dataportabilityproject.types.transfer.auth.AuthData in project data-transfer-project by google.

the class FlickrPhotosExporterTest method exportAlbumInitial.

@Test
public void exportAlbumInitial() throws FlickrException {
    // set up auth, flickr service
    when(user.getId()).thenReturn("userId");
    when(authInterface.checkToken(any(Token.class))).thenReturn(auth);
    when(flickr.getPhotosetsInterface()).thenReturn(photosetsInterface);
    when(flickr.getPhotosInterface()).thenReturn(photosInterface);
    when(flickr.getAuthInterface()).thenReturn(authInterface);
    // setup photoset
    Photoset photoset = FlickrTestUtils.initializePhotoset("photosetId", "title", "description");
    // setup photoset list (aka album view)
    int page = 1;
    Photosets photosetsList = new Photosets();
    photosetsList.setPage(page);
    photosetsList.setPages(page + 1);
    photosetsList.setPhotosets(Collections.singletonList(photoset));
    when(photosetsInterface.getList(anyString(), anyInt(), anyInt(), anyString())).thenReturn(photosetsList);
    // run test
    FlickrPhotosExporter exporter = new FlickrPhotosExporter(flickr);
    AuthData authData = new TokenSecretAuthData("token", "secret");
    ExportResult<PhotosContainerResource> result = exporter.export(UUID.randomUUID(), authData);
    // make sure album and photo information is correct
    assertThat(result.getExportedData().getPhotos()).isEmpty();
    Collection<PhotoAlbum> albums = result.getExportedData().getAlbums();
    assertThat(albums.size()).isEqualTo(1);
    assertThat(albums).containsExactly(new PhotoAlbum("photosetId", "title", "description"));
    // check continuation information
    ContinuationData continuationData = (ContinuationData) result.getContinuationData();
    assertThat(continuationData.getPaginationData()).isInstanceOf(IntPaginationToken.class);
    assertThat(((IntPaginationToken) continuationData.getPaginationData()).getStart()).isEqualTo(page + 1);
    Collection<? extends ContainerResource> subResources = continuationData.getContainerResources();
    assertThat(subResources.size()).isEqualTo(1);
    assertThat(subResources).containsExactly(new IdOnlyContainerResource("photosetId"));
}
Also used : IntPaginationToken(org.dataportabilityproject.spi.transfer.types.IntPaginationToken) AuthData(org.dataportabilityproject.types.transfer.auth.AuthData) TokenSecretAuthData(org.dataportabilityproject.types.transfer.auth.TokenSecretAuthData) IntPaginationToken(org.dataportabilityproject.spi.transfer.types.IntPaginationToken) Token(org.scribe.model.Token) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) PhotosContainerResource(org.dataportabilityproject.types.transfer.models.photos.PhotosContainerResource) TokenSecretAuthData(org.dataportabilityproject.types.transfer.auth.TokenSecretAuthData) Photoset(com.flickr4java.flickr.photosets.Photoset) Photosets(com.flickr4java.flickr.photosets.Photosets) IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) PhotoAlbum(org.dataportabilityproject.types.transfer.models.photos.PhotoAlbum) Test(org.junit.Test)

Example 2 with AuthData

use of org.dataportabilityproject.types.transfer.auth.AuthData in project data-transfer-project by google.

the class OauthCallbackHandler method handleExchange.

private String handleExchange(HttpExchange exchange) throws IOException {
    String redirect = "/error";
    try {
        Headers requestHeaders = exchange.getRequestHeaders();
        // Get the URL for the request - needed for the authorization.
        String requestURL = ReferenceApiUtils.createURL(requestHeaders.getFirst(HttpHeaders.HOST), exchange.getRequestURI().toString(), IS_LOCAL);
        Map<String, String> requestParams = ReferenceApiUtils.getRequestParams(exchange);
        String encodedIdCookie = ReferenceApiUtils.getCookie(requestHeaders, JsonKeys.ID_COOKIE_KEY);
        Preconditions.checkArgument(!Strings.isNullOrEmpty(encodedIdCookie), "Missing encodedIdCookie");
        String oauthToken = requestParams.get("oauth_token");
        Preconditions.checkArgument(!Strings.isNullOrEmpty(oauthToken), "Missing oauth_token");
        String oauthVerifier = requestParams.get("oauth_verifier");
        Preconditions.checkArgument(!Strings.isNullOrEmpty(oauthVerifier), "Missing oauth_verifier");
        // Valid job must be present
        Preconditions.checkArgument(!Strings.isNullOrEmpty(encodedIdCookie), "Encoded Id cookie required");
        UUID jobId = ReferenceApiUtils.decodeJobId(encodedIdCookie);
        PortabilityJob job = store.findJob(jobId);
        logger.debug("Found job: {}->{} in OCH", jobId, job);
        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());
        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.toString());
        AuthDataGenerator generator = registry.getAuthDataGenerator(service, job.transferDataType(), authMode);
        Preconditions.checkNotNull(generator, "Generator not found for type: %s, service: %s", job.transferDataType(), service);
        // 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);
        }
        Preconditions.checkNotNull(initialAuthData, "Initial AuthData expected during Oauth 1.0 flow");
        // TODO: Use UUID instead of UUID.toString()
        // Generate auth data
        AuthData authData = generator.generateAuthData(baseApiUrl, oauthVerifier, jobId.toString(), initialAuthData, null);
        Preconditions.checkNotNull(authData, "Auth data should not be null");
        // 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);
        redirect = baseUrl + ((authMode == AuthMode.EXPORT) ? FrontendConstantUrls.URL_NEXT_PAGE : FrontendConstantUrls.URL_COPY_PAGE);
    } catch (Exception e) {
        logger.error("Error handling request", e);
        throw e;
    }
    return redirect;
}
Also used : PortabilityJob(org.dataportabilityproject.spi.cloud.types.PortabilityJob) AuthDataGenerator(org.dataportabilityproject.spi.gateway.auth.AuthDataGenerator) SecretKey(javax.crypto.SecretKey) AuthData(org.dataportabilityproject.types.transfer.auth.AuthData) HttpHeaders(com.google.common.net.HttpHeaders) Headers(com.sun.net.httpserver.Headers) UUID(java.util.UUID) AuthMode(org.dataportabilityproject.spi.gateway.auth.AuthServiceProviderRegistry.AuthMode) IOException(java.io.IOException)

Example 3 with AuthData

use of org.dataportabilityproject.types.transfer.auth.AuthData 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;
    }
}
Also used : PortabilityJob(org.dataportabilityproject.spi.cloud.types.PortabilityJob) AuthDataGenerator(org.dataportabilityproject.spi.gateway.auth.AuthDataGenerator) SecretKey(javax.crypto.SecretKey) AuthData(org.dataportabilityproject.types.transfer.auth.AuthData) Headers(com.sun.net.httpserver.Headers) HttpHeaders(org.apache.http.HttpHeaders) SimpleLoginRequest(org.dataportabilityproject.types.client.transfer.SimpleLoginRequest) DataTransferResponse(org.dataportabilityproject.types.client.transfer.DataTransferResponse) UUID(java.util.UUID) AuthMode(org.dataportabilityproject.spi.gateway.auth.AuthServiceProviderRegistry.AuthMode) IOException(java.io.IOException)

Example 4 with AuthData

use of org.dataportabilityproject.types.transfer.auth.AuthData in project data-transfer-project by google.

the class LocalCopier method copyDataType.

private <T extends DataModel> void copyDataType(ServiceProviderRegistry registry, PortableDataType type) throws IOException {
    String exporterName = ioInterface.ask("What service do you want to export from", registry.getServiceProvidersThatCanExport(type));
    String importerName = ioInterface.ask("What service do you want to import to", registry.getServiceProvidersThatCanImport(type));
    AuthData exportAuthData = registry.getOfflineAuth(exporterName, type, ServiceMode.EXPORT).generateAuthData(ioInterface);
    // This is a hack to allow round tripping to the same account while only doing one auth.
    AuthData importAuthData;
    if (exporterName.equals(importerName)) {
        importAuthData = exportAuthData;
    } else {
        importAuthData = registry.getOfflineAuth(importerName, type, ServiceMode.IMPORT).generateAuthData(ioInterface);
    }
    UUID jobId = UUID.randomUUID();
    try {
        logger.info("Starting job {}", jobId);
        PortabilityCopier.copyDataType(registry, type, exporterName, exportAuthData, importerName, importAuthData, jobId);
    } finally {
        cloudFactory.clearJobData(jobId);
    }
}
Also used : AuthData(org.dataportabilityproject.types.transfer.auth.AuthData) UUID(java.util.UUID)

Example 5 with AuthData

use of org.dataportabilityproject.types.transfer.auth.AuthData in project data-transfer-project by google.

the class Oauth2CallbackHandler method handleExchange.

private String handleExchange(HttpExchange exchange) throws IOException {
    String redirect = "/error";
    try {
        Headers requestHeaders = exchange.getRequestHeaders();
        String requestURL = ReferenceApiUtils.createURL(requestHeaders.getFirst(HttpHeaders.HOST), exchange.getRequestURI().toString(), IS_LOCAL);
        AuthorizationCodeResponseUrl authResponse = new AuthorizationCodeResponseUrl(requestURL);
        // check for user-denied error
        if (authResponse.getError() != null) {
            logger.warn("Authorization DENIED: {} Redirecting to /error", authResponse.getError());
            return redirect;
        }
        // retrieve cookie from exchange
        Map<String, HttpCookie> httpCookies = ReferenceApiUtils.getCookies(requestHeaders);
        HttpCookie encodedIdCookie = httpCookies.get(JsonKeys.ID_COOKIE_KEY);
        Preconditions.checkArgument(encodedIdCookie != null && !Strings.isNullOrEmpty(encodedIdCookie.getValue()), "Encoded Id cookie required");
        UUID jobId = ReferenceApiUtils.decodeJobId(encodedIdCookie.getValue());
        logger.debug("State token: {}", authResponse.getState());
        // TODO(#258): Check job ID in state token, was broken during local demo
        // UUID jobIdFromState = ReferenceApiUtils.decodeJobId(authResponse.getState());
        // // TODO: Remove sanity check
        // Preconditions.checkState(
        // jobIdFromState.equals(jobId),
        // "Job id in cookie [%s] and request [%s] should match",
        // jobId,
        // jobIdFromState);
        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());
        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.toString());
        AuthDataGenerator generator = registry.getAuthDataGenerator(service, job.transferDataType(), authMode);
        Preconditions.checkNotNull(generator, "Generator not found for type: %s, service: %s", job.transferDataType(), service);
        // 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(baseApiUrl, authResponse.getCode(), jobId.toString(), initialAuthData, null);
        Preconditions.checkNotNull(authData, "Auth data should not be null");
        // 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);
        redirect = baseUrl + ((authMode == AuthMode.EXPORT) ? FrontendConstantUrls.URL_NEXT_PAGE : FrontendConstantUrls.URL_COPY_PAGE);
    } catch (Exception e) {
        logger.error("Error handling request: {}", e);
        throw e;
    }
    return redirect;
}
Also used : PortabilityJob(org.dataportabilityproject.spi.cloud.types.PortabilityJob) AuthDataGenerator(org.dataportabilityproject.spi.gateway.auth.AuthDataGenerator) SecretKey(javax.crypto.SecretKey) AuthData(org.dataportabilityproject.types.transfer.auth.AuthData) HttpHeaders(com.google.common.net.HttpHeaders) Headers(com.sun.net.httpserver.Headers) AuthorizationCodeResponseUrl(com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl) UUID(java.util.UUID) HttpCookie(java.net.HttpCookie) AuthMode(org.dataportabilityproject.spi.gateway.auth.AuthServiceProviderRegistry.AuthMode) IOException(java.io.IOException)

Aggregations

AuthData (org.dataportabilityproject.types.transfer.auth.AuthData)6 UUID (java.util.UUID)5 IOException (java.io.IOException)4 SecretKey (javax.crypto.SecretKey)4 PortabilityJob (org.dataportabilityproject.spi.cloud.types.PortabilityJob)4 Headers (com.sun.net.httpserver.Headers)3 AuthDataGenerator (org.dataportabilityproject.spi.gateway.auth.AuthDataGenerator)3 AuthMode (org.dataportabilityproject.spi.gateway.auth.AuthServiceProviderRegistry.AuthMode)3 HttpHeaders (com.google.common.net.HttpHeaders)2 Photoset (com.flickr4java.flickr.photosets.Photoset)1 Photosets (com.flickr4java.flickr.photosets.Photosets)1 AuthorizationCodeResponseUrl (com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl)1 HttpCookie (java.net.HttpCookie)1 HttpHeaders (org.apache.http.HttpHeaders)1 Decrypter (org.dataportabilityproject.security.Decrypter)1 JobAuthorization (org.dataportabilityproject.spi.cloud.types.JobAuthorization)1 ContinuationData (org.dataportabilityproject.spi.transfer.types.ContinuationData)1 IdOnlyContainerResource (org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource)1 IntPaginationToken (org.dataportabilityproject.spi.transfer.types.IntPaginationToken)1 DataTransferResponse (org.dataportabilityproject.types.client.transfer.DataTransferResponse)1