AttestoPhoenix.ProtectedResource (AttestoPhoenix v5.0.0)

Copy Markdown View Source

Shared protected-resource authentication for Phoenix endpoints.

This module owns the transport check, access-token verification wiring, and revoked-token check used by protected-resource controllers. Endpoint actions receive the verified claims only after this common work succeeds. Every 401 completes the synchronous :auth_denied callback before sending the response; hosts must commit audit writes before returning (see AttestoPhoenix.EventSink).

DPoP target URI

A DPoP proof signs the URI of the request it was made for (htu, RFC 9449 §4.3), and the resource compares it with the URI it computes for the request. That URI is chosen in this order:

  1. a per-route override passed to AttestoPhoenix.Plug.Authenticate: core's :htu callback or core's :origin;
  2. the configuration's :htu callback;
  3. the configuration's :resource_origin followed by conn.request_path (AttestoPhoenix.Config.new/1 defaults :resource_origin to the origin of :audience when neither :htu nor :resource_origin is set and DPoP is enabled).

Requests never fall back to the Host header when DPoP is enabled. Pin an explicit resource origin when it differs from :audience:

config :my_app, AttestoPhoenix.Config,
  issuer: "https://auth.example.com",
  audience: "https://api.example.com",
  resource_origin: "https://api.example.com"

:resource_origin applies to every protected resource that uses this configuration, including the bundled UserInfo and credential endpoints. When those are served on another origin than the host's resources, set :htu (or a per-route override) instead.

Summary

Functions

Return whether the access token identified by the claims is revoked.

Translate the Phoenix configuration into the core Attesto configuration.

Run the protected-resource verification skeleton.

Build the transport options for a protected-resource error response.

Answer a request whose verified token is refused after verification, with the RFC 6750 §3.1 invalid_token challenge under the token's own scheme.

Return the challenge scheme for verified token claims.

Types

result()

@type result() :: {:ok, Plug.Conn.t(), map()} | {:halt, Plug.Conn.t()}

Functions

access_token_revoked?(config, arg2)

@spec access_token_revoked?(AttestoPhoenix.Config.t(), map()) :: boolean()

Return whether the access token identified by the claims is revoked.

attesto_config(config)

@spec attesto_config(AttestoPhoenix.Config.t()) :: Attesto.Config.t()

Translate the Phoenix configuration into the core Attesto configuration.

authenticate(conn, config, resource_metadata)

@spec authenticate(Plug.Conn.t(), AttestoPhoenix.Config.t(), String.t() | nil) ::
  result()

Run the protected-resource verification skeleton.

Returns {:ok, conn, claims} when endpoint-specific work may proceed, or {:halt, conn} when the request has already been answered.

authenticate_opts(config, resource_metadata)

@spec authenticate_opts(AttestoPhoenix.Config.t(), String.t() | nil) :: keyword()

Build the options consumed by Attesto.Plug.Authenticate.

error_opts(config, resource_metadata, extra)

@spec error_opts(AttestoPhoenix.Config.t(), String.t() | nil, keyword()) :: keyword()

Build the transport options for a protected-resource error response.

invalid_token(conn, config, resource_metadata, claims, error_overrides \\ [])

@spec invalid_token(
  Plug.Conn.t(),
  AttestoPhoenix.Config.t(),
  String.t() | nil,
  map(),
  keyword()
) ::
  Plug.Conn.t()

Answer a request whose verified token is refused after verification, with the RFC 6750 §3.1 invalid_token challenge under the token's own scheme.

Used for a revoked token, and by an endpoint whose host callback reports that the token's subject is no longer valid.

scheme_of(arg1)

@spec scheme_of(map()) :: :dpop | :bearer

Return the challenge scheme for verified token claims.