-
Notifications
You must be signed in to change notification settings - Fork 601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ConsolePasswordFinder to read from Console #338
Merged
hierynomus
merged 4 commits into
hierynomus:master
from
matthew-dailey:console-password-finder
Jul 10, 2017
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
67c47a8
Add ConsolePasswordFinder to read from Console
matthew-dailey 6ed109f
Upgraded Mockito, and added message and retries to ConsolePasswordFinder
matthew-dailey 6f85541
Added assertions to testPromptFormat
matthew-dailey b136fd5
Removed Builder, and fixed call to checkFormatString
matthew-dailey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/main/java/net/schmizz/sshj/userauth/password/ConsolePasswordFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* 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; | ||
import java.util.IllegalFormatException; | ||
|
||
/** A PasswordFinder that reads a password from a console */ | ||
public class ConsolePasswordFinder implements PasswordFinder { | ||
|
||
private final Console console; | ||
private final String promptFormat; | ||
private final int maxTries; | ||
|
||
private int numTries; | ||
|
||
public static ConsolePasswordFinderBuilder builder() { | ||
return new ConsolePasswordFinderBuilder(); | ||
} | ||
|
||
public ConsolePasswordFinder(Console console, String promptFormat, int maxTries) { | ||
this.console = console; | ||
this.promptFormat = promptFormat; | ||
this.maxTries = maxTries; | ||
this.numTries = 0; | ||
} | ||
|
||
@Override | ||
public char[] reqPassword(Resource<?> resource) { | ||
numTries++; | ||
if (console == null) { | ||
// the request cannot be serviced | ||
return null; | ||
} | ||
return console.readPassword(promptFormat, resource.toString()); | ||
} | ||
|
||
@Override | ||
public boolean shouldRetry(Resource<?> resource) { | ||
return numTries < maxTries; | ||
} | ||
|
||
public static class ConsolePasswordFinderBuilder { | ||
private Console console; | ||
private String promptFormat; | ||
private int maxTries; | ||
|
||
/** Builder constructor should only be called from parent class */ | ||
private ConsolePasswordFinderBuilder() { | ||
console = System.console(); | ||
promptFormat = "Enter passphrase for %s:"; | ||
maxTries = 3; | ||
} | ||
|
||
public ConsolePasswordFinder build() { | ||
return new ConsolePasswordFinder(console, promptFormat, maxTries); | ||
} | ||
|
||
public ConsolePasswordFinderBuilder setConsole(Console console) { | ||
this.console = console; | ||
return this; | ||
} | ||
|
||
public Console getConsole() { | ||
return console; | ||
} | ||
|
||
/** | ||
* @param promptFormat a StringFormatter string that may contain up to one "%s" | ||
*/ | ||
public ConsolePasswordFinderBuilder setPromptFormat(String promptFormat) { | ||
checkFormatString(promptFormat); | ||
this.promptFormat = promptFormat; | ||
return this; | ||
} | ||
|
||
public String getPromptFormat() { | ||
return promptFormat; | ||
} | ||
|
||
public ConsolePasswordFinderBuilder setMaxTries(int maxTries) { | ||
this.maxTries = maxTries; | ||
return this; | ||
} | ||
|
||
public int getMaxTries() { | ||
return maxTries; | ||
} | ||
|
||
private static void checkFormatString(String promptFormat) { | ||
try { | ||
String.format(promptFormat, ""); | ||
} catch (IllegalFormatException e) { | ||
throw new IllegalArgumentException("promptFormat must have no more than one %s and no other markers", e); | ||
} | ||
} | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
src/test/java/net/schmizz/sshj/userauth/password/TestConsolePasswordFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* 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; | ||
|
||
import java.io.Console; | ||
|
||
public class TestConsolePasswordFinder { | ||
|
||
@Test | ||
public void testReqPassword() { | ||
char[] expectedPassword = "password".toCharArray(); | ||
|
||
Console console = Mockito.mock(Console.class); | ||
Mockito.when(console.readPassword(Mockito.anyString(), Mockito.any())) | ||
.thenReturn(expectedPassword); | ||
|
||
Resource resource = Mockito.mock(Resource.class); | ||
char[] password = ConsolePasswordFinder.builder() | ||
.setConsole(console) | ||
.build() | ||
.reqPassword(resource); | ||
|
||
Assert.assertArrayEquals("Password should match mocked return value", | ||
expectedPassword, password); | ||
Mockito.verifyNoMoreInteractions(resource); | ||
} | ||
|
||
@Test | ||
public void testReqPasswordNullConsole() { | ||
Resource<?> resource = Mockito.mock(Resource.class); | ||
char[] password = ConsolePasswordFinder.builder() | ||
.setConsole(null) | ||
.build() | ||
.reqPassword(resource); | ||
|
||
Assert.assertNull("Password should be null with null console", password); | ||
Mockito.verifyNoMoreInteractions(resource); | ||
} | ||
|
||
@Test | ||
public void testShouldRetry() { | ||
Resource<String> resource = new PrivateKeyStringResource(""); | ||
ConsolePasswordFinder finder = ConsolePasswordFinder.builder() | ||
.setConsole(null) | ||
.setMaxTries(1) | ||
.build(); | ||
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)); | ||
} | ||
|
||
@Test | ||
public void testPromptFormat() { | ||
Assert.assertNotNull( | ||
"Empty format should create valid ConsolePasswordFinder", | ||
ConsolePasswordFinder.builder().setPromptFormat("").build()); | ||
Assert.assertNotNull( | ||
"Single-string format should create valid ConsolePasswordFinder", | ||
ConsolePasswordFinder.builder().setPromptFormat("%s").build()); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testPromptFormatTooManyMarkers() { | ||
ConsolePasswordFinder.builder().setPromptFormat("%s%s"); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testPromptFormatWrongMarkerType() { | ||
ConsolePasswordFinder.builder().setPromptFormat("%d"); | ||
} | ||
|
||
} |
2 changes: 2 additions & 0 deletions
2
src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# incubating feature to allow mocking final classes | ||
mock-maker-inline |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're not checking the format of the prompt here, but the constructor is public.
I don't think we actually need the builder, it's just 3 parameters. I'm fine with using just a constructor.
In any case we need to do the check here also.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! OK, I'll take out the builder (good to have less code to maintain) and put the check here