@Override public void init(Reader location) { assert location != null; resource = new PrivateKeyReaderResource(location); }
public ConsolePasswordFinder(Console console, String promptFormat, int maxTries) { checkFormatString(promptFormat); this.console = console; this.promptFormat = promptFormat; this.maxTries = maxTries; this.numTries = 0; }
@Override public void init(String privateKey, String publicKey) { assert privateKey != null; assert publicKey == null; resource = new PrivateKeyStringResource(privateKey); }
@Test public void testShouldRetry() { Resource<String> resource = new PrivateKeyStringResource(""); ConsolePasswordFinder finder = new ConsolePasswordFinder(null, FORMAT, 1); Assert.assertTrue("Should allow a retry at first", finder.shouldRetry(resource)); finder.reqPassword(resource); Assert.assertFalse("Should stop allowing retries after one interaction", finder.shouldRetry(resource)); }
/** * Utility function for createing a {@link KeyProvider} instance from given location on the file system. Creates a * one-off {@link PasswordFinder} using {@link PasswordUtils#createOneOff(char[])}, and calls {@link * #loadKeys(String, PasswordFinder)}. * * @param location location of the key file * @param passphrase passphrase as a char-array * * @return the key provider ready for use in authentication * * @throws SSHException if there was no suitable key provider available for the file format; typically because * BouncyCastle is not in the classpath * @throws IOException if the key file format is not known, if the file could not be read, etc. */ public KeyProvider loadKeys(String location, char[] passphrase) throws IOException { return loadKeys(location, PasswordUtils.createOneOff(passphrase)); }
/** * Returns {@code true} if the associated {@link PasswordFinder} tells that we should retry with a new password that * it will supply. */ @Override public boolean shouldRetry() { AccountResource accountResource = makeAccountResource(); return newPasswordProvider.shouldRetry(accountResource) || pwdf.shouldRetry(accountResource); }
protected AccountResource makeAccountResource() { return new AccountResource(params.getUsername(), params.getTransport().getRemoteHost()); }
@Override public Reader getReader() throws IOException { return getDetail(); } }
@Override public boolean shouldRetry() { return pwdf.shouldRetry(resource); }
@Override public void init(File location) { assert location != null; resource = new PrivateKeyFileResource(location.getAbsoluteFile()); }
@Override public Reader getReader() throws IOException { return new StringReader(getDetail()); }
@Override public Reader getReader() throws IOException { return new StringReader(getDetail()); } }
@Override public char[] getResponse(String prompt, boolean echo) { if (!echo && promptPattern.matcher(prompt).matches()) { return pwdf.reqPassword(resource); } return EMPTY_RESPONSE; }
@Override public Reader getReader() throws IOException { return new InputStreamReader(new FileInputStream(getDetail()), "UTF-8"); } }
@Override public char[] reqPassword(Resource<?> resource) { char[] cloned = password.clone(); blankOut(password); return cloned; }
@Override public char[] reqPassword(Resource<?> resource) { numTries++; if (console == null) { // the request cannot be serviced return null; } return console.readPassword(promptFormat, resource.toString()); }
@Test public void testReqPasswordNullConsole() { Resource<?> resource = Mockito.mock(Resource.class); char[] password = new ConsolePasswordFinder(null, FORMAT, 1).reqPassword(resource); Assert.assertNull("Password should be null with null console", password); Mockito.verifyNoMoreInteractions(resource); }
@Test(expected = IllegalArgumentException.class) public void testPromptFormatTooManyMarkers() { new ConsolePasswordFinder(null, "%s%s", 1); }
@Test(expected = IllegalArgumentException.class) public void testPromptFormatWrongMarkerType() { new ConsolePasswordFinder(null, "%d", 1); }
@Test public void testPromptFormat() { Assert.assertNotNull( "Empty format should create valid ConsolePasswordFinder", new ConsolePasswordFinder(null, "", 1)); Assert.assertNotNull( "Single-string format should create valid ConsolePasswordFinder", new ConsolePasswordFinder(null, FORMAT, 1)); }