keyring
Store and retrieve secrets in the operating system's keyring, so API keys and tokens can be stored without plaintext credential files.
import { setSecret, getSecret, deleteSecret, isKeyringAvailable } from "std::auth/keyring"
node main() {
if (isKeyringAvailable()) {
setSecret("my-api-key", "sk-abc123")
const key = getSecret("my-api-key")
print(key)
deleteSecret("my-api-key")
}
}On macOS this uses the Keychain, and on Linux the Secret Service. Windows is not yet supported, so fall back to environment variables there. Secrets live under the "agency-lang" service name unless you pass a custom service.
Effects
std::setSecret
@always(service, key)
effect std::setSecret {
key: string;
service: string
}(source)
std::getSecret
@always(service, key)
effect std::getSecret {
key: string;
service: string
}(source)
std::deleteSecret
@always(service, key)
effect std::deleteSecret {
key: string;
service: string
}(source)
Functions
setSecret
setSecret(
key: string,
value: string,
service: string = "agency-lang",
): Result raises <std::setSecret>Store a secret in the system keyring, replacing any existing value in place. The secret goes to the keyring on stdin, never on a command line, and must be one line.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| key | string | A non-empty name identifying the secret within its service. | |
| value | string | The non-empty secret to store. | |
| service | string | "agency-lang" | The namespace shared by calls that store and retrieve this secret. |
Returns: Result
Throws: std::setSecret
Needs: subprocess
(source)
getSecret
getSecret(
key: string,
service: string = "agency-lang",
): Result raises <std::getSecret>Retrieve a secret from the system keyring, or null when it is missing. Fails when the keyring couldn't be read, for example when it's locked or access was refused.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| key | string | The name used when storing the secret. | |
| service | string | "agency-lang" | The namespace used when storing the secret. |
Returns: Result
Throws: std::getSecret
Needs: subprocess
(source)
deleteSecret
deleteSecret(
key: string,
service: string = "agency-lang",
): Result raises <std::deleteSecret>Delete a secret from the system keyring, returning whether it existed.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| key | string | The name used when storing the secret. | |
| service | string | "agency-lang" | The namespace used when storing the secret. |
Returns: Result
Throws: std::deleteSecret
Needs: subprocess
(source)
isKeyringAvailable
isKeyringAvailable(): booleanCheck whether this platform has a supported keyring command available.
Checks whether macOS security or Linux secret-tool can run. A successful check does not guarantee access to a stored secret.
Returns: boolean
Needs: subprocess
(source)