use of org.syncany.plugins.transfer.oauth.OAuthGenerator in project syncany by syncany.
the class AbstractInitCommand method doOAuthInRedirectMode.
private void doOAuthInRedirectMode(OAuthGenerator generator, OAuth settings) throws IOException, InterruptedException, ExecutionException, TimeoutException, StorageException {
OAuthTokenWebListener.Builder tokenListerBuilder = OAuthTokenWebListener.forMode(settings.mode());
if (settings.callbackPort() != OAuth.RANDOM_PORT) {
tokenListerBuilder.setPort(settings.callbackPort());
}
if (!settings.callbackId().equals(OAuth.PLUGIN_ID)) {
tokenListerBuilder.setId(settings.callbackId());
}
// non standard plugin?
if (generator instanceof OAuthGenerator.WithInterceptor) {
tokenListerBuilder.setTokenInterceptor(((OAuthGenerator.WithInterceptor) generator).getInterceptor());
}
if (generator instanceof OAuthGenerator.WithExtractor) {
tokenListerBuilder.setTokenExtractor(((OAuthGenerator.WithExtractor) generator).getExtractor());
}
OAuthTokenWebListener tokenListener = tokenListerBuilder.build();
URI oAuthURL = generator.generateAuthUrl(tokenListener.start());
Future<OAuthTokenFinish> futureTokenResponse = tokenListener.getToken();
out.println();
out.println("This plugin needs you to authenticate your account so that Syncany can access it.");
out.printf("Please navigate to the URL below and accept the given permissions:\n\n %s\n\n", oAuthURL.toString());
out.print("Waiting for authorization...");
OAuthTokenFinish tokenResponse = futureTokenResponse.get(OAUTH_TOKEN_WAIT_TIMEOUT, TimeUnit.SECONDS);
if (tokenResponse != null) {
out.printf(" received token '%s'\n\n", tokenResponse.getToken());
generator.checkToken(tokenResponse.getToken(), tokenResponse.getCsrfState());
} else {
out.println(" canceled");
throw new StorageException("Error while acquiring token, perhaps user denied authorization");
}
}
use of org.syncany.plugins.transfer.oauth.OAuthGenerator in project syncany by syncany.
the class AbstractInitCommand method printOAuthInformation.
private void printOAuthInformation(TransferSettings settings) throws StorageException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException, ExecutionException, InterruptedException, TimeoutException, NoSuchFieldException {
OAuth oAuthSettings = settings.getClass().getAnnotation(OAuth.class);
if (oAuthSettings != null) {
Constructor<? extends OAuthGenerator> optionCallbackClassConstructor = oAuthSettings.value().getDeclaredConstructor(settings.getClass());
OAuthGenerator oAuthGenerator = optionCallbackClassConstructor.newInstance(settings);
if (isHeadless) {
logger.log(Level.FINE, "User is in headless mode and the plugin is OAuth based");
if (oAuthGenerator instanceof OAuthGenerator.WithNoRedirectMode) {
doOAuthInCopyTokenMode(oAuthGenerator);
} else {
throw new RuntimeException("OAuth based plugin does not support headless mode");
}
} else {
doOAuthInRedirectMode(oAuthGenerator, oAuthSettings);
}
}
}
use of org.syncany.plugins.transfer.oauth.OAuthGenerator in project syncany by syncany.
the class OAuthTokenWebListenerTest method testOAuthGenerator.
@Test
public void testOAuthGenerator() {
OAuthGenerator testOAuthGenerator = new TestOAuthGenerator();
assertThat(testOAuthGenerator, instanceOf(OAuthGenerator.class));
assertThat(testOAuthGenerator, instanceOf(OAuthGenerator.WithExtractor.class));
assertThat(testOAuthGenerator, not(instanceOf(OAuthGenerator.WithInterceptor.class)));
}
Aggregations