pg_auth
alohadb · AlohaDB authentication, sessions and JWT API served by pg_restapi
Overview
PostgreSQL · 18
| Relationships | |
|---|---|
| Requires | pgcrypto pg_restapi plpgsql |
Usage
Sources:
pg_auth 1.3 is AlohaDB’s preview authentication extension for the pg_restapi /auth/v1 API. It stores users, identities, sessions, rotating refresh tokens, MFA and provider settings in the fixed auth schema. The reviewed distribution is AlohaDB 21.10.0, based on PostgreSQL 18; this is not a portability claim for stock PostgreSQL.
Enable Authentication
The SQL installation requires pgcrypto, PL/pgSQL and pg_restapi 1.3 or later. Install matching AlohaDB libraries, enable the REST listener, and configure a strong random pg_restapi.jwt_secret of at least 32 characters. Preloading pg_auth starts its optional mail worker; alternatively schedule auth.process_outbox(). Preload changes require a restart.
Run administrative SQL with the required privileged role. Installation creates missing anon, authenticated, service_role and auth_admin roles without login; service_role has BYPASSRLS. Existing superuser roles with these names block installation. Protect the service-role key on the server only. Put HTTPS in front of the listener before sending credentials.
API and SQL Objects
Use a compatible Supabase client for sign-up, password sign-in, refresh and logout. auth.uid(), auth.role(), auth.email() and auth.jwt() expose verified request claims to SQL policies. They do not authenticate an arbitrary SQL session by themselves. auth.configure manages settings, auth.set_provider configures OAuth providers, and auth.email_outbox holds mail awaiting delivery.
SMTP and OAuth code exchange require libcurl. WebAuthn MFA requires the matching OpenSSL build; SAML additionally requires libxml2. Unsupported builds return explicit errors. WebAuthn here is a second factor, not passkey sign-in. Web3 and recovery codes remain unsupported. Version 1.1 adds explicitly configured OpenID Connect ID-token sign-in as described below.
Privileges and Recovery
The installation revokes public access to authentication tables and internal functions, leaving only claim helpers public. HTTP authentication work runs as auth_admin; configuration and key-signing functions retain separate administrative restrictions. Secret masking in auth.settings() does not encrypt stored provider/TOTP/SMTP secrets. Protect database backups, redirects, mail/SMS endpoints and proxy-trust settings. Do not drop the extension to upgrade: it owns account and session data, while the cluster roles survive removal.
1.1 ID Tokens and Upgrade
The ID-token grant checks provider RS256/ES256 signing keys, issuer, audience, expiry and nonce. Configure auth.set_provider and auth.providers with jwks_url and permitted id_token_audiences; keep id_token_skip_nonce false unless the reviewed client flow requires otherwise. Provider discovery is not blanket trust in arbitrary token issuers. Fetching keys requires libcurl.
Keys are cached for one hour and refreshed for an unknown key ID, with a 30-second fetch throttle. Fetch failure retains cached keys; account for that in revocation/rotation procedures. These external tokens sign a user into a local session; local sessions and API keys still use HS256.
Install matching binaries and restart preloaded workers. Upgrade pg_restapi to 1.3 first, then run ALTER EXTENSION pg_auth UPDATE TO '1.3'. The upgrade preserves account data and changes the provider API; verify dependent callers and backup/recovery before enabling the new grant.
1.2–1.3 Security and Rate Limits
The 1.2 migration corrects phone pre-registration handling, limits wrong TOTP codes per factor, takes the rightmost forwarded address from a trusted proxy, and confines redirect globs to the configured host. Trust forwarded headers only behind a proxy that controls them. Version 1.3 shards rate counters by backend to avoid serializing requests during password hashing. Uncommitted hits from other processes are not yet visible, so concurrent bursts can exceed the nominal limit. Install the complete SQL update chain; retain account and session data.