Search in sources :

Example 1 with OAuthTokenWebListener

use of org.syncany.plugins.transfer.oauth.OAuthTokenWebListener 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 OAuthTokenWebListener

use of org.syncany.plugins.transfer.oauth.OAuthTokenWebListener in project syncany by syncany.

the class OAuthTokenWebListenerTest method testTokenAsQuery.

@Test
public void testTokenAsQuery() throws Exception {
    OAuthTokenWebListener twl = OAuthTokenWebListener.forMode(OAuthMode.SERVER).build();
    URI baseUri = twl.start();
    Future<OAuthTokenFinish> submittedToken = twl.getToken();
    final URI requestUri = URI.create(baseUri.toString() + "?" + OAuthTokenExtractors.RFC_CODE_FIELD + "=" + REFERENCE_TOKEN + "&" + OAuthTokenExtractors.RFC_STATE_FIELD + "=1234");
    new Thread(new Runnable() {

        public void run() {
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println("Requesting: " + requestUri);
                HtmlPage page = WEB_CLIENT.getPage(requestUri.toURL());
                assertEquals(200, page.getWebResponse().getStatusCode());
            } catch (ScriptException e) {
            // ignore  "Connection is not open"
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }).start();
    assertEquals(REFERENCE_TOKEN, submittedToken.get().getToken());
}
Also used : ScriptException(com.gargoylesoftware.htmlunit.ScriptException) OAuthTokenFinish(org.syncany.plugins.transfer.oauth.OAuthTokenFinish) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) OAuthTokenWebListener(org.syncany.plugins.transfer.oauth.OAuthTokenWebListener) URI(java.net.URI) StorageException(org.syncany.plugins.transfer.StorageException) ScriptException(com.gargoylesoftware.htmlunit.ScriptException) Test(org.junit.Test)

Example 3 with OAuthTokenWebListener

use of org.syncany.plugins.transfer.oauth.OAuthTokenWebListener in project syncany by syncany.

the class OAuthTokenWebListenerTest method testMissingField.

@Test
public void testMissingField() throws Exception {
    OAuthTokenWebListener twl = OAuthTokenWebListener.forMode(OAuthMode.SERVER).build();
    URI baseUri = twl.start();
    Future<OAuthTokenFinish> submittedToken = twl.getToken();
    final URI requestUri = URI.create(baseUri.toString() + "?" + OAuthTokenExtractors.RFC_STATE_FIELD + "=1234");
    new Thread(new Runnable() {

        public void run() {
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println("Requesting: " + requestUri);
                HtmlPage page = WEB_CLIENT.getPage(requestUri.toURL());
                assertEquals(400, page.getWebResponse().getStatusCode());
            } catch (ScriptException e) {
            // ignore  "Connection is not open"
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }).start();
    assertNull(submittedToken.get());
}
Also used : ScriptException(com.gargoylesoftware.htmlunit.ScriptException) OAuthTokenFinish(org.syncany.plugins.transfer.oauth.OAuthTokenFinish) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) OAuthTokenWebListener(org.syncany.plugins.transfer.oauth.OAuthTokenWebListener) URI(java.net.URI) StorageException(org.syncany.plugins.transfer.StorageException) ScriptException(com.gargoylesoftware.htmlunit.ScriptException) Test(org.junit.Test)

Example 4 with OAuthTokenWebListener

use of org.syncany.plugins.transfer.oauth.OAuthTokenWebListener in project syncany by syncany.

the class OAuthTokenWebListenerTest method testTokenAsHash.

@Test
public void testTokenAsHash() throws Exception {
    OAuthTokenWebListener twl = OAuthTokenWebListener.forMode(OAuthMode.BROWSER).build();
    URI baseUri = twl.start();
    Future<OAuthTokenFinish> submittedToken = twl.getToken();
    final URI requestUri = URI.create(baseUri.toString() + "?a=b#" + OAuthTokenExtractors.RFC_ACCESS_TOKEN_FIELD + "=" + REFERENCE_TOKEN + "&" + OAuthTokenExtractors.RFC_STATE_FIELD + "=1234");
    new Thread(new Runnable() {

        public void run() {
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println("Requesting: " + requestUri);
                HtmlPage page = WEB_CLIENT.getPage(requestUri.toURL());
                assertEquals(200, page.getWebResponse().getStatusCode());
            } catch (ScriptException e) {
            // ignore  "Connection is not open"
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }).start();
    assertEquals(REFERENCE_TOKEN, submittedToken.get().getToken());
}
Also used : ScriptException(com.gargoylesoftware.htmlunit.ScriptException) OAuthTokenFinish(org.syncany.plugins.transfer.oauth.OAuthTokenFinish) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) OAuthTokenWebListener(org.syncany.plugins.transfer.oauth.OAuthTokenWebListener) URI(java.net.URI) StorageException(org.syncany.plugins.transfer.StorageException) ScriptException(com.gargoylesoftware.htmlunit.ScriptException) Test(org.junit.Test)

Aggregations

URI (java.net.URI)4 StorageException (org.syncany.plugins.transfer.StorageException)4 OAuthTokenFinish (org.syncany.plugins.transfer.oauth.OAuthTokenFinish)4 OAuthTokenWebListener (org.syncany.plugins.transfer.oauth.OAuthTokenWebListener)4 ScriptException (com.gargoylesoftware.htmlunit.ScriptException)3 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)3 Test (org.junit.Test)3 OAuthGenerator (org.syncany.plugins.transfer.oauth.OAuthGenerator)1