diff --git a/src/main/java/net/schmizz/sshj/userauth/password/ConsolePasswordFinder.java b/src/main/java/net/schmizz/sshj/userauth/password/ConsolePasswordFinder.java new file mode 100644 index 00000000..f7456907 --- /dev/null +++ b/src/main/java/net/schmizz/sshj/userauth/password/ConsolePasswordFinder.java @@ -0,0 +1,52 @@ +/* + * Copyright (C)2009 - SSHJ Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.schmizz.sshj.userauth.password; + +import java.io.Console; + +/** A PasswordFinder that reads a password from a console */ +public class ConsolePasswordFinder implements PasswordFinder { + + private final Console console; + + /** + * Initializes with the System Console, which will be null if not run from an interactive shell. + */ + public ConsolePasswordFinder() { + this(System.console()); + } + + /** + * @param console the console to read the password from. May be null. + */ + public ConsolePasswordFinder(Console console) { + this.console = console; + } + + @Override + public char[] reqPassword(Resource resource) { + if (console == null) { + // the request cannot be serviced + return null; + } + return console.readPassword("Enter passphrase for %s:", resource.toString()); + } + + @Override + public boolean shouldRetry(Resource resource) { + return true; + } +} diff --git a/src/test/java/net/schmizz/sshj/userauth/password/TestConsolePasswordFinder.java b/src/test/java/net/schmizz/sshj/userauth/password/TestConsolePasswordFinder.java new file mode 100644 index 00000000..3af5e204 --- /dev/null +++ b/src/test/java/net/schmizz/sshj/userauth/password/TestConsolePasswordFinder.java @@ -0,0 +1,36 @@ +/* + * Copyright (C)2009 - SSHJ Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.schmizz.sshj.userauth.password; + +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +public class TestConsolePasswordFinder { + + /* + * Note that Mockito 1.9 cannot mock Console because it is a final class, + * so there are no other tests. + */ + + @Test + public void testReqPasswordNullConsole() { + char[] password = new ConsolePasswordFinder(null) + .reqPassword(Mockito.mock(Resource.class)); + Assert.assertNull("Password should be null with null console", password); + } + +}