AttestoPhoenix.ConsentGrant (AttestoPhoenix v5.0.0)

Copy Markdown View Source

The request binding a single-use consent grant is tied to, and the canonical hash over it (RFC 6749 §4.1.1).

A consent grant must approve exactly the authorization request the resource owner saw — the same client, redirect URI, scope and resource sets, requested claims, authorization details, DPoP key, PKCE challenge, and PKCE method — and nothing else. This module builds that binding from either raw authorization params (binding_from_params/2, used by the consent-screen mint action) or from a validated %Attesto.AuthorizationRequest{} (binding/2, used by the live /authorize consume side). Both builders feed the same canonical field list and therefore yield the same binding_hash/1 for the equivalent request.

Hosts that park requests should use AttestoPhoenix.Interaction.park/3 with subject: and AttestoPhoenix.Interaction.decide/4 to record consent on the parked entry, then read AttestoPhoenix.Interaction.consent_decision/3 on resume. That flow needs no separate consent grant or session token; see AttestoPhoenix.Interaction for the consent callback and host POST handler. Keep ConsentGrant for hosts that do not park, as described below.

The existing flow can still carry the token back as a consent_grant parameter alongside the original authorization parameters. Mint with binding_from_params(params, subject, scope: request.scope, resource: request.resource), then have :consent read conn.params["consent_grant"] and consume it against binding(request, subject.subject). This starts a fresh authorization request, including a fresh prompt=login check (OIDC Core §3.1.2.1, §3.1.2.3). Do not combine that parameter with resume.

Canonical binding

The binding contains (subject, client_id, redirect_uri, scope, resource, claims, dpop_jkt, code_challenge, code_challenge_method), followed by authorization_details when the request carries any.

  • subject - the OIDC sub of the resource owner who consented. Binding to the subject stops one user's consent token from approving another's request.
  • client_id / redirect_uri - the requesting client and the exact redirect URI the code will be returned to (RFC 6749 §3.1.2). Binding both stops a consent shown for one client/redirect from authorizing a different one.
  • scope - the requested scope set. Order is not significant (RFC 6749 §3.3), so the set is sorted before hashing: a request with scope=openid profile and one with scope=profile openid hash identically, while adding or dropping a scope changes the hash.
  • resource - the RFC 8707 resource set. Order and duplicates are insignificant, while changing the authorized resource changes the hash.
  • claims - the validated OIDC claims request. JSON objects are encoded recursively with sorted keys and explicit type/length framing.
  • dpop_jkt - the RFC 9449 authorization-code key binding, or nil.
  • code_challenge - the PKCE challenge (RFC 7636 §4.3), or the empty string when the request carries none. Binding it stops a consent from being replayed against a request that swapped in a different PKCE challenge.
  • code_challenge_method - the PKCE method (S256, RFC 7636 §4.3), or the empty string when the request carries no PKCE challenge. Binding it stops a consent granted for an S256 request from being reused for a plain request with the same challenge value.
  • authorization_details - the RFC 9396 authorization details array, in request order. Each object is encoded like claims. Binding it stops a consent shown for a request without, or with different, authorization details from approving one that carries an entitlement such as an OID4VCI openid_credential configuration. An absent or empty array adds nothing to the encoding, so the hash of a request without authorization details does not depend on this field.

binding_hash/1 is SHA-256 over a type-tagged, length-framed canonical encoding, URL-base64 encoded without padding. It is stable across the mint and consume sides because both derive it from this one function.

Summary

Types

The fields a consent grant is bound to. subject, client_id, and redirect_uri are required; scope is a (possibly empty) list whose order is normalized away; code_challenge and code_challenge_method are nil when the request carries no PKCE challenge.

Functions

Builds the consent binding for request consented to by subject.

Builds the consent binding from raw OAuth authorization params and subject.

The canonical binding hash for binding.

Safely builds a consent binding from untrusted raw parameters.

Types

binding()

@type binding() :: %{
  optional(:authorization_details) => [map()],
  subject: String.t(),
  client_id: String.t(),
  redirect_uri: String.t(),
  scope: [String.t()],
  resource: [String.t()],
  claims: map(),
  dpop_jkt: String.t() | nil,
  code_challenge: String.t() | nil,
  code_challenge_method: String.t() | nil
}

The fields a consent grant is bound to. subject, client_id, and redirect_uri are required; scope is a (possibly empty) list whose order is normalized away; code_challenge and code_challenge_method are nil when the request carries no PKCE challenge.

Functions

binding(request, subject)

Builds the consent binding for request consented to by subject.

request is the validated %Attesto.AuthorizationRequest{} (the front-channel request whose client_id, redirect_uri, scope, code_challenge, and code_challenge_method the user saw); subject is the authenticated resource owner's OIDC sub. The returned map feeds binding_hash/1 and the store's mint/2 / consume/2.

binding_from_params(params, subject, opts \\ [])

@spec binding_from_params(map(), String.t(), keyword()) :: binding()

Builds the consent binding from raw OAuth authorization params and subject.

Use this on the consent-screen mint side, where the host has the raw string-keyed params that reached /authorize / the consent action rather than the validated %Attesto.AuthorizationRequest{}. The consume side should keep using binding/2.

params is read by string keys. Scope and resource sets are normalized; claims may be the original JSON object string or an already-decoded map, and authorization_details the original JSON array string or an already-decoded list. Unknown params are ignored. Malformed values raise ArgumentError; callers handling untrusted data without an existing validation boundary should use safe_binding_from_params/3. For the equivalent validated request, binding_hash(binding_from_params(params, subject, scope: request.scope, resource: request.resource)) == binding_hash(binding(request, subject)).

The effective scope

binding/2 hashes the validated request's scope, which is the scope the scope policy decided: the client's request narrowed by the policy, or the policy's default when the request omitted scope (RFC 6749 §3.3), including a pushed request without one. The raw scope parameter can differ from it in both cases, so pass the scope the consent screen showed, which is request.scope in the :consent callback, as the :scope option. When :scope is given it replaces the scope parameter; it is a list of scope strings or a space-delimited string.

The bound resource

Likewise, binding/2 hashes the validated request's resource, which is the host's resource_indicators: [default_resources_for: ...] choice when the request named no resource. Pass request.resource as the :resource option; when given it replaces the resource parameter and is a list of resource strings or a single string.

binding_hash(binding)

@spec binding_hash(binding()) :: String.t()

The canonical binding hash for binding.

SHA-256 over a type-tagged, length-framed encoding, URL-base64 encoded without padding. Scope and resource are sorted unique sets; nested claims maps are ordered by key. Identical on the mint and consume sides for the same request.

safe_binding_from_params(params, subject, opts \\ [])

@spec safe_binding_from_params(map(), String.t(), keyword()) ::
  {:ok, binding()} | {:error, :invalid_binding}

Safely builds a consent binding from untrusted raw parameters.

Returns {:error, :invalid_binding} for an invalid required scalar, set, claims object, authorization details array, DPoP thumbprint value, or nested JSON value. Takes the same :scope and :resource options as binding_from_params/3.