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