Search in sources :

Example 1 with OAuth2TokenListenerRegistry

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);
                }
            }
    }
}
Also used : OAuth2TokenListenerRegistry(ch.cyberduck.core.oauth.OAuth2TokenListenerRegistry) NameValuePair(org.apache.http.NameValuePair) NSAppleEventDescriptor(ch.cyberduck.binding.foundation.NSAppleEventDescriptor) NSAttributedString(ch.cyberduck.binding.foundation.NSAttributedString) HostParserException(ch.cyberduck.core.exception.HostParserException) TransferOptions(ch.cyberduck.core.transfer.TransferOptions) DownloadTransfer(ch.cyberduck.core.transfer.DownloadTransfer) ArrayList(java.util.ArrayList) List(java.util.List) AbstractBackgroundAction(ch.cyberduck.core.threading.AbstractBackgroundAction) Action(ch.cyberduck.binding.Action)

Example 2 with OAuth2TokenListenerRegistry

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());
}
Also used : OAuth2TokenListenerRegistry(ch.cyberduck.core.oauth.OAuth2TokenListenerRegistry) HostUrlProvider(ch.cyberduck.core.HostUrlProvider) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

OAuth2TokenListenerRegistry (ch.cyberduck.core.oauth.OAuth2TokenListenerRegistry)2 Action (ch.cyberduck.binding.Action)1 NSAppleEventDescriptor (ch.cyberduck.binding.foundation.NSAppleEventDescriptor)1 NSAttributedString (ch.cyberduck.binding.foundation.NSAttributedString)1 HostUrlProvider (ch.cyberduck.core.HostUrlProvider)1 HostParserException (ch.cyberduck.core.exception.HostParserException)1 AbstractBackgroundAction (ch.cyberduck.core.threading.AbstractBackgroundAction)1 DownloadTransfer (ch.cyberduck.core.transfer.DownloadTransfer)1 TransferOptions (ch.cyberduck.core.transfer.TransferOptions)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 NameValuePair (org.apache.http.NameValuePair)1