hammerspoon2-docs
    Preparing search index...

    Namespace keychain

    Store small secrets, such as passwords, API tokens and TOTP seeds, in the macOS keychain, so they don't have to live in your config files.

    Each secret is identified by a service and an account, both strings of your choosing. A good convention is to use a host name or a Spoon's name as the service, and a user name as the account.

    Secrets are kept in Hammerspoon 2's own part of the keychain (its keychain access group). Other apps can't read, list or even see them, and Hammerspoon 2 can't see any other app's items, such as the passwords saved in the Passwords app. Bear in mind that any code running in Hammerspoon 2, including every Spoon you load, can read every secret stored here, unless it's stored with requireUserPresence.

    Secrets can be read while the screen is locked, once you've logged in, so timers and watchers can use them. Reading a secret is asynchronous, because macOS may need to ask you to authenticate first.

    Errors are thrown (or rejected, for get()) as Errors with a code property:

    Code Meaning
    EINVALIDARG An argument or option is missing or malformed
    ENOENTITLEMENT This copy of Hammerspoon 2 can't use the keychain, see isAvailable
    ECANCELED You cancelled the authentication prompt
    EAUTH Authentication failed
    EINTERACTION The keychain needed to ask you something, but couldn't
    EUNAVAILABLE The keychain isn't available, e.g. before you've logged in
    ENOENT A file to import can't be read
    EEXIST A certificate or private key to import is already stored under another name
    EKEYCHAIN Any other keychain error. The message includes its OSStatus, which is also in the status property

    Store a secret once, from the Console:

    hs.keychain.set("api.example.com", "me", "my-api-token")
    

    Then use it in your config:

    hs.keychain.get("api.example.com", "me").then(token => {
    return fetch("https://api.example.com/things", {
    headers: { Authorization: `Bearer ${token}` }
    })
    }).then(response => response.json())
    .then(things => console.log(`${things.length} things`))
    .catch(err => console.error(err.message))
    const user = "me"
    hs.keychain.get("intranet.example.com", user).then(password => {
    return fetch("https://intranet.example.com/status", {
    headers: { Authorization: "Basic " + hs.hashing.base64Encode(`${user}:${password}`) }
    })
    }).then(response => console.log(`Status: ${response.status}`))
    .catch(err => console.error(err.message))

    Certificates, and certificates with their private keys (identities), can be stored under a name of your choosing, and then used by name for TLS, so neither the certificate files nor their passphrases have to stay on disk or in your config:

    • hs.httpserver's setTLSFromKeychain(name) and hs.socket's enableServerTLSFromKeychain(name) serve TLS with an identity
    • hs.mqtt's caCertificate: "keychain:<name>" option trusts certificates, and its clientCertificate: "keychain:<name>" option identifies the client with an identity

    Those look for a certificate stored here first, then for one in your login keychain.

    // Once, from the Console:
    hs.keychain.importIdentity("my server", "~/Downloads/server.p12", "the passphrase")

    // Then in your config:
    const server = hs.httpserver.create().setPort(8443).setTLSFromKeychain("my server")
    isAvailable
    certificates
    get
    importCertificate
    importIdentity
    items
    remove
    removeCertificate
    set