mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-06 15:20:54 +03:00
Per #77 use regex matching inside PasswordResponseProvider. Also remove the 'gaveAlready' state, we can leave such logic to the PasswordFinder to implement if needed.
This commit is contained in:
@@ -20,33 +20,31 @@ import net.schmizz.sshj.userauth.password.Resource;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class PasswordResponseProvider
|
public class PasswordResponseProvider
|
||||||
implements ChallengeResponseProvider {
|
implements ChallengeResponseProvider {
|
||||||
|
|
||||||
|
public static final Pattern DEFAULT_PROMPT_PATTERN = Pattern.compile(".*[pP]assword:\\s?\\z");
|
||||||
|
|
||||||
private final Logger log = LoggerFactory.getLogger(getClass());
|
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
private static final char[] EMPTY_RESPONSE = new char[0];
|
private static final char[] EMPTY_RESPONSE = new char[0];
|
||||||
|
|
||||||
private static final Collection<String> DEFAULT_ACCEPTABLE_PROMPTS =
|
private final Pattern promptPattern;
|
||||||
Collections.unmodifiableCollection(Arrays.asList("Password:", "Password: "));
|
|
||||||
|
|
||||||
private final Collection<String> acceptablePrompts;
|
|
||||||
private final PasswordFinder pwdf;
|
private final PasswordFinder pwdf;
|
||||||
|
|
||||||
private Resource resource;
|
private Resource resource;
|
||||||
private boolean gaveAlready;
|
|
||||||
|
|
||||||
public PasswordResponseProvider(PasswordFinder pwdf) {
|
public PasswordResponseProvider(PasswordFinder pwdf) {
|
||||||
this(pwdf, DEFAULT_ACCEPTABLE_PROMPTS);
|
this(pwdf, DEFAULT_PROMPT_PATTERN);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PasswordResponseProvider(PasswordFinder pwdf, Collection<String> acceptablePrompts) {
|
public PasswordResponseProvider(PasswordFinder pwdf, Pattern promptPattern) {
|
||||||
this.pwdf = pwdf;
|
this.pwdf = pwdf;
|
||||||
this.acceptablePrompts = acceptablePrompts;
|
this.promptPattern = promptPattern;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -62,8 +60,7 @@ public class PasswordResponseProvider
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public char[] getResponse(String prompt, boolean echo) {
|
public char[] getResponse(String prompt, boolean echo) {
|
||||||
if (!gaveAlready && !echo && acceptablePrompts.contains(prompt)) {
|
if (!echo && promptPattern.matcher(prompt).matches()) {
|
||||||
gaveAlready = true;
|
|
||||||
return pwdf.reqPassword(resource);
|
return pwdf.reqPassword(resource);
|
||||||
}
|
}
|
||||||
return EMPTY_RESPONSE;
|
return EMPTY_RESPONSE;
|
||||||
|
|||||||
Reference in New Issue
Block a user