use of ch.cyberduck.core.oauth.OAuth2TokenListenerRegistry in project cyberduck by iterate-ch.
the class MainController method handleGetURLEvent_withReplyEvent.
/**
* Extract the URL from the Apple event and handle it here.
*/
@Action
public void handleGetURLEvent_withReplyEvent(NSAppleEventDescriptor event, NSAppleEventDescriptor reply) {
log.debug(String.format("Received URL from event %s", event));
final NSAppleEventDescriptor param = event.paramDescriptorForKeyword(keyAEResult);
if (null == param) {
log.error("No URL parameter");
return;
}
final String url = param.stringValue();
if (StringUtils.isEmpty(url)) {
log.error("URL parameter is empty");
return;
}
switch(url) {
case "x-cyberduck-action:update":
updater.check(false);
break;
default:
if (StringUtils.startsWith(url, OAuth2AuthorizationService.CYBERDUCK_REDIRECT_URI)) {
final String action = StringUtils.removeStart(url, OAuth2AuthorizationService.CYBERDUCK_REDIRECT_URI);
final List<NameValuePair> pairs = URLEncodedUtils.parse(URI.create(action), Charset.defaultCharset());
String state = StringUtils.EMPTY;
String code = StringUtils.EMPTY;
for (NameValuePair pair : pairs) {
if (StringUtils.equals(pair.getName(), "state")) {
state = StringUtils.equals(pair.getName(), "state") ? pair.getValue() : StringUtils.EMPTY;
}
if (StringUtils.equals(pair.getName(), "code")) {
code = StringUtils.equals(pair.getName(), "code") ? pair.getValue() : StringUtils.EMPTY;
}
}
final OAuth2TokenListenerRegistry oauth = OAuth2TokenListenerRegistry.get();
oauth.notify(state, code);
} else if (StringUtils.startsWith(url, CteraProtocol.CTERA_REDIRECT_URI)) {
final String action = StringUtils.removeStart(url, String.format("%s:", preferences.getProperty("oauth.handler.scheme")));
final List<NameValuePair> pairs = URLEncodedUtils.parse(URI.create(action), Charset.defaultCharset());
String code = StringUtils.EMPTY;
for (NameValuePair pair : pairs) {
if (StringUtils.equals(pair.getName(), "ActivationCode")) {
code = StringUtils.equals(pair.getName(), "ActivationCode") ? pair.getValue() : StringUtils.EMPTY;
}
}
final OAuth2TokenListenerRegistry oauth = OAuth2TokenListenerRegistry.get();
oauth.notify(StringUtils.EMPTY, code);
} else {
try {
final Host h = HostParser.parse(url);
h.setCredentials(CredentialsConfiguratorFactory.get(h.getProtocol()).configure(h));
if (Path.Type.file == detector.detect(h.getDefaultPath())) {
final Path file = new Path(PathNormalizer.normalize(h.getDefaultPath()), EnumSet.of(Path.Type.file));
TransferControllerFactory.get().start(new DownloadTransfer(h, file, LocalFactory.get(preferences.getProperty("queue.download.folder"), file.getName())), new TransferOptions());
} else {
for (BrowserController browser : MainController.getBrowsers()) {
if (browser.isMounted()) {
if (new HostUrlProvider().get(browser.getSession().getHost()).equals(new HostUrlProvider().get(h))) {
// Handle browser window already connected to the same host. #4215
browser.window().makeKeyAndOrderFront(null);
if (Path.Type.directory == detector.detect(h.getDefaultPath())) {
browser.setWorkdir(new Path(PathNormalizer.normalize(h.getDefaultPath()), EnumSet.of(Path.Type.directory)));
}
return;
}
}
}
final BrowserController browser = newDocument(false);
browser.mount(h);
}
} catch (HostParserException e) {
log.warn(e);
}
}
}
}
use of ch.cyberduck.core.oauth.OAuth2TokenListenerRegistry in project cyberduck by iterate-ch.
the class CteraSession method startWebSSOFlow.
private AttachDeviceResponse startWebSSOFlow(final CancelCallback cancel, final Credentials credentials) throws BackgroundException {
final String url = String.format("%s/ServicesPortal/activate?scheme=%s", new HostUrlProvider().withUsername(false).withPath(false).get(host), CteraProtocol.CTERA_REDIRECT_URI);
if (log.isDebugEnabled()) {
log.debug(String.format("Open browser with URL %s", url));
}
if (!BrowserLauncherFactory.get().open(url)) {
log.warn(String.format("Failed to launch web browser for %s", url));
}
final AtomicReference<String> activationCode = new AtomicReference<>();
final CountDownLatch signal = new CountDownLatch(1);
final OAuth2TokenListenerRegistry registry = OAuth2TokenListenerRegistry.get();
registry.register(StringUtils.EMPTY, code -> {
if (log.isInfoEnabled()) {
log.info(String.format("Callback with code %s", code));
}
if (!StringUtils.isBlank(code)) {
activationCode.set(code);
}
signal.countDown();
});
while (!Uninterruptibles.awaitUninterruptibly(signal, 500, TimeUnit.MILLISECONDS)) {
cancel.verify();
}
return this.attachDeviceWithActivationCode(activationCode.get());
}
Aggregations