Skip to content

Commit

Permalink
closes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
ba0f3 committed Feb 21, 2024
1 parent 917c78f commit ddee3ef
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/ssh2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)

Expand Down
12 changes: 8 additions & 4 deletions src/ssh2/private/session.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ddee3ef

Please sign in to comment.