Attesto.Plug.ProtectResource (Attesto v2.28.0)

Copy Markdown View Source

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.

Summary

Types

An opaque, prepared protected-resource boundary.

Functions

Authenticates a request through a prepared ProtectResource boundary.

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

Initialize protection with optional authentication and scope plug adapters.

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

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

Types

protection()

@opaque protection()

An opaque, prepared protected-resource boundary.

Functions

authenticate(conn, protection)

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

Authenticates a request through a prepared ProtectResource boundary.

authorize(conn, protection, scopes)

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

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

init(opts, adapters)

Initialize protection with optional authentication and scope plug adapters.

prepare(opts)

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

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

prepare(opts, adapters)

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