Search in sources :

Example 1 with OAuthGenerator

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");
    }
}
Also used : OAuthTokenFinish(org.syncany.plugins.transfer.oauth.OAuthTokenFinish) OAuthGenerator(org.syncany.plugins.transfer.oauth.OAuthGenerator) OAuthTokenWebListener(org.syncany.plugins.transfer.oauth.OAuthTokenWebListener) URI(java.net.URI) StorageException(org.syncany.plugins.transfer.StorageException)

Example 2 with OAuthGenerator

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);
        }
    }
}
Also used : OAuthGenerator(org.syncany.plugins.transfer.oauth.OAuthGenerator) OAuth(org.syncany.plugins.transfer.oauth.OAuth)

Example 3 with OAuthGenerator

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)));
}
Also used : OAuthGenerator(org.syncany.plugins.transfer.oauth.OAuthGenerator) Test(org.junit.Test)

Aggregations

OAuthGenerator (org.syncany.plugins.transfer.oauth.OAuthGenerator)3 URI (java.net.URI)1 Test (org.junit.Test)1 StorageException (org.syncany.plugins.transfer.StorageException)1 OAuth (org.syncany.plugins.transfer.oauth.OAuth)1 OAuthTokenFinish (org.syncany.plugins.transfer.oauth.OAuthTokenFinish)1 OAuthTokenWebListener (org.syncany.plugins.transfer.oauth.OAuthTokenWebListener)1