Skip to content

keyring ​

Store and retrieve secrets in the operating system's keyring, so API keys and tokens can be stored without plaintext credential files.

ts
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 ​

ts
@always(service, key)
effect std::setSecret {
  key: string;
  service: string
}

(source)

std::getSecret ​

ts
@always(service, key)
effect std::getSecret {
  key: string;
  service: string
}

(source)

std::deleteSecret ​

ts
@always(service, key)
effect std::deleteSecret {
  key: string;
  service: string
}

(source)

Functions ​

setSecret ​

ts
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:

NameTypeDefaultDescription
keystringA non-empty name identifying the secret within its service.
valuestringThe non-empty secret to store.
servicestring"agency-lang"The namespace shared by calls that store and retrieve this secret.

Returns: Result

Throws: std::setSecret

Needs: subprocess

(source)

getSecret ​

ts
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:

NameTypeDefaultDescription
keystringThe name used when storing the secret.
servicestring"agency-lang"The namespace used when storing the secret.

Returns: Result

Throws: std::getSecret

Needs: subprocess

(source)

deleteSecret ​

ts
deleteSecret(
  key: string,
  service: string = "agency-lang",
): Result raises <std::deleteSecret>

Delete a secret from the system keyring, returning whether it existed.

Parameters:

NameTypeDefaultDescription
keystringThe name used when storing the secret.
servicestring"agency-lang"The namespace used when storing the secret.

Returns: Result

Throws: std::deleteSecret

Needs: subprocess

(source)

isKeyringAvailable ​

ts
isKeyringAvailable(): boolean

Check 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)