AttestoClient.Issuer (AttestoClient v2.26.0)

Copy Markdown View Source

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 - a GenServer name 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 - :oidc or :oauth. When set, every refresh also runs AttestoClient.Discovery.validate_metadata/3 on the document for that protocol, so one that AttestoClient.AuthorizationCode.start/2 would 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 and jwks_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_uri and no :metadata, since the process then holds no document.
  • :jwks_uri - fetch only the JWKS from this URI. With :jwks_uri and no :metadata the process holds no discovery metadata, and metadata/1 returns {: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 unknown kid; 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 underlying Req request.
  • :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 loopback http development issuer (allow_loopback_http). See AttestoClient.Outbound. A call that names this cache with issuer: {AttestoClient.Issuer, name} still passes its own :outbound for 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, as AttestoClient.Discovery takes. 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 kid causes at most one refresh per cache generation and :unknown_kid_refresh_interval, so random kid values cannot cause a fetch per request;
  • stale keys are used only within :stale_ttl and only after a transient refresh failure (transport error, timeout, 429, or 5xx);
  • every refresh failure starts a :refresh_retry_interval cooldown, 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, while warm/1 and refresh/1 still 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

ref()

@type ref() :: {AttestoClient.Issuer, server()}

The value of an :issuer option that names an issuer cache.

refresh_error()

@type refresh_error() :: {:jwks_refresh_failed, term()}

server()

@type server() :: GenServer.server()

start_opt()

@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

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

issuer(server)

@spec issuer(server()) :: {:ok, String.t()} | {:error, refresh_error()}

Return the issuer identifier the cache was started with.

keys(server)

@spec keys(server()) :: {:ok, [map()]} | {:error, refresh_error()}

Return the issuer's verification keys, fetching them when the cache has none that are usable.

metadata(server)

@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.

ready?(server)

@spec ready?(server()) :: boolean()

Return whether the cache holds a currently usable key snapshot.

refresh(server)

@spec refresh(server()) :: :ok | {:error, refresh_error()}

Force a metadata/JWKS refresh.

start_link(opts)

@spec start_link([start_opt()]) :: GenServer.on_start()

Start a supervised issuer cache.

warm(server)

@spec warm(server()) :: :ok | {:error, refresh_error()}

Warm the cache by completing metadata/JWKS retrieval.