use of com.sshtools.j2ssh.authentication.KBIPrompt in project iaf by ibissource.
the class FtpSession method openSftpClient.
private void openSftpClient(String remoteDirectory) throws FtpConnectException {
try {
// Set the connection properties and if necessary the proxy properties
SshConnectionProperties sshProp = new SshConnectionProperties();
sshProp.setHost(host);
sshProp.setPort(port);
if (StringUtils.isNotEmpty(prefCSEncryption))
sshProp.setPrefCSEncryption(prefCSEncryption);
if (StringUtils.isNotEmpty(prefSCEncryption))
sshProp.setPrefCSEncryption(prefSCEncryption);
if (!StringUtils.isEmpty(proxyHost)) {
sshProp.setTransportProvider(proxyTransportType);
sshProp.setProxyHost(proxyHost);
sshProp.setProxyPort(proxyPort);
CredentialFactory pcf = new CredentialFactory(getProxyAuthAlias(), proxyUsername, proxyPassword);
if (!StringUtils.isEmpty(pcf.getUsername())) {
sshProp.setProxyUsername(pcf.getUsername());
sshProp.setProxyPassword(pcf.getPassword());
}
}
// make a secure connection with the remote host
sshClient = new SshClient();
if (StringUtils.isNotEmpty(knownHostsPath)) {
AbstractKnownHostsKeyVerification hv = null;
if (consoleKnownHostsVerifier) {
hv = new ConsoleKnownHostsKeyVerification(knownHostsPath);
} else {
hv = new SftpHostVerification(knownHostsPath);
}
sshClient.connect(sshProp, hv);
} else {
sshClient.connect(sshProp, new IgnoreHostKeyVerification());
}
SshAuthenticationClient sac;
if (!isKeyboardInteractive()) {
// pass the authentication information
sac = getSshAuthentication();
} else {
// TODO: detecteren dat sshClient.getAvailableAuthMethods("ftpmsg")
// wel keyboard-interactive terug geeft, maar geen password en dan deze methode
// gebruiken
final CredentialFactory credentialFactory = new CredentialFactory(getAuthAlias(), getUsername(), getPassword());
KBIAuthenticationClient kbiAuthenticationClient = new KBIAuthenticationClient();
kbiAuthenticationClient.setUsername(credentialFactory.getUsername());
kbiAuthenticationClient.setKBIRequestHandler(new KBIRequestHandler() {
public void showPrompts(String name, String instruction, KBIPrompt[] prompts) {
// deze 3 regels in x.zip naar Zenz gemaild, hielp ook niet
if (prompts == null) {
return;
}
for (int i = 0; i < prompts.length; i++) {
prompts[i].setResponse(credentialFactory.getPassword());
}
}
});
sac = kbiAuthenticationClient;
}
int result = sshClient.authenticate(sac);
if (result != AuthenticationProtocolState.COMPLETE) {
closeSftpClient();
throw new IOException("Could not authenticate to sftp server " + result);
}
// use the connection for sftp
sftpClient = sshClient.openSftpClient();
if (!StringUtils.isEmpty(remoteDirectory)) {
sftpClient.cd(remoteDirectory);
}
} catch (Exception e) {
closeSftpClient();
throw new FtpConnectException(e);
}
}
Aggregations