From ddee3ef6a00f57f14a0731fc04514a03a0109350 Mon Sep 17 00:00:00 2001 From: Huy Doan Date: Wed, 21 Feb 2024 18:52:51 +0700 Subject: [PATCH] closes #15 --- src/ssh2.nim | 6 +++--- src/ssh2/private/session.nim | 12 ++++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/ssh2.nim b/src/ssh2.nim index 4530ae2..b0cc676 100644 --- a/src/ssh2.nim +++ b/src/ssh2.nim @@ -18,7 +18,7 @@ proc disconnect*(ssh: SSHClient) = ssh.socket.close() libssh2.exit() -proc connect*(s: SSHClient, hostname: string, username: string, port = Port(22), password = "", pkey = "", useAgent = false) {.async.} = +proc connect*(s: SSHClient, hostname: string, username: string, port = Port(22), password = "", privKey = "", pubKey = "", useAgent = false) {.async.} = s.socket = newAsyncSocket() await s.socket.connect(hostname, port) s.session = initSession() @@ -35,8 +35,8 @@ proc connect*(s: SSHClient, hostname: string, username: string, port = Port(22), break agent.close_agent() else: - if pkey.len != 0: - discard s.session.authPublicKey(username, pkey, password) + if privKey.len != 0: + discard s.session.authPublicKey(username, privKey, pubKey, password) else: discard s.session.authPassword(username, password) diff --git a/src/ssh2/private/session.nim b/src/ssh2/private/session.nim index bfea343..f806f49 100644 --- a/src/ssh2/private/session.nim +++ b/src/ssh2/private/session.nim @@ -31,14 +31,18 @@ proc authPassword*(session: Session, username, password: string): bool = break result = true -proc authPublicKey*(session: Session; username, pkey: string, passphrase = ""): bool = - let pkey = expandTilde(pkey) +proc authPublicKey*(session: Session; username, privKey: string, pubKey = "", passphrase = ""): bool = + let privKey = expandTilde(privKey) + var pubKey = pubKey + if pubKey.len > 0: + pubKey = expandTilde(pubKey) + while true: - let rc = session.userauth_publickey_from_file(username, nil, pkey, passphrase) + let rc = session.userauth_publickey_from_file(username, pubKey, privKey, passphrase) if rc == LIBSSH2_ERROR_EAGAIN: discard elif rc < 0: - raise newException(AuthenticationException, &"Authentication with privateKey {pkey} failed!") + raise newException(AuthenticationException, &"Authentication with privateKey {privKey} failed!") else: break result = true