mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-07 15:50:57 +03:00
more versatile password response provider
This commit is contained in:
@@ -20,6 +20,8 @@ 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;
|
||||||
|
|
||||||
@@ -30,12 +32,21 @@ public class PasswordResponseProvider
|
|||||||
|
|
||||||
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 =
|
||||||
|
Collections.unmodifiableCollection(Arrays.asList("Password:"));
|
||||||
|
|
||||||
|
private final Collection<String> acceptablePrompts;
|
||||||
private final PasswordFinder pwdf;
|
private final PasswordFinder pwdf;
|
||||||
private Resource resource;
|
private Resource resource;
|
||||||
private boolean gaveOnce;
|
private boolean gaveAlready;
|
||||||
|
|
||||||
public PasswordResponseProvider(PasswordFinder pwdf) {
|
public PasswordResponseProvider(PasswordFinder pwdf) {
|
||||||
|
this(pwdf, DEFAULT_ACCEPTABLE_PROMPTS);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PasswordResponseProvider(PasswordFinder pwdf, Collection<String> acceptablePrompts) {
|
||||||
this.pwdf = pwdf;
|
this.pwdf = pwdf;
|
||||||
|
this.acceptablePrompts = acceptablePrompts;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -46,14 +57,13 @@ public class PasswordResponseProvider
|
|||||||
@Override
|
@Override
|
||||||
public void init(Resource resource, String name, String instruction) {
|
public void init(Resource resource, String name, String instruction) {
|
||||||
this.resource = resource;
|
this.resource = resource;
|
||||||
log.debug("name=`{}`; instruction=`{}`", name, instruction);
|
log.debug("Challenge - name=`{}`; instruction=`{}`", name, instruction);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public char[] getResponse(String prompt, boolean echo) {
|
public char[] getResponse(String prompt, boolean echo) {
|
||||||
if (!gaveOnce && !echo && prompt.toLowerCase().contains("password")) {
|
if (!gaveAlready && !echo && acceptablePrompts.contains(prompt)) {
|
||||||
gaveOnce = true;
|
gaveAlready = true;
|
||||||
log.debug("prompt = `{}`", prompt);
|
|
||||||
return pwdf.reqPassword(resource);
|
return pwdf.reqPassword(resource);
|
||||||
}
|
}
|
||||||
return EMPTY_RESPONSE;
|
return EMPTY_RESPONSE;
|
||||||
|
|||||||
Reference in New Issue
Block a user