No description
Find a file
2024-02-22 21:38:36 -05:00
.github/workflows Typo 2024-02-22 21:36:24 -05:00
changes Bump to v0.4.2 2024-02-22 21:38:36 -05:00
src Annotate dbus.append calls with {.gcsafe.} 2024-02-22 21:28:25 -05:00
tests Add keyringAvailable 2023-01-05 23:27:23 -05:00
.gitignore Initial commit with support for macOS and Windows 2019-12-19 22:03:18 -05:00
CHANGELOG.md Bump to v0.4.2 2024-02-22 21:38:36 -05:00
keyring.nimble Bump to v0.4.2 2024-02-22 21:38:36 -05:00
LICENSE.md README and LICENSE 2019-12-19 22:10:11 -05:00
README.md Add keyringAvailable 2023-01-05 23:27:23 -05:00

keyring

SECURITY NOTE: Though an effort has been made to ensure secret confidentiality and memory safety, this library has not undergone strenuous security testing. Use it at your own risk.

This is a Nim library that provides access to the operating system keyring. It uses the following backends:

Usage

nimble install keyring
import keyring

assert keyringAvailable()
setPassword("my-service", "myuser", "secretpassword")
assert getPassword("my-service", "myuser").get() == "secretpassword"
deletePassword("my-service", "myuser")

Note that getPassword(...) returns Option[string], so you can check whether a password was previously saved with .isNone()/.isSome().

Error handling

All 3 procs can raise KeyringFailed or KeyringNotSupported in the case of an error. Some errors are transient, such as if the user doesn't enter the right password. Other errors are more permanent (e.g. keychain software isn't installed on Linux). Use keyringAvailable() to detect if the keyring works for the current system.

Additionally, different OS implementations might raise other errors (e.g. DBus erros on Linux), so the following is a more complete example that handles errors:

import keyring

if not keyringAvailable():
  echo "Keyring is not going to work."

try:
  setPassword("my-service", "myuser", "secret")
except KeyringFailed:
  discard
except:
  discard

var password:string
try:
  let ret = getPassword("my-service", "myuser")
  if ret.isSome:
    password = ret.get()
except KeyringFailed:
  discard
except:
  discard

try:
  deletePassword("my-service", "myuser")
except KeyringFailed:
  discard
except:
  discard