A supervised cache of one issuer's discovery metadata and JWKS.
Without it, each AttestoClient.IDToken.verify/2 call that is not handed a
:jwks fetches the issuer's discovery document and key set again. An
Issuer process fetches them once, serves them from memory, and refreshes
them on a schedule and when a token names a kid it has not seen (RFC 7517
key rotation). AttestoClient.ResourceServer is built on the same process,
so access-token and ID Token verification share one cache and one refresh
logic.
Example
children = [
{AttestoClient.Issuer,
name: MyApp.Issuer,
issuer: "https://issuer.example",
fresh_ttl: :timer.minutes(5),
stale_ttl: :timer.hours(1)}
]
:ok = AttestoClient.Issuer.warm(MyApp.Issuer)
{:ok, claims} =
AttestoClient.IDToken.verify(id_token,
issuer: {AttestoClient.Issuer, MyApp.Issuer},
client_id: "my-client",
nonce: nonce
)issuer: {AttestoClient.Issuer, name} is accepted wherever those functions
take an :issuer string: AttestoClient.IDToken.verify/2,
AttestoClient.AuthorizationCode.start/2 and callback/3, and
AttestoClient.Token.refresh/4. The issuer identifier compared with the ID
Token's iss and the RFC 9207 response iss is the one this process was
started with. A process started by AttestoClient.ResourceServer can be
named the same way, so one process per issuer serves both kinds of token.
Options
:issuer- required. The HTTPS issuer identifier.:name- aGenServername for the process.:well_known- which discovery document to fetch; defaults to:openid_configuration.:metadata- discovery metadata supplied by the caller, used in place of a discovery fetch.:protocol-:oidcor:oauth. When set, every refresh also runsAttestoClient.Discovery.validate_metadata/3on the document for that protocol, so one thatAttestoClient.AuthorizationCode.start/2would refuse is a refresh failure and never becomes a snapshot. Set it on a cache used for sign-in. Unset (the default), the document is checked only for its issuer andjwks_uri, which is all access-token verification needs; a resource server's authorization server may publish a document with no authorization endpoint at all. The check does not apply with:jwks_uriand no:metadata, since the process then holds no document.:jwks_uri- fetch only the JWKS from this URI. With:jwks_uriand no:metadatathe process holds no discovery metadata, andmetadata/1returns{:error, :metadata_unavailable}.:accepted_algs- a fetched JWKS must hold at least one key usable with one of these algorithms. Defaults to every algorithm Attesto verifies.:fresh_ttl,:stale_ttl- milliseconds; default 5 minutes and 1 hour. The stale interval starts when the fresh interval ends.:unknown_kid_refresh_interval- milliseconds between refreshes caused by an unknownkid; default 30 seconds.:refresh_retry_interval- milliseconds before a failed refresh is retried; default 5 seconds.:refresh_timeout- milliseconds allowed for one metadata and JWKS fetch; default 15 seconds.:max_jwks_keys- default 32.:max_response_bytes- default 512 KiB.:req_options- options for the underlyingReqrequest.:outbound- deliberate exceptions to the HTTPS and public-address rules for this cache's metadata and JWKS fetches: an issuer at a private address (allowed_cidrs) or a loopbackhttpdevelopment issuer (allow_loopback_http). SeeAttestoClient.Outbound. A call that names this cache withissuer: {AttestoClient.Issuer, name}still passes its own:outboundfor the requests it makes itself, such as the token request.:resolver- a two-argument function used to resolve host names for this cache's metadata and JWKS fetches, asAttestoClient.Discoverytakes. Every address it returns is screened the same way. Defaults to the system resolver.
Cache behavior
- discovery metadata must report the configured issuer exactly, and metadata and JWKS are fetched only over HTTPS with redirects disabled;
- concurrent callers that need a refresh share one fetch;
- an unknown
kidcauses at most one refresh per cache generation and:unknown_kid_refresh_interval, so randomkidvalues cannot cause a fetch per request; - stale keys are used only within
:stale_ttland only after a transient refresh failure (transport error, timeout, 429, or 5xx); - every refresh failure starts a
:refresh_retry_intervalcooldown, so a persistent failure does not make every verification wait on the issuer. Before the first successful fetch, verification returns the remembered error until the cooldown ends, whilewarm/1andrefresh/1still fetch.
Signature and claim verification run in the calling process. The process owns only the cache and the refresh coordination.
Summary
Types
The value of an :issuer option that names an issuer cache.
Functions
Returns a specification to start this module under a supervisor.
Return the issuer identifier the cache was started with.
Return the issuer's verification keys, fetching them when the cache has none that are usable.
Return the issuer's discovery metadata, fetching it when the cache has none that is usable.
Return whether the cache holds a currently usable key snapshot.
Force a metadata/JWKS refresh.
Start a supervised issuer cache.
Warm the cache by completing metadata/JWKS retrieval.
Types
@type ref() :: {AttestoClient.Issuer, server()}
The value of an :issuer option that names an issuer cache.
@type refresh_error() :: {:jwks_refresh_failed, term()}
@type server() :: GenServer.server()
@type start_opt() :: {:name, GenServer.name()} | {:issuer, String.t()} | {:accepted_algs, [Attesto.SigningAlg.alg()]} | {:well_known, AttestoClient.Discovery.well_known()} | {:metadata, map()} | {:jwks_uri, String.t()} | {:req_options, keyword()} | {:fresh_ttl, non_neg_integer()} | {:stale_ttl, non_neg_integer()} | {:unknown_kid_refresh_interval, non_neg_integer()} | {:refresh_retry_interval, non_neg_integer()} | {:refresh_timeout, pos_integer()} | {:max_jwks_keys, pos_integer()} | {:max_response_bytes, pos_integer()} | {:outbound, AttestoClient.Outbound.t()} | {:resolver, (charlist(), :inet | :inet6 -> {:ok, [:inet.ip_address()]} | {:error, term()})}
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec issuer(server()) :: {:ok, String.t()} | {:error, refresh_error()}
Return the issuer identifier the cache was started with.
@spec keys(server()) :: {:ok, [map()]} | {:error, refresh_error()}
Return the issuer's verification keys, fetching them when the cache has none that are usable.
@spec metadata(server()) :: {:ok, map()} | {:error, :metadata_unavailable | refresh_error()}
Return the issuer's discovery metadata, fetching it when the cache has none that is usable.
Return whether the cache holds a currently usable key snapshot.
@spec refresh(server()) :: :ok | {:error, refresh_error()}
Force a metadata/JWKS refresh.
@spec start_link([start_opt()]) :: GenServer.on_start()
Start a supervised issuer cache.
@spec warm(server()) :: :ok | {:error, refresh_error()}
Warm the cache by completing metadata/JWKS retrieval.