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