I am attempting to connect to an SFTP server that requires private key authentication and wanting to use the Mina. Looking through the documentation, I can see how to perform that authentication using password authentication but not private key authentication. I do not see any example code that would demonstrate how to perform private key authentication using mina.
Is it currently possible with this library, and if so, can you provide an example code on how to load the key and perform the connection?
Here is an example of what I want to do using SSHTools for reference.
private static void authenticate(Ssh2Client ssh2, String host, Integer port, String username, InputStream privateKey) {
Ssh2PublicKeyAuthentication auth = createKeyAuthentication(privateKey);
try {
int result = ssh2.authenticate(auth);
if (result != SshAuthentication.COMPLETE) {
throw new AuthenticationIncomplete(host, port, username, result);
}
} catch (SshException ex) {
throw new UnableToAuthenticate(host, port, username, ex);
}
}
private static Ssh2PublicKeyAuthentication createKeyAuthentication(InputStream privateKey) {
try {
SshPrivateKeyFile privateKeyFile = SshPrivateKeyFileFactory.parse(StreamUtil.readIntoByteArray(privateKey));
SshKeyPair keyPair = privateKeyFile.toKeyPair("");
Ssh2PublicKeyAuthentication auth = new Ssh2PublicKeyAuthentication();
auth.setPrivateKey(keyPair.getPrivateKey());
auth.setPublicKey(keyPair.getPublicKey());
return auth;
} catch (IOException | InvalidPassphraseException ex) {
throw new ConfigurationIssue(ex);
}
}