Behavior for pluggable authorization in PgRest.
Implement this behavior to add runtime permission checks to your PgRest resources.
Usage
defmodule MyApp.PgRestAuth do
@behaviour PgRest.Authorization
@impl true
def authorize(_conn, _resource_module, :read, _context), do: :ok
def authorize(_conn, _resource_module, _op, %{role: :admin}), do: :ok
def authorize(_conn, _resource_module, _op, _context), do: {:error, "Forbidden"}
endThen configure in your router:
forward "/api", PgRest.Plug,
repo: MyApp.Repo,
authorization: MyApp.PgRestAuthauthorize_query/5 receives the base scope after authorize/4 permits the operation.
It returns {:ok, query} or {:ok, query, opts}. PgRest intersects the query with the
base scope. columns: names physical columns; limit: caps rows per parent.
aggregates: and count: default to false; columns default to :all, limit to nil.
A query-only module can grant table operations; functions still require authorize/4.
Function targets are %PgRest.Function{}: match schema/name (and params for
overloads), not module, which is nil for introspected functions.
context.graph and context.plan_enabled are unstable planner data; callbacks
should use their own application keys and the documented identity/path fields.
Both callbacks receive context.path (embed output names) and context.root?;
a junction appends its table name to the embed path. When a read authorizes an embed,
context.relationship is the PgRest.SchemaGraph.Relationship the embed resolved to
(source, target, assoc, columns) and context.embed the name the request gave the embed
(a table, constraint or column name as PostgREST resolves them); both are nil for the root
and a junction. A policy can authorize the embed by the relationship rather than by the
resource it targets; the embed name is request input, so check it against the relationship's
columns before trusting it.
path, root?, relationship and embed are reserved context keys: PgRest writes them on
every node a read authorizes, over any value a context_builder put under the same key
(relationship and embed are nil for the root and a junction). Keep host data under other
keys.
Differences from PostgREST
Query policies extend database privileges/RLS: stars expose permitted columns only,
and writes use distinct read and payload policies. Filters, counts, representations
and named conflict targets or Location require a read grant as well as the write grant.
GET /projects?select=* under columns: ["id"] returns [{"id":1}];
PostgREST returns every granted column.
RPC result_scope: :none trusts the function for rows only: query-hook denials,
column grants, count/aggregate grants and permission limits still apply without
keyed row matching. PostgREST relies on function/database privileges and RLS.
A hook's bare {:ok, query} denies EXPLAIN; an explicit three-tuple grants it.
EXPLAIN is an admission check on the actual authorized statement, not a second
narrowing pass. PostgREST uses privileges and db-plan-enabled instead.
Summary
Types
:execute is asked for a PgRest.Function before an RPC call is planned; :explain for a
query plan response. The others are asked for resources.
Functions
Whether the resource's allow: list (from its __pgrest_config__/0) admits operation.
:explain is always admitted: it is decided by the hook alone.
The single authorization gate: :ok, or the 42501 PgRest answers with when the
resource's allow: list excludes operation (a missing grant) or auth (when not nil)
denies. allow: is checked first, so the hook never sees an operation the resource does not
expose. A %PgRest.Function{} has no allow: list; only the hook decides, and a denial names
the function.
Whether :explain is allowed (check/5) on every one of targets, resource modules and the
PgRest.Function of a call: the gate of the application/vnd.pgrst.plan media type.
Whether a configured policy exports the query-narrowing callback.
Types
@type operation() :: :read | :create | :update | :delete | :execute | :explain
:execute is asked for a PgRest.Function before an RPC call is planned; :explain for a
query plan response. The others are asked for resources.
@type query_operation() :: :read | :create | :update | :delete | :explain
@type query_options() :: [ columns: :all | [String.t()], limit: pos_integer() | nil, aggregates: boolean(), count: boolean() ]
Callbacks
@callback authorize( conn :: Plug.Conn.t(), resource_module :: module() | PgRest.Function.t(), operation :: operation(), context :: map() ) :: :ok | {:error, String.t() | map()}
@callback authorize_query( Plug.Conn.t(), module(), query_operation(), Ecto.Query.t(), map() ) :: {:ok, Ecto.Query.t()} | {:ok, Ecto.Query.t(), query_options()} | {:error, term()}
Functions
Whether the resource's allow: list (from its __pgrest_config__/0) admits operation.
:explain is always admitted: it is decided by the hook alone.
@spec check( module() | nil, Plug.Conn.t(), module() | PgRest.Function.t(), operation(), map(), PgRest.Authorization.State.t() ) :: :ok | {:error, PgRest.Error.t()}
The single authorization gate: :ok, or the 42501 PgRest answers with when the
resource's allow: list excludes operation (a missing grant) or auth (when not nil)
denies. allow: is checked first, so the hook never sees an operation the resource does not
expose. A %PgRest.Function{} has no allow: list; only the hook decides, and a denial names
the function.
It runs for every resource a request touches: the root and each embed, spread and
many-to-many junction (:read), the write's own operations, :read on a write's target
when its filters, count, representation or Location read rows, :execute on an RPC's
function and :read on the resources its result and embeds read.
Missing callbacks deny. A query-only policy delegates table operations to
PgRest.Authorization.Apply; functions require authorize/4. A query hook's
:explain grant must be a three-tuple and receives the effective source query.
Write row checks use the write-operation query; read policies protect targeting and output.
@spec explain?( module() | nil, Plug.Conn.t(), [module() | PgRest.Function.t()], map(), PgRest.Authorization.State.t() ) :: boolean()
Whether :explain is allowed (check/5) on every one of targets, resource modules and the
PgRest.Function of a call: the gate of the application/vnd.pgrst.plan media type.
Whether a configured policy exports the query-narrowing callback.