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
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: constserver = hs.httpserver.create().setPort(8443).setTLSFromKeychain("my server")
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
serviceand anaccount, 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()) asErrors with acodeproperty:EINVALIDARGENOENTITLEMENTisAvailableECANCELEDEAUTHEINTERACTIONEUNAVAILABLEENOENTEEXISTEKEYCHAINOSStatus, which is also in thestatuspropertyQuick start
Store a secret once, from the Console:
Then use it in your config:
HTTP basic authentication
Certificates
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'ssetTLSFromKeychain(name)andhs.socket'senableServerTLSFromKeychain(name)serve TLS with an identityhs.mqtt'scaCertificate: "keychain:<name>"option trusts certificates, and itsclientCertificate: "keychain:<name>"option identifies the client with an identityThose look for a certificate stored here first, then for one in your login keychain.