Search in sources :

Example 1 with AuthFlowConfiguration

use of org.datatransferproject.spi.api.types.AuthFlowConfiguration in project data-transfer-project by google.

the class OAuth2DataGenerator method generateConfiguration.

@Override
public AuthFlowConfiguration generateConfiguration(String callbackBaseUrl, String id) {
    String encodedJobId = BaseEncoding.base64Url().encode(id.getBytes(UTF_8));
    String scope = scopes.isEmpty() ? "" : String.join(" ", scopes);
    try {
        URIBuilder builder = new URIBuilder(config.getAuthUrl()).setParameter("response_type", "code").setParameter("client_id", clientId).setParameter("redirect_uri", callbackBaseUrl).setParameter("scope", scope).setParameter("state", encodedJobId);
        if (config.getAdditionalAuthUrlParameters() != null) {
            for (Entry<String, String> entry : config.getAdditionalAuthUrlParameters().entrySet()) {
                builder.setParameter(entry.getKey(), entry.getValue());
            }
        }
        String url = builder.build().toString();
        return new AuthFlowConfiguration(url, OAUTH_2, getTokenUrl());
    } catch (URISyntaxException e) {
        throw new IllegalStateException("Could not produce url.", e);
    }
}
Also used : AuthFlowConfiguration(org.datatransferproject.spi.api.types.AuthFlowConfiguration) URISyntaxException(java.net.URISyntaxException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 2 with AuthFlowConfiguration

use of org.datatransferproject.spi.api.types.AuthFlowConfiguration 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;
}
Also used : MicrosoftAuthServiceExtension(org.datatransferproject.auth.microsoft.MicrosoftAuthServiceExtension) AuthFlowConfiguration(org.datatransferproject.spi.api.types.AuthFlowConfiguration) AuthDataGenerator(org.datatransferproject.spi.api.auth.AuthDataGenerator) OkHttpClient(okhttp3.OkHttpClient) Desktop(java.awt.Desktop) TokenAuthData(org.datatransferproject.types.transfer.auth.TokenAuthData) URI(java.net.URI)

Example 3 with AuthFlowConfiguration

use of org.datatransferproject.spi.api.types.AuthFlowConfiguration 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);
    }
}
Also used : AuthFlowConfiguration(org.datatransferproject.spi.api.types.AuthFlowConfiguration) ExportInformation(org.datatransferproject.types.common.ExportInformation) PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob) AuthDataGenerator(org.datatransferproject.spi.api.auth.AuthDataGenerator) SecretKey(javax.crypto.SecretKey) IOException(java.io.IOException) UUID(java.util.UUID) TransferJob(org.datatransferproject.types.client.transfer.TransferJob) CreateTransferJob(org.datatransferproject.types.client.transfer.CreateTransferJob)

Example 4 with AuthFlowConfiguration

use of org.datatransferproject.spi.api.types.AuthFlowConfiguration in project data-transfer-project by google.

the class OAuth1DataGenerator method generateConfiguration.

@Override
public AuthFlowConfiguration generateConfiguration(String callbackBaseUrl, String id) {
    String callback = (Strings.isNullOrEmpty(callbackBaseUrl)) ? OUT_OF_BOUNDS_CALLBACK : callbackBaseUrl;
    OAuthGetTemporaryToken tempTokenRequest = new OAuthGetTemporaryToken(config.getRequestTokenUrl());
    tempTokenRequest.callback = callback;
    tempTokenRequest.transport = httpTransport;
    tempTokenRequest.consumerKey = clientId;
    tempTokenRequest.signer = config.getRequestTokenSigner(clientSecret);
    config.getAdditionalUrlParameters(dataType, mode, OAuth1Step.REQUEST_TOKEN).forEach(tempTokenRequest::set);
    TokenSecretAuthData authData;
    try {
        // get request token
        OAuthCredentialsResponse tempTokenResponse = tempTokenRequest.execute();
        authData = new TokenSecretAuthData(tempTokenResponse.token, tempTokenResponse.tokenSecret);
    } catch (IOException e) {
        monitor.severe(() -> "Error retrieving request token", e);
        return null;
    }
    OAuthAuthorizeTemporaryTokenUrl authorizeUrl = new OAuthAuthorizeTemporaryTokenUrl(config.getAuthorizationUrl());
    authorizeUrl.temporaryToken = authData.getToken();
    config.getAdditionalUrlParameters(dataType, mode, OAuth1Step.AUTHORIZATION).forEach(authorizeUrl::set);
    String url = authorizeUrl.build();
    return new AuthFlowConfiguration(url, getTokenUrl(), AuthProtocol.OAUTH_1, authData);
}
Also used : AuthFlowConfiguration(org.datatransferproject.spi.api.types.AuthFlowConfiguration) TokenSecretAuthData(org.datatransferproject.types.transfer.auth.TokenSecretAuthData) OAuthGetTemporaryToken(com.google.api.client.auth.oauth.OAuthGetTemporaryToken) IOException(java.io.IOException) OAuthAuthorizeTemporaryTokenUrl(com.google.api.client.auth.oauth.OAuthAuthorizeTemporaryTokenUrl) OAuthCredentialsResponse(com.google.api.client.auth.oauth.OAuthCredentialsResponse)

Aggregations

AuthFlowConfiguration (org.datatransferproject.spi.api.types.AuthFlowConfiguration)4 IOException (java.io.IOException)2 AuthDataGenerator (org.datatransferproject.spi.api.auth.AuthDataGenerator)2 OAuthAuthorizeTemporaryTokenUrl (com.google.api.client.auth.oauth.OAuthAuthorizeTemporaryTokenUrl)1 OAuthCredentialsResponse (com.google.api.client.auth.oauth.OAuthCredentialsResponse)1 OAuthGetTemporaryToken (com.google.api.client.auth.oauth.OAuthGetTemporaryToken)1 Desktop (java.awt.Desktop)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 UUID (java.util.UUID)1 SecretKey (javax.crypto.SecretKey)1 OkHttpClient (okhttp3.OkHttpClient)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1 MicrosoftAuthServiceExtension (org.datatransferproject.auth.microsoft.MicrosoftAuthServiceExtension)1 PortabilityJob (org.datatransferproject.spi.cloud.types.PortabilityJob)1 CreateTransferJob (org.datatransferproject.types.client.transfer.CreateTransferJob)1 TransferJob (org.datatransferproject.types.client.transfer.TransferJob)1 ExportInformation (org.datatransferproject.types.common.ExportInformation)1 TokenAuthData (org.datatransferproject.types.transfer.auth.TokenAuthData)1 TokenSecretAuthData (org.datatransferproject.types.transfer.auth.TokenSecretAuthData)1