Skip to main content

Module credentials

Module credentials 

Source
Expand description

Dynamic credentials for model providers.

A static API key is fine on a server, but an app running on a user’s device must not ship a provider key inside its binary. Providers therefore accept a CredentialSource, asked for a credential right before each request:

  • bring-your-own-key: the host app reads the user’s key from the platform keychain / keystore;
  • token broker: a backend issues short-lived, per-user tokens;
  • static keys for servers and tests (StaticCredential).

Sources are consulted per request. Wrap slow or rate-limited sources in CachedCredentials, which caches until shortly before expiry and shares one refresh between concurrent requests.

SourceUse
StaticCredentialServers, tests.
EnvCredentialsServers: read the key from an environment variable at request time.
FnCredentialsHosts: any async closure (keychain lookup, custom auth).
TokenBroker (feature http)Apps: short-lived tokens from your backend.

See docs/credentials.md for the threat model and the proxy pattern.

Structs§

CachedCredentials
Caches credentials per provider until shortly before they expire.
Credential
A credential to authenticate one request.
EnvCredentials
Reads keys from environment variables at request time (servers), so rotating the variable in a supervisor takes effect without rebuilding the registry.
FnCredentials
A CredentialSource backed by an async closure, e.g. a keychain lookup.
StaticCredential
A fixed secret.
TokenBroker
Fetches short-lived provider credentials from a broker endpoint.
TokenBrokerConfig
Token broker settings.

Enums§

BrokerAuth
How the app authenticates to the broker.

Constants§

DEFAULT_REFRESH_SKEW_MS
Credentials are refreshed this long before they expire (60 s).

Traits§

CredentialSource
Supplies credentials on demand.

Functions§

is_secure_endpoint
Whether url may receive credentials: https://, or http:// to a loopback / Android-emulator-host address (local development and tests only).
require_secure_endpoint
Validate an endpoint that will receive credentials.
unix_now_ms
Current Unix time in milliseconds. On wasm32-unknown-unknown, where there is no system clock, returns 0: credentials then never expire by time and are refreshed only through CredentialSource::invalidate.