JFLib

20. Secrets (passwords, tokens, keys)#

plat::secret (SECRETS.md) stores small secrets in the operating system's store instead of a file: the Credential Manager on Windows, the Secret Service (gnome-keyring, KWallet, KeePassXC) on Linux, the Android Keystore on Android; in the browser the web host encrypts them with a WebCrypto key that cannot be exported (IndexedDB). The app needs secrets in its PERMISSIONS.

use plat::secret::{secret_available, secret_delete, secret_get, secret_set};

if secret_available() && self.remember {
    if let Err(e) = secret_set("imap.example.org", &self.user, self.password.as_bytes()) {
        self.error = Some(format!("Das Passwort wurde nicht gespeichert ({e})."));
    }
}
let saved: Option<Vec<u8>> = secret_get("imap.example.org", &self.user);
let _ = secret_delete("imap.example.org", &self.user); // when the user signs out
  • An entry is the app's (vfs::app_id()) plus a service and an account ("" for one value per service); values are bytes up to 64 KiB. secret_set replaces, secret_get gives None for a missing entry and on errors, secret_get_checked tells them apart.
  • Without a store (WSL, a minimal Linux, a page without a secure context) every call fails with SecretError::Unavailable -- nothing is ever written to a plain file. Offer "remember" only when secret_available() says so.
  • In the browser every script of the page's origin can use the key: the store keeps values from other origins and from readers of the profile's storage, not from the page itself.
  • A locked keyring asks the user to unlock it, and the call waits for the answer (at most five minutes): call it from a plat::Job when the window must keep drawing. A dismissed dialog is SecretError::Dismissed.
  • Other programs of the same user can read the store too; it protects against other users and stolen disks, not against malware running as the user.

Testing: plattest --test secret (the real store; its entries are removed again), tools/platshared/secret_bus.py (a fake Secret Service in WSL).

Source: docs/GUIDE.md in the JFLib repository.