# `AttestoPhoenix.ProtectedResource`
[🔗](https://github.com/XukuLLC/attesto_phoenix/blob/v5.0.0/lib/attesto_phoenix/protected_resource.ex#L1)

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.

# `result`

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

# `access_token_revoked?`

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

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

# `attesto_config`

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

Translate the Phoenix configuration into the core Attesto configuration.

# `authenticate`

```elixir
@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`

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

Build the options consumed by `Attesto.Plug.Authenticate`.

# `error_opts`

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

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

# `invalid_token`

```elixir
@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`

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

Return the challenge scheme for verified token claims.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
