Authentication by Private Keys
Closed this issue ยท 4 comments
This is a huge amount of work but I just cannot use it without support for private key auth. Not that familiar with FFI but looks like a private key Struct needs to be included and things would just work.
- Simple way to auth from private key file
Not sure if you are still supporting this work but thought I would ask!
Thanks
Colin
So I did manage to get things working will post an example once it's a bit neater..
Again fab work.
Colin
Example of using private key for auth based on the examples in the repo..
https://gist.github.com/cconstab/11171392833db9a34a6f2da3ebacdc6b
@cconstab
thanks, glad you managed to do what you wanted, I use this package in a personal internal project and it has worked well for me, feel free if you want to contribute a pull request to add other features
I'll leave your example here so I can help others who have this problem
import 'dart:ffi';
import 'dart:io';
import 'package:ffi/ffi.dart';
import 'package:libssh_binding/libssh_binding.dart';
import 'dart:ffi' as ffi;
import 'package:path/path.dart' as path;
void main() async {
// Some vars (change to suit)
String remoteFile = "/home/cconstab/testfile";
String localFile = "./testfile";
String sshKeyFile = "/home/cconstab/.ssh/GitHub_rsa";
var host = "192.168.1.149";
var port = 22;
var username = "cconstab";
// Open the dynamic library
var libraryPath = path.join('/lib/x86_64-linux-gnu/', 'libssh.so.4');
final dll = ffi.DynamicLibrary.open(libraryPath);
var libssh = LibsshBinding(dll);
// Set up session
var mySshSession = libssh.ssh_new();
libssh.ssh_options_set(mySshSession, ssh_options_e.SSH_OPTIONS_HOST, stringToNativeVoid(host));
libssh.ssh_options_set(mySshSession, ssh_options_e.SSH_OPTIONS_PORT, intToNativeVoid(port));
libssh.ssh_options_set(mySshSession, ssh_options_e.SSH_OPTIONS_USER, stringToNativeVoid(username));
// Connect to server
var rc = libssh.ssh_connect(mySshSession);
if (rc != SSH_OK) {
print('Error connecting to host: $host\n');
}
ffi.Pointer<ffi.Pointer<ssh_key_struct>> sshKey = calloc();
rc = libssh.ssh_pki_import_privkey_file(
stringToNativeInt8(sshKeyFile), nullptr, nullptr, nullptr, sshKey);
rc = libssh.ssh_userauth_publickey(mySshSession, stringToNativeInt8(username), sshKey[0]);
if (rc != ssh_auth_e.SSH_AUTH_SUCCESS) {
var error = libssh.ssh_get_error(mySshSession.cast());
print("Error authenticating with Keys:$error\n");
libssh.ssh_disconnect(mySshSession);
libssh.ssh_free(mySshSession);
exit(-1);
}
await libssh.sftpDownloadFileTo(
mySshSession, stringToNativeInt8(remoteFile), path.join(Directory.current.path, localFile));
libssh.ssh_disconnect(mySshSession);
libssh.ssh_free(mySshSession);
exit(0);
}
Awesome thank you