@Test(expected = LoginFailureException.class) @Ignore public void testAuthenticate() throws Exception { assertFalse(new SFTPChallengeResponseAuthentication(session).authenticate(session.getHost(), new DisabledLoginCallback(), new DisabledCancelCallback())); }
@Override public Boolean authenticate(final Host bookmark, final LoginCallback callback, final CancelCallback cancel) throws BackgroundException { if(log.isDebugEnabled()) { log.debug(String.format("Login using challenge response authentication for %s", bookmark)); } final AtomicBoolean canceled = new AtomicBoolean(); final AtomicBoolean publickey = new AtomicBoolean(); try { session.getClient().auth(bookmark.getCredentials().getUsername(), new AuthKeyboardInteractive(new ChallengeResponseProvider() { private String name = StringUtils.EMPTY; private String instruction = StringUtils.EMPTY; @Override public List<String> getSubmethods() { return Collections.emptyList(); } @Override public void init(final Resource resource, final String name, final String instruction) { if(StringUtils.isNoneBlank(instruction)) { this.instruction = instruction; } if(StringUtils.isNoneBlank(name)) { this.name = name; } } @Override public char[] getResponse(final String prompt, final boolean echo) { if(log.isDebugEnabled()) { log.debug(String.format("Reply to challenge name %s with instruction %s", name, instruction)); } if(DEFAULT_PROMPT_PATTERN.matcher(prompt).matches()) { if(StringUtils.isBlank(bookmark.getCredentials().getPassword())) { try { final Credentials input = callback.prompt(bookmark, bookmark.getCredentials().getUsername(), String.format("%s %s", LocaleFactory.localizedString("Login", "Login"), bookmark.getHostname()), MessageFormat.format(LocaleFactory.localizedString( "Login {0} with username and password", "Credentials"), BookmarkNameProvider.toString(bookmark)), new LoginOptions(bookmark.getProtocol()).user(false)); if(input.isPublicKeyAuthentication()) { bookmark.getCredentials().setIdentity(input.getIdentity()); publickey.set(true); return StringUtils.EMPTY.toCharArray(); } bookmark.getCredentials().setSaved(input.isSaved()); bookmark.getCredentials().setPassword(input.getPassword()); } catch(LoginCanceledException e) { canceled.set(true); return StringUtils.EMPTY.toCharArray(); } } return bookmark.getCredentials().getPassword().toCharArray(); } else { final StringAppender message = new StringAppender().append(instruction).append(prompt); final Credentials additional; try { final StringAppender title = new StringAppender().append(name).append( LocaleFactory.localizedString("Provide additional login credentials", "Credentials") ); additional = callback.prompt(bookmark, title.toString(), message.toString(), new LoginOptions() .icon(bookmark.getProtocol().disk()) .password(true) .user(false) .keychain(false) ); } catch(LoginCanceledException e) { canceled.set(true); return StringUtils.EMPTY.toCharArray(); } return additional.getPassword().toCharArray(); } } @Override public boolean shouldRetry() { return false; } })); } catch(IOException e) { if(publickey.get()) { return new SFTPPublicKeyAuthentication(session).authenticate(bookmark, callback, cancel); } if(canceled.get()) { throw new LoginCanceledException(); } throw new SFTPExceptionMappingService().map(e); } return session.getClient().isAuthenticated(); }
SFTPChallengeResponseAuthentication implements AuthenticationProvider<Boolean> { @Override public Boolean authenticate(final Host bookmark, final LoginCallback callback, final CancelCallback cancel) throws BackgroundException { if(log.isDebugEnabled()) { log.debug(String.format("Login using challenge response authentication for %s", bookmark)); } final AtomicBoolean canceled = new AtomicBoolean(); final AtomicBoolean publickey = new AtomicBoolean(); try { session.getClient().auth(bookmark.getCredentials().getUsername(), new AuthKeyboardInteractive(new ChallengeResponseProvider() { private String name = StringUtils.EMPTY; private String instruction = StringUtils.EMPTY; @Override public List<String> getSubmethods() { return Collections.emptyList(); } @Override public void init(final Resource resource, final String name, final String instruction) { if(StringUtils.isNoneBlank(instruction)) { this.instruction = instruction; } if(StringUtils.isNoneBlank(name)) { this.name = name; } } @Override public char[] getResponse(final String prompt, final boolean echo) { if(log.isDebugEnabled()) { log.debug(String.format("Reply to challenge name %s with instruction %s", name, instruction)); } if(DEFAULT_PROMPT_PATTERN.matcher(prompt).matches()) { if(StringUtils.isBlank(bookmark.getCredentials().getPassword())) { try { final Credentials input = callback.prompt(bookmark, bookmark.getCredentials().getUsername(), String.format("%s %s", LocaleFactory.localizedString("Login", "Login"), bookmark.getHostname()), MessageFormat.format(LocaleFactory.localizedString( "Login {0} with username and password", "Credentials"), BookmarkNameProvider.toString(bookmark)), new LoginOptions(bookmark.getProtocol()).user(false)); if(input.isPublicKeyAuthentication()) { bookmark.getCredentials().setIdentity(input.getIdentity()); publickey.set(true); return StringUtils.EMPTY.toCharArray(); } bookmark.getCredentials().setSaved(input.isSaved()); bookmark.getCredentials().setPassword(input.getPassword()); } catch(LoginCanceledException e) { canceled.set(true); return StringUtils.EMPTY.toCharArray(); } } return bookmark.getCredentials().getPassword().toCharArray(); } else { final StringAppender message = new StringAppender().append(instruction).append(prompt); final Credentials additional; try { final StringAppender title = new StringAppender().append(name).append( LocaleFactory.localizedString("Provide additional login credentials", "Credentials") ); additional = callback.prompt(bookmark, title.toString(), message.toString(), new LoginOptions() .icon(bookmark.getProtocol().disk()) .password(true) .user(false) .keychain(false) ); } catch(LoginCanceledException e) { canceled.set(true); return StringUtils.EMPTY.toCharArray(); } return additional.getPassword().toCharArray(); } } @Override public boolean shouldRetry() { return false; } })); } catch(IOException e) { if(publickey.get()) { return new SFTPPublicKeyAuthentication(session).authenticate(bookmark, callback, cancel); } if(canceled.get()) { throw new LoginCanceledException(); } throw new SFTPExceptionMappingService().map(e); } return session.getClient().isAuthenticated(); } }
SFTPChallengeResponseAuthentication implements AuthenticationProvider<Boolean> { @Override public Boolean authenticate(final Host bookmark, final LoginCallback callback, final CancelCallback cancel) throws BackgroundException { if(log.isDebugEnabled()) { log.debug(String.format("Login using challenge response authentication for %s", bookmark)); } final AtomicBoolean canceled = new AtomicBoolean(); final AtomicBoolean publickey = new AtomicBoolean(); try { session.getClient().auth(bookmark.getCredentials().getUsername(), new AuthKeyboardInteractive(new ChallengeResponseProvider() { private String name = StringUtils.EMPTY; private String instruction = StringUtils.EMPTY; @Override public List<String> getSubmethods() { return Collections.emptyList(); } @Override public void init(final Resource resource, final String name, final String instruction) { if(StringUtils.isNoneBlank(instruction)) { this.instruction = instruction; } if(StringUtils.isNoneBlank(name)) { this.name = name; } } @Override public char[] getResponse(final String prompt, final boolean echo) { if(log.isDebugEnabled()) { log.debug(String.format("Reply to challenge name %s with instruction %s", name, instruction)); } if(DEFAULT_PROMPT_PATTERN.matcher(prompt).matches()) { if(StringUtils.isBlank(bookmark.getCredentials().getPassword())) { try { final Credentials input = callback.prompt(bookmark, bookmark.getCredentials().getUsername(), String.format("%s %s", LocaleFactory.localizedString("Login", "Login"), bookmark.getHostname()), MessageFormat.format(LocaleFactory.localizedString( "Login {0} with username and password", "Credentials"), BookmarkNameProvider.toString(bookmark)), new LoginOptions(bookmark.getProtocol()).user(false)); if(input.isPublicKeyAuthentication()) { bookmark.getCredentials().setIdentity(input.getIdentity()); publickey.set(true); return StringUtils.EMPTY.toCharArray(); } bookmark.getCredentials().setSaved(input.isSaved()); bookmark.getCredentials().setPassword(input.getPassword()); } catch(LoginCanceledException e) { canceled.set(true); return StringUtils.EMPTY.toCharArray(); } } return bookmark.getCredentials().getPassword().toCharArray(); } else { final StringAppender message = new StringAppender().append(instruction).append(prompt); final Credentials additional; try { final StringAppender title = new StringAppender().append(name).append( LocaleFactory.localizedString("Provide additional login credentials", "Credentials") ); additional = callback.prompt(bookmark, title.toString(), message.toString(), new LoginOptions() .icon(bookmark.getProtocol().disk()) .password(true) .user(false) .keychain(false) ); } catch(LoginCanceledException e) { canceled.set(true); return StringUtils.EMPTY.toCharArray(); } return additional.getPassword().toCharArray(); } } @Override public boolean shouldRetry() { return false; } })); } catch(IOException e) { if(publickey.get()) { return new SFTPPublicKeyAuthentication(session).authenticate(bookmark, callback, cancel); } if(canceled.get()) { throw new LoginCanceledException(); } throw new SFTPExceptionMappingService().map(e); } return session.getClient().isAuthenticated(); } SFTPChallengeResponseAuthentication(final SFTPSession session); }
SFTPChallengeResponseAuthentication implements AuthenticationProvider<Boolean> { @Override public Boolean authenticate(final Host bookmark, final LoginCallback callback, final CancelCallback cancel) throws BackgroundException { if(log.isDebugEnabled()) { log.debug(String.format("Login using challenge response authentication for %s", bookmark)); } final AtomicBoolean canceled = new AtomicBoolean(); final AtomicBoolean publickey = new AtomicBoolean(); try { session.getClient().auth(bookmark.getCredentials().getUsername(), new AuthKeyboardInteractive(new ChallengeResponseProvider() { private String name = StringUtils.EMPTY; private String instruction = StringUtils.EMPTY; @Override public List<String> getSubmethods() { return Collections.emptyList(); } @Override public void init(final Resource resource, final String name, final String instruction) { if(StringUtils.isNoneBlank(instruction)) { this.instruction = instruction; } if(StringUtils.isNoneBlank(name)) { this.name = name; } } @Override public char[] getResponse(final String prompt, final boolean echo) { if(log.isDebugEnabled()) { log.debug(String.format("Reply to challenge name %s with instruction %s", name, instruction)); } if(DEFAULT_PROMPT_PATTERN.matcher(prompt).matches()) { if(StringUtils.isBlank(bookmark.getCredentials().getPassword())) { try { final Credentials input = callback.prompt(bookmark, bookmark.getCredentials().getUsername(), String.format("%s %s", LocaleFactory.localizedString("Login", "Login"), bookmark.getHostname()), MessageFormat.format(LocaleFactory.localizedString( "Login {0} with username and password", "Credentials"), BookmarkNameProvider.toString(bookmark)), new LoginOptions(bookmark.getProtocol()).user(false)); if(input.isPublicKeyAuthentication()) { bookmark.getCredentials().setIdentity(input.getIdentity()); publickey.set(true); return StringUtils.EMPTY.toCharArray(); } bookmark.getCredentials().setSaved(input.isSaved()); bookmark.getCredentials().setPassword(input.getPassword()); } catch(LoginCanceledException e) { canceled.set(true); return StringUtils.EMPTY.toCharArray(); } } return bookmark.getCredentials().getPassword().toCharArray(); } else { final StringAppender message = new StringAppender().append(instruction).append(prompt); final Credentials additional; try { final StringAppender title = new StringAppender().append(name).append( LocaleFactory.localizedString("Provide additional login credentials", "Credentials") ); additional = callback.prompt(bookmark, title.toString(), message.toString(), new LoginOptions() .icon(bookmark.getProtocol().disk()) .password(true) .user(false) .keychain(false) ); } catch(LoginCanceledException e) { canceled.set(true); return StringUtils.EMPTY.toCharArray(); } return additional.getPassword().toCharArray(); } } @Override public boolean shouldRetry() { return false; } })); } catch(IOException e) { if(publickey.get()) { return new SFTPPublicKeyAuthentication(session).authenticate(bookmark, callback, cancel); } if(canceled.get()) { throw new LoginCanceledException(); } throw new SFTPExceptionMappingService().map(e); } return session.getClient().isAuthenticated(); } SFTPChallengeResponseAuthentication(final SFTPSession session); @Override Boolean authenticate(final Host bookmark, final LoginCallback callback, final CancelCallback cancel); @Override String getMethod(); }
SFTPChallengeResponseAuthentication implements AuthenticationProvider<Boolean> { @Override public Boolean authenticate(final Host bookmark, final LoginCallback callback, final CancelCallback cancel) throws BackgroundException { if(log.isDebugEnabled()) { log.debug(String.format("Login using challenge response authentication for %s", bookmark)); } final AtomicBoolean canceled = new AtomicBoolean(); final AtomicBoolean publickey = new AtomicBoolean(); try { session.getClient().auth(bookmark.getCredentials().getUsername(), new AuthKeyboardInteractive(new ChallengeResponseProvider() { private String name = StringUtils.EMPTY; private String instruction = StringUtils.EMPTY; @Override public List<String> getSubmethods() { return Collections.emptyList(); } @Override public void init(final Resource resource, final String name, final String instruction) { if(StringUtils.isNoneBlank(instruction)) { this.instruction = instruction; } if(StringUtils.isNoneBlank(name)) { this.name = name; } } @Override public char[] getResponse(final String prompt, final boolean echo) { if(log.isDebugEnabled()) { log.debug(String.format("Reply to challenge name %s with instruction %s", name, instruction)); } if(DEFAULT_PROMPT_PATTERN.matcher(prompt).matches()) { if(StringUtils.isBlank(bookmark.getCredentials().getPassword())) { try { final Credentials input = callback.prompt(bookmark, bookmark.getCredentials().getUsername(), String.format("%s %s", LocaleFactory.localizedString("Login", "Login"), bookmark.getHostname()), MessageFormat.format(LocaleFactory.localizedString( "Login {0} with username and password", "Credentials"), BookmarkNameProvider.toString(bookmark)), new LoginOptions(bookmark.getProtocol()).user(false)); if(input.isPublicKeyAuthentication()) { bookmark.getCredentials().setIdentity(input.getIdentity()); publickey.set(true); return StringUtils.EMPTY.toCharArray(); } bookmark.getCredentials().setSaved(input.isSaved()); bookmark.getCredentials().setPassword(input.getPassword()); } catch(LoginCanceledException e) { canceled.set(true); return StringUtils.EMPTY.toCharArray(); } } return bookmark.getCredentials().getPassword().toCharArray(); } else { final StringAppender message = new StringAppender().append(instruction).append(prompt); final Credentials additional; try { final StringAppender title = new StringAppender().append(name).append( LocaleFactory.localizedString("Provide additional login credentials", "Credentials") ); additional = callback.prompt(bookmark, title.toString(), message.toString(), new LoginOptions() .icon(bookmark.getProtocol().disk()) .password(true) .user(false) .keychain(false) ); } catch(LoginCanceledException e) { canceled.set(true); return StringUtils.EMPTY.toCharArray(); } return additional.getPassword().toCharArray(); } } @Override public boolean shouldRetry() { return false; } })); } catch(IOException e) { if(publickey.get()) { return new SFTPPublicKeyAuthentication(session).authenticate(bookmark, callback, cancel); } if(canceled.get()) { throw new LoginCanceledException(); } throw new SFTPExceptionMappingService().map(e); } return session.getClient().isAuthenticated(); } SFTPChallengeResponseAuthentication(final SFTPSession session); @Override Boolean authenticate(final Host bookmark, final LoginCallback callback, final CancelCallback cancel); @Override String getMethod();  }