AttestoClient.IDToken (AttestoClient v2.26.0)

Copy Markdown View Source

Verify OpenID Connect ID Tokens issued by an authorization server.

This is the client-side mirror of Attesto.IDToken.mint/4. A relying party receives an ID Token from the server, fetches (or supplies) the server JWKS, and verifies the signed JWT plus the OIDC claims that bind it to the client and the authorization response.

Checks

  • signature verifies against the issuer JWKS, selected by kid, with Attesto's supported asymmetric algorithms. A missing kid is refused with :missing_kid when the issuer's complete key set has more than one entry, even if only one is eligible. require_kid: true requires it for a single key too; the default is false.
  • JOSE typ, when present, must be "JWT" or "application/jwt", case-insensitively; an access-token type such as "at+jwt" is rejected. require_typ: true also rejects an absent typ with :unexpected_typ.
  • iss equals :issuer, or one of :issuer_aliases when the caller opted into that deviation (see "Issuer aliases").
  • aud equals, or is an all-string array containing, :client_id. Any other entry must be listed in :trusted_audiences (see "Additional audiences").
  • azp equals :client_id whenever present, and is required when aud contains multiple audiences.
  • sub, exp, and iat are present and well-typed; exp must be in the future and iat / optional nbf must not be meaningfully in the future.
  • a supplied :nonce must match the token's nonce claim.
  • auth_time, when present, must not be meaningfully in the future; a supplied :max_age requires auth_time and enforces the age. require_auth_time: true requires the claim even without :max_age (OIDC Core §3.1.3.7 item 13), for a client that registered require_auth_time or otherwise depends on the claim. Under this option, a non-positive integer counts as missing (:auth_time_required), even with :max_age. This is a local policy, not an OIDC NumericDate rule.
  • a supplied :max_token_age_seconds rejects a token whose iat is older than that many seconds, plus :clock_skew_seconds (§3.1.3.7 item 10), with {:error, :token_too_old}. It is the option AttestoClient.ResourceServer takes, with the same rule.
  • supplied :access_token, :code, and :state are checked against at_hash, c_hash, and s_hash respectively using the ID Token signing algorithm and the verified issuer key's left-half hash construction.

:jwks may be supplied directly. Otherwise the verifier can fetch through AttestoClient.Discovery: pass :metadata, :jwks_uri, or just :issuer with optional :req_options / :well_known. Those fetches happen on every call. To cache the keys, start an AttestoClient.Issuer and pass issuer: {AttestoClient.Issuer, name}: the keys come from that process, which refreshes them once when a token names a kid it does not hold, and the expected iss is the issuer the process was started with.

Issuer aliases

OIDC Core §3.1.3.7 item 2 says the issuer identifier "MUST exactly match" the iss claim, and that is what this module does by default. Some providers break the rule and put more than one value in iss. Google issues ID Tokens with both https://accounts.google.com and accounts.google.com; the second is not a valid issuer identifier at all, since it has no https scheme. :issuer_aliases is an opt-in interop workaround for such a provider: a list of extra exact strings accepted as iss. It deviates from §3.1.3.7 and is empty by default. Discovery, the JWKS, and the RFC 9207 authorization-response iss check all stay pinned to :issuer; an alias only widens the comparison against the ID Token's iss claim.

Additional audiences

OIDC Core §3.1.3.7 item 3 says the ID Token MUST be rejected if it "contains additional audiences not trusted by the Client". :trusted_audiences is the list of audiences this client trusts besides itself; it is empty by default, so an aud array naming anything but :client_id fails with {:error, :untrusted_audience}. List an audience here only when the provider is known to issue ID Tokens for that party together with this client. The azp rules are unchanged: with more than one audience azp is required, and it must equal :client_id whenever present.

Fetching keys

Without :jwks (or an issuer cache) the issuer's metadata and JWKS are fetched over HTTPS from public addresses only. :outbound names a deliberate exception for an issuer at a private address or a loopback http development issuer; see AttestoClient.Outbound.

Clock tolerance

:clock_skew_seconds bounds how far in the future iat, nbf, and auth_time may lie. It defaults to 60. :expiry_leeway_seconds is how long past exp a token is still accepted (RFC 7519 §4.1.4); it defaults to 0, so an expired token is rejected at exp. Each is an integer from 0 to 300. AttestoClient.ResourceServer takes the same two options with the same meanings.

:now accepts Unix seconds, a zero-arity function returning integer Unix seconds, or a DateTime (retained for compatibility). The live clock or function is read after key resolution and signature verification, immediately before the time checks. Invalid options or function results return {:error, :invalid_time}. :require_typ must be a boolean; other values return {:error, :invalid_require_typ}. It defaults to false because OIDC does not require typ (RFC 7519 §5.1). Requiring generic JWT typing does not distinguish all JWT purposes (RFC 8725 §3.11).

Unsigned ID Tokens (alg: "none")

By default an unsigned ID Token is rejected. OIDC Core §3.1.3.7 permits a client to skip signature validation only when the ID Token was received directly from the token endpoint over TLS and the client registered id_token_signed_response_alg none. Pass allow_unsigned: true to opt into that case: a token whose JOSE header alg is exactly "none" (with an empty signature part) is then accepted without signature verification, while all claim checks still run. The option has no effect on signed tokens, and never applies to artifacts received through the front channel.

Summary

Functions

Verify id_token, returning {:ok, claims} or {:error, reason}.

Types

error()

@type error() ::
  :missing_issuer
  | :missing_client_id
  | :invalid_jwks
  | :invalid_metadata
  | :issuer_mismatch
  | :unsupported_alg
  | :invalid_token
  | :invalid_signature
  | :ambiguous_key
  | :missing_kid
  | :invalid_require_kid
  | :weak_key
  | :unsupported_critical_header
  | :unexpected_typ
  | :invalid_issuer
  | :invalid_issuer_aliases
  | :invalid_clock_skew_seconds
  | :invalid_expiry_leeway_seconds
  | :invalid_auth_time_allowance_seconds
  | :invalid_max_token_age_seconds
  | :invalid_require_auth_time
  | :invalid_require_typ
  | :invalid_time
  | :token_too_old
  | :invalid_audience
  | :invalid_trusted_audiences
  | :untrusted_audience
  | :missing_azp
  | :invalid_azp
  | :invalid_claims
  | :subject_mismatch
  | :missing_exp
  | :expired
  | :invalid_iat
  | :not_yet_valid
  | :invalid_nbf
  | :nonce_required
  | :nonce_mismatch
  | :invalid_max_age
  | :auth_time_required
  | :invalid_auth_time
  | :max_age_exceeded
  | :missing_at_hash
  | :invalid_at_hash
  | :missing_c_hash
  | :invalid_c_hash
  | :missing_s_hash
  | :invalid_s_hash
  | AttestoClient.Issuer.refresh_error()
  | AttestoClient.Discovery.error()

jwks()

@type jwks() :: %{optional(String.t()) => term()} | [map()] | map()

verify_opt()

@type verify_opt() ::
  {:issuer, String.t() | AttestoClient.Issuer.ref()}
  | {:client_id, String.t()}
  | {:jwks, jwks()}
  | {:metadata, map()}
  | {:jwks_uri, String.t()}
  | {:nonce, String.t()}
  | {:subject, String.t()}
  | {:max_age, non_neg_integer()}
  | {:require_auth_time, boolean()}
  | {:require_typ, boolean()}
  | {:require_kid, boolean()}
  | {:max_token_age_seconds, non_neg_integer()}
  | {:issuer_aliases, [String.t()]}
  | {:trusted_audiences, [String.t()]}
  | {:outbound, AttestoClient.Outbound.t()}
  | {:clock_skew_seconds, 0..300}
  | {:expiry_leeway_seconds, 0..300}
  | {:auth_time_allowance_seconds, 0..300}
  | {:access_token, String.t()}
  | {:require_at_hash, boolean()}
  | {:code, String.t()}
  | {:require_c_hash, boolean()}
  | {:state, String.t()}
  | {:accepted_algs, [Attesto.SigningAlg.alg()]}
  | {:enforce_fapi_alg_policy, boolean()}
  | {:allow_unsigned, boolean()}
  | {:now, integer() | DateTime.t() | (-> integer())}
  | {:req_options, keyword()}
  | {:well_known, AttestoClient.Discovery.well_known()}

Functions

verify(id_token, opts)

@spec verify(String.t(), [verify_opt()]) :: {:ok, map()} | {:error, error()}

Verify id_token, returning {:ok, claims} or {:error, reason}.

Required options:

  • :issuer - expected OpenID Provider issuer, or {AttestoClient.Issuer, name} to take the issuer and its keys from a running cache.
  • :client_id - this relying party's client id.

Pass :subject when a later ID Token (for example, from refresh) must remain bound to the subject of an earlier verified ID Token.

JWKS options:

  • :jwks - a JWKS map/list supplied by the caller.
  • :metadata - discovery metadata containing matching issuer and jwks_uri.
  • :jwks_uri - fetch only the JWKS.
  • no JWKS option - fetch discovery from :issuer, then fetch its JWKS; with an AttestoClient.Issuer, use its cached keys.