Who a request runs as: RLS mode's bearer token, role allowlist and request GUCs
(PgRest.Plug's rls: option), or, without it, the anonymous? predicate.
RLS mode
rls: makes every request run as a database role, as PostgREST does:
forward "/api", PgRest.Plug,
repo: MyApp.AuthenticatorRepo,
rls: [
roles: ["anon", "authenticated", "service_role"],
jwt: {PgRest.JWT.HS256, secret: {:system, "JWT_SECRET"}},
pre_request: "public.check_request"
]- No
Authorization: Bearerheader: thedefault_role("anon";nilmakes the request a 401PGRST302). A token is verified byjwt:({PgRest.JWT.HS256, secret: ...}, or:contextto take the claims a:context_builderverified fromcontext[:jwt_claims]). - The role is the claim at
role_claim("role", or a key path such as["app_metadata", "role"]), elsedefault_role. A role outside theroles:allowlist is a 40122023role "<role>" does not exist, before any SQL runs. - The request transaction's setup statement sets the role's
ALTER ROLE ... SETsettings (database-wide, of the roles the login is a member of, as PostgREST reads them), thenrole,request.jwt.claims(the claims withroleinserted),request.method,request.path,request.headers(lowercase names, noCookie; a repeated header keeps its last value where the adapter keeps duplicates, as Plug.Test and Bandit do, while Plug.Cowboy joins them with", ") andrequest.cookies; reads run in a read-only transaction.pre_request: "schema.fn"is then called in the same transaction.
A request is authenticated when its role is not default_role (PostgREST's
authRole /= anonRole): a 42501 is then a 403 instead of a 401.
ALTER ROLE ... SET changes need a restart or PgRest.Catalog.forget/1 for the plug's repo.
The repo must log in as a role that is neither superuser nor BYPASSRLS (a NOINHERIT
authenticator granted the API roles): otherwise every request raises ArgumentError, unless
allow_privileged_login: true, which logs a warning once per repo instead.
Differences from PostgREST
- Roles are an allowlist: a token naming
none,postgresor any role outsideroles:is a 40122023. PostgREST switches to any role the authenticator may assume (role: "none"runs as the authenticator;role: "postgres"is a 40342501permission denied to set role). request.pathis relative to the plug's mount (forward "/api"sees/rls_notes); PostgREST's is the full request path.pre_requestruns after PgRest has planned the request, so an error found while planning (an unknown column) answers before the function's own. PostgREST calls it before the database sees the query:select=nopewith a pre-request that raises gets itsP0001.- A superuser or
BYPASSRLSlogin raises (see above); PostgREST serves it. - A role's
default_transaction_isolationsetting is not applied; PostgREST opens that role's transactions at that isolation level. PgRest's run at the server default.
Summary
Functions
An anonymous resolution without RLS mode: no claims, no role.
Raises when RLS mode's repo logs in as a superuser or BYPASSRLS role (from
PgRest.Catalog.facts/1), unless allow_privileged_login: true, which logs the same text as
a warning once per repo.
The default anonymous? predicate of non-RLS mode: whether context[:role] (the default
context reads it from conn.assigns) is nil, :anon or "anon".
Validates the rls: plug option and fills its defaults (role_claim: "role",
default_role: "anon", pre_request: nil, allow_privileged_login: false). nil (no RLS
mode) stays nil. Raises ArgumentError.
Resolves who conn runs as. In RLS mode (rls from init/1), from the bearer token (see the
moduledoc); otherwise claims are empty, db_role is nil and the request is authenticated
unless anonymous?.(conn, context).
The role PgRest.Error constructors and PgRest.Error.Translate take, from anything that
carries authenticated? (a resolution, a request, a context or an executor config).
What the request transaction needs from an RLS-mode request: the role's ALTER ROLE settings
(PgRest.Catalog.Facts, set before the role, as PostgREST does), the setup statement's GUCs
in PostgREST's order, whether a read is read-only, and the pre_request call. Without RLS
mode only authenticated? is set (for error statuses).
Types
Functions
@spec anonymous() :: t()
An anonymous resolution without RLS mode: no claims, no role.
@spec check_login!(module(), PgRest.Catalog.Facts.t(), rls()) :: :ok
Raises when RLS mode's repo logs in as a superuser or BYPASSRLS role (from
PgRest.Catalog.facts/1), unless allow_privileged_login: true, which logs the same text as
a warning once per repo.
@spec default_anonymous?(Plug.Conn.t(), map()) :: boolean()
The default anonymous? predicate of non-RLS mode: whether context[:role] (the default
context reads it from conn.assigns) is nil, :anon or "anon".
Validates the rls: plug option and fills its defaults (role_claim: "role",
default_role: "anon", pre_request: nil, allow_privileged_login: false). nil (no RLS
mode) stays nil. Raises ArgumentError.
@spec resolve(Plug.Conn.t(), rls() | nil, map(), (Plug.Conn.t(), map() -> boolean())) :: {:ok, t()} | {:error, PgRest.Error.t()}
Resolves who conn runs as. In RLS mode (rls from init/1), from the bearer token (see the
moduledoc); otherwise claims are empty, db_role is nil and the request is authenticated
unless anonymous?.(conn, context).
@spec role(map() | nil) :: :anon | :authenticated
The role PgRest.Error constructors and PgRest.Error.Translate take, from anything that
carries authenticated? (a resolution, a request, a context or an executor config).
@spec transaction(Plug.Conn.t(), t(), rls() | nil, PgRest.Catalog.Facts.t()) :: map()
What the request transaction needs from an RLS-mode request: the role's ALTER ROLE settings
(PgRest.Catalog.Facts, set before the role, as PostgREST does), the setup statement's GUCs
in PostgREST's order, whether a read is read-only, and the pre_request call. Without RLS
mode only authenticated? is set (for error statuses).