OAuth2 client credentials for CipherOwl, with a per-run token cache.
POST https://svc.cipherowl.ai/oauth/token takes a JSON body
{client_id, client_secret, audience: "svc.cipherowl.ai", grant_type: "client_credentials"} and returns {access_token, expires_in, token_type: "Bearer"} (24 hours by default). There is no refresh
token: a new token is minted the same way.
Masking. A minted token is marked as a secret as soon as it comes back, so it is masked in call records.
Token cache. A minted token is stored in the run's execution
context as a secret (so it is masked in run logs) and reused until 60
seconds before it expires. The cache lives for one run. When the API
answers 401, ::cipherowl::api/request evicts the token, mints a new
one and retries the request once.
Values
CACHE_PREFIX
CACHE_PREFIX: Str "cipherowl.token-cache."
EXPIRY_MARGIN_SECONDS
EXPIRY_MARGIN_SECONDS: Int 60
FALLBACK_EXPIRES_IN_SECONDS
FALLBACK_EXPIRES_IN_SECONDS: Int 3600
Functions
access-token
fn (opts: Map): Str
An access token for opts or the context credentials (cached for the
run). Use it for calls this package does not wrap; prefer
::cipherowl::api/request, which also re-mints on 401.
Example
token ::cipherowl::auth/access-token({})
bearer
fn (creds: Map): Map
A bearer token for resolved credentials (see
::cipherowl::api/resolve): the access-token itself when one was
given (never cached), otherwise a cached or freshly minted token.
Returns {token, cache-key, cached} (cached is true when the token
came from the cache; cache-key is null for a given access token),
or an Err.
Example
auth ::cipherowl::auth/bearer(::cipherowl::api/resolve({}))
auth.token
auth.cached
cache-key
fn (creds: Map): Str
The context key a token is cached under: a SHA-256 of the credential identity (client id, a hash of the secret, audience and base URL), never the raw credential. Exposed for testing.
Example
cache ::cipherowl::auth/cache-key(::cipherowl::api/resolve({}))
::hot::ctx/get(`${cache}.expires-at`)
evict
fn (key: Str?): Bool
Drop a cached token (after a 401). No-op for a null key (a caller-supplied access token).
Example
cache ::cipherowl::auth/cache-key(::cipherowl::api/resolve({}))
::cipherowl::auth/evict(cache)
expires-at
fn (tokens: Map, now-ms: Int): Int
Epoch milliseconds at which a token minted at now-ms should be treated
as expired: expires_in minus EXPIRY_MARGIN_SECONDS. Exposed for
testing.
Example
::cipherowl::auth/expires-at({expires_in: 3600}, 1000) // 3541000
is-fresh
fn (token: Any, expiry: Any, now-ms: Int): Bool
True when a cached token exists and now-ms is before its
margin-adjusted expiry. Exposed for testing.
Example
::cipherowl::auth/is-fresh("cached-token", 2000, 1000) // true
::cipherowl::auth/is-fresh("cached-token", 2000, 3000) // false
::cipherowl::auth/is-fresh(null, 2000, 1000) // false
mint
fn (client-id: Str, client-secret: Str, base-url: Str): Map
Mint a new access token (no cache). Returns the token response with the token masked in call records, or an Err.
Example
tokens ::cipherowl::auth/mint(::hot::ctx/get("cipherowl.client.id"), ::hot::ctx/get("cipherowl.client.secret"), ::cipherowl/BASE_URL)
tokens.access_token
tokens.expires_in
token-error
fn (status: Int, body: Any): Str
The Err message for a failed token request: the HTTP status and the
OAuth error code when the body has one. Never includes credentials or
the response body. Exposed for testing.
Example
::cipherowl::auth/token-error(401, {error: "invalid_client"}) // "cipherowl: token request failed with HTTP 401 (invalid_client); check cipherowl.client.id and cipherowl.client.secret"
::cipherowl::auth/token-error(500, "oops") // "cipherowl: token request failed with HTTP 500"
token-request
fn (client-id: Str, client-secret: Str, base-url: Str): ::hot::http/HttpRequest
The HttpRequest that mints a token at {base-url}/oauth/token.
Exposed for testing.
Example
request ::cipherowl::auth/token-request(::hot::ctx/get("cipherowl.client.id"), ::hot::ctx/get("cipherowl.client.secret"), ::cipherowl/BASE_URL)
request.url
token-request-body
fn (client-id: Str, client-secret: Str): Map
The JSON body of the token request. Exposed for testing.
Example
body ::cipherowl::auth/token-request-body(::hot::ctx/get("cipherowl.client.id"), ::hot::ctx/get("cipherowl.client.secret"))
body.grant_type