# `Attesto.Plug.ProtectResource`
[🔗](https://github.com/XukuLLC/attesto/blob/v2.28.0/lib/attesto/plug/protect_resource.ex#L2)

Authenticate and authorize one OAuth protected resource in order.

    plug Attesto.Plug.ProtectResource,
      config: {MyHost.Auth, :config},
      resource_path: "/documents",
      base_url: "https://api.example.com",
      resource_audience: :resource,
      scopes: ["documents.read"],
      principal: {MyHost.Auth, :load_principal}

All `Attesto.Plug.ResourceAuthenticate` options are accepted. `:scope` or
`:scopes` supplies the required concrete scopes; `:scope_hierarchy` supplies
the host's broader-to-narrower scope map. Transport hooks and assign keys
are shared between authentication and authorization.

Use `prepare/1`, `authenticate/2` and `authorize/3` when a bounded,
side-effect-free classification determines operation scopes after verifying
the token. An empty dynamic list still requires authentication by the same
prepared boundary. A prepared split boundary cannot be used with `call/2`.

`init/2` and `prepare/2` additionally accept `:authenticate` and
`:require_scopes` Plug modules for protocol adapters. They must preserve the
core option and verified-assignment contracts. Normal hosts use the defaults.
Initialization produces no closures and works in compile-time pipelines.

# `protection`

```elixir
@opaque protection()
```

An opaque, prepared protected-resource boundary.

# `authenticate`

```elixir
@spec authenticate(Plug.Conn.t(), protection() | map()) :: Plug.Conn.t()
```

Authenticates a request through a prepared ProtectResource boundary.

# `authorize`

```elixir
@spec authorize(Plug.Conn.t(), protection(), [String.t()]) :: Plug.Conn.t()
```

Enforces dynamically selected scopes after `authenticate/2` and before dispatch.

# `init`

Initialize protection with optional authentication and scope plug adapters.

# `prepare`

```elixir
@spec prepare(keyword()) :: protection()
```

Prepares an authentication-first boundary whose scopes are supplied to `authorize/3`.

# `prepare`

Prepare a split boundary with optional authentication and scope plug adapters.

---

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