Karta.Credentials.KeyCache (Karta v1.0.0)

API keys verified recently, so that a request does not cost an Argon2id verification (Karta.Credentials.authenticate_api_key/1) every time.

The table maps the SHA-256 hash of a key to the id of its credential and the time it was verified; the key itself is never kept. An entry is used for ttl milliseconds (default 10 minutes). It only says "this key's secret was right": the credential is still read from the database on every request (authenticate/2), so a revoked credential or a suspended server takes effect at once, on every node. At most max_size entries are kept (default 10,000); when the table is full it is emptied.

While the cache's process is restarting its table does not exist for a moment: a lookup is then a miss and the key is verified again, never a crashed request.

Summary

Functions

The active API key credential (with its server) for token, when the key was verified within the time to live: {:ok, credential}. :error when the credential is gone or revoked (its entry is dropped); :miss when the key has to be verified. Options: name, now (ms, for tests).

Returns a specification to start this module under a supervisor.

Empties the cache.

Records that token was verified for credential. Options: name, now.

Options: name (also the ETS table; default this module), ttl (ms), max_size.

Functions

authenticate(token, opts \\ [])

@spec authenticate(String.t(), keyword()) ::
  {:ok, Karta.Credentials.Credential.t()} | :error | :miss

The active API key credential (with its server) for token, when the key was verified within the time to live: {:ok, credential}. :error when the credential is gone or revoked (its entry is dropped); :miss when the key has to be verified. Options: name, now (ms, for tests).

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clear(name \\ __MODULE__)

@spec clear(atom()) :: :ok

Empties the cache.

put(token, credential, opts \\ [])

@spec put(String.t(), Karta.Credentials.Credential.t(), keyword()) :: :ok

Records that token was verified for credential. Options: name, now.

start_link(opts)

Options: name (also the ETS table; default this module), ttl (ms), max_size.