@Test(expected = LoginCanceledException.class) public void testMessages() throws Exception { final Host host = new Host(new TestProtocol(), "test.cyberduck.ch", new Credentials( PreferencesFactory.get().getProperty("connection.login.anon.name"), null )); host.setDefaultPath("/dav/basic"); final Session session = new NullSession(host) { @Override public void login(final Proxy proxy, final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException { throw new LoginCanceledException(); } }; session.open(Proxy.DIRECT, new DisabledHostKeyCallback(), new DisabledLoginCallback()); LoginService l = new KeychainLoginService(new DisabledPasswordStore()); l.authenticate(Proxy.DIRECT, session, new ProgressListener() { int i = 0; @Override public void message(final String message) { if(0 == i) { assertEquals("Authenticating as anonymous", message); } else if(1 == i) { assertEquals("Login failed", message); } else { fail(); } i++; } }, new DisabledLoginCallback(), new DisabledCancelCallback()); }
@Override public boolean authenticate(final Proxy proxy, final Session session, final ProgressListener listener, final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException { final Host bookmark = session.getHost(); final Credentials credentials = bookmark.getCredentials(); if(credentials.isPasswordAuthentication()) { listener.message(MessageFormat.format(LocaleFactory.localizedString("Authenticating as {0}", "Status"), credentials.getUsername())); } else if(credentials.isOAuthAuthentication()) { listener.message(MessageFormat.format(LocaleFactory.localizedString("Authenticating as {0}", "Status"), credentials.getOauth().getAccessToken())); } else if(credentials.isPublicKeyAuthentication()) { listener.message(MessageFormat.format(LocaleFactory.localizedString("Authenticating as {0}", "Status"), credentials.getIdentity().getName())); } else if(credentials.isCertificateAuthentication()) { listener.message(MessageFormat.format(LocaleFactory.localizedString("Authenticating as {0}", "Status"), credentials.getCertificate())); } try { if(log.isDebugEnabled()) { log.debug(String.format("Attempt authentication for %s", bookmark)); } session.login(proxy, prompt, cancel); if(log.isDebugEnabled()) { log.debug(String.format("Login successful for session %s", session)); } listener.message(LocaleFactory.localizedString("Login successful", "Credentials")); if(credentials.isSaved()) { try { keychain.save(bookmark); } catch(LocalAccessDeniedException e) { log.error(String.format("Failure saving credentials for %s in keychain. %s", session, e)); } } else { if(log.isInfoEnabled()) { log.info(String.format("Skip writing credentials for bookmark %s", bookmark.getHostname())); } } credentials.setPassed(true); credentials.reset(); return true; } catch(LoginFailureException e) { listener.message(LocaleFactory.localizedString("Login failed", "Credentials")); credentials.setPassed(false); final LoginOptions options = new LoginOptions(bookmark.getProtocol()); final StringAppender details = new StringAppender(); details.append(LocaleFactory.localizedString("Login failed", "Credentials")); details.append(e.getDetail()); if(this.prompt(bookmark, details.toString(), prompt, options)) { return false; } credentials.reset(); throw e; } }
KeychainLoginService implements LoginService { @Override public boolean authenticate(final Proxy proxy, final Session session, final ProgressListener listener, final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException { final Host bookmark = session.getHost(); final Credentials credentials = bookmark.getCredentials(); if(credentials.isPasswordAuthentication()) { listener.message(MessageFormat.format(LocaleFactory.localizedString("Authenticating as {0}", "Status"), credentials.getUsername())); } else if(credentials.isOAuthAuthentication()) { listener.message(MessageFormat.format(LocaleFactory.localizedString("Authenticating as {0}", "Status"), credentials.getOauth().getAccessToken())); } else if(credentials.isPublicKeyAuthentication()) { listener.message(MessageFormat.format(LocaleFactory.localizedString("Authenticating as {0}", "Status"), credentials.getIdentity().getName())); } else if(credentials.isCertificateAuthentication()) { listener.message(MessageFormat.format(LocaleFactory.localizedString("Authenticating as {0}", "Status"), credentials.getCertificate())); } try { if(log.isDebugEnabled()) { log.debug(String.format("Attempt authentication for %s", bookmark)); } session.login(proxy, prompt, cancel); if(log.isDebugEnabled()) { log.debug(String.format("Login successful for session %s", session)); } listener.message(LocaleFactory.localizedString("Login successful", "Credentials")); if(credentials.isSaved()) { try { keychain.save(bookmark); } catch(LocalAccessDeniedException e) { log.error(String.format("Failure saving credentials for %s in keychain. %s", session, e)); } } else { if(log.isInfoEnabled()) { log.info(String.format("Skip writing credentials for bookmark %s", bookmark.getHostname())); } } credentials.setPassed(true); credentials.reset(); return true; } catch(LoginFailureException e) { listener.message(LocaleFactory.localizedString("Login failed", "Credentials")); credentials.setPassed(false); final LoginOptions options = new LoginOptions(bookmark.getProtocol()); final StringAppender details = new StringAppender(); details.append(LocaleFactory.localizedString("Login failed", "Credentials")); details.append(e.getDetail()); if(this.prompt(bookmark, details.toString(), prompt, options)) { return false; } credentials.reset(); throw e; } } }
KeychainLoginService implements LoginService { @Override public boolean authenticate(final Proxy proxy, final Session session, final ProgressListener listener, final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException { final Host bookmark = session.getHost(); final Credentials credentials = bookmark.getCredentials(); if(credentials.isPasswordAuthentication()) { listener.message(MessageFormat.format(LocaleFactory.localizedString("Authenticating as {0}", "Status"), credentials.getUsername())); } else if(credentials.isOAuthAuthentication()) { listener.message(MessageFormat.format(LocaleFactory.localizedString("Authenticating as {0}", "Status"), credentials.getOauth().getAccessToken())); } else if(credentials.isPublicKeyAuthentication()) { listener.message(MessageFormat.format(LocaleFactory.localizedString("Authenticating as {0}", "Status"), credentials.getIdentity().getName())); } else if(credentials.isCertificateAuthentication()) { listener.message(MessageFormat.format(LocaleFactory.localizedString("Authenticating as {0}", "Status"), credentials.getCertificate())); } try { if(log.isDebugEnabled()) { log.debug(String.format("Attempt authentication for %s", bookmark)); } session.login(proxy, prompt, cancel); if(log.isDebugEnabled()) { log.debug(String.format("Login successful for session %s", session)); } listener.message(LocaleFactory.localizedString("Login successful", "Credentials")); if(credentials.isSaved()) { try { keychain.save(bookmark); } catch(LocalAccessDeniedException e) { log.error(String.format("Failure saving credentials for %s in keychain. %s", session, e)); } } else { if(log.isInfoEnabled()) { log.info(String.format("Skip writing credentials for bookmark %s", bookmark.getHostname())); } } credentials.setPassed(true); credentials.reset(); return true; } catch(LoginFailureException e) { listener.message(LocaleFactory.localizedString("Login failed", "Credentials")); credentials.setPassed(false); final LoginOptions options = new LoginOptions(bookmark.getProtocol()); final StringAppender details = new StringAppender(); details.append(LocaleFactory.localizedString("Login failed", "Credentials")); details.append(e.getDetail()); if(this.prompt(bookmark, details.toString(), prompt, options)) { return false; } credentials.reset(); throw e; } } KeychainLoginService(final HostPasswordStore keychain); }
KeychainLoginService implements LoginService { @Override public boolean authenticate(final Proxy proxy, final Session session, final ProgressListener listener, final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException { final Host bookmark = session.getHost(); final Credentials credentials = bookmark.getCredentials(); if(credentials.isPasswordAuthentication()) { listener.message(MessageFormat.format(LocaleFactory.localizedString("Authenticating as {0}", "Status"), credentials.getUsername())); } else if(credentials.isOAuthAuthentication()) { listener.message(MessageFormat.format(LocaleFactory.localizedString("Authenticating as {0}", "Status"), credentials.getOauth().getAccessToken())); } else if(credentials.isPublicKeyAuthentication()) { listener.message(MessageFormat.format(LocaleFactory.localizedString("Authenticating as {0}", "Status"), credentials.getIdentity().getName())); } else if(credentials.isCertificateAuthentication()) { listener.message(MessageFormat.format(LocaleFactory.localizedString("Authenticating as {0}", "Status"), credentials.getCertificate())); } try { if(log.isDebugEnabled()) { log.debug(String.format("Attempt authentication for %s", bookmark)); } session.login(proxy, prompt, cancel); if(log.isDebugEnabled()) { log.debug(String.format("Login successful for session %s", session)); } listener.message(LocaleFactory.localizedString("Login successful", "Credentials")); if(credentials.isSaved()) { try { keychain.save(bookmark); } catch(LocalAccessDeniedException e) { log.error(String.format("Failure saving credentials for %s in keychain. %s", session, e)); } } else { if(log.isInfoEnabled()) { log.info(String.format("Skip writing credentials for bookmark %s", bookmark.getHostname())); } } credentials.setPassed(true); credentials.reset(); return true; } catch(LoginFailureException e) { listener.message(LocaleFactory.localizedString("Login failed", "Credentials")); credentials.setPassed(false); final LoginOptions options = new LoginOptions(bookmark.getProtocol()); final StringAppender details = new StringAppender(); details.append(LocaleFactory.localizedString("Login failed", "Credentials")); details.append(e.getDetail()); if(this.prompt(bookmark, details.toString(), prompt, options)) { return false; } credentials.reset(); throw e; } } KeychainLoginService(final HostPasswordStore keychain); @Override void validate(final Host bookmark, final String message, final LoginCallback prompt,
                         final LoginOptions options); boolean prompt(final Host bookmark, final String message, final LoginCallback prompt, final LoginOptions options); @Override boolean authenticate(final Proxy proxy, final Session session, final ProgressListener listener,
                                final LoginCallback prompt, final CancelCallback cancel); }
KeychainLoginService implements LoginService { @Override public boolean authenticate(final Proxy proxy, final Session session, final ProgressListener listener, final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException { final Host bookmark = session.getHost(); final Credentials credentials = bookmark.getCredentials(); if(credentials.isPasswordAuthentication()) { listener.message(MessageFormat.format(LocaleFactory.localizedString("Authenticating as {0}", "Status"), credentials.getUsername())); } else if(credentials.isOAuthAuthentication()) { listener.message(MessageFormat.format(LocaleFactory.localizedString("Authenticating as {0}", "Status"), credentials.getOauth().getAccessToken())); } else if(credentials.isPublicKeyAuthentication()) { listener.message(MessageFormat.format(LocaleFactory.localizedString("Authenticating as {0}", "Status"), credentials.getIdentity().getName())); } else if(credentials.isCertificateAuthentication()) { listener.message(MessageFormat.format(LocaleFactory.localizedString("Authenticating as {0}", "Status"), credentials.getCertificate())); } try { if(log.isDebugEnabled()) { log.debug(String.format("Attempt authentication for %s", bookmark)); } session.login(proxy, prompt, cancel); if(log.isDebugEnabled()) { log.debug(String.format("Login successful for session %s", session)); } listener.message(LocaleFactory.localizedString("Login successful", "Credentials")); if(credentials.isSaved()) { try { keychain.save(bookmark); } catch(LocalAccessDeniedException e) { log.error(String.format("Failure saving credentials for %s in keychain. %s", session, e)); } } else { if(log.isInfoEnabled()) { log.info(String.format("Skip writing credentials for bookmark %s", bookmark.getHostname())); } } credentials.setPassed(true); credentials.reset(); return true; } catch(LoginFailureException e) { listener.message(LocaleFactory.localizedString("Login failed", "Credentials")); credentials.setPassed(false); final LoginOptions options = new LoginOptions(bookmark.getProtocol()); final StringAppender details = new StringAppender(); details.append(LocaleFactory.localizedString("Login failed", "Credentials")); details.append(e.getDetail()); if(this.prompt(bookmark, details.toString(), prompt, options)) { return false; } credentials.reset(); throw e; } } KeychainLoginService(final HostPasswordStore keychain); @Override void validate(final Host bookmark, final String message, final LoginCallback prompt,
                         final LoginOptions options); boolean prompt(final Host bookmark, final String message, final LoginCallback prompt, final LoginOptions options); @Override boolean authenticate(final Proxy proxy, final Session session, final ProgressListener listener,
                                final LoginCallback prompt, final CancelCallback cancel);  }