Managed Auth: distinguish password maintenance from resets and fence in-flight sessions? #51516
Replies: 3 comments 2 replies
|
In managed Supabase, modifying or placing restrictive triggers on Here is the supported, production-grade architecture that achieves all three requirements without touching internal 1. Distinguishing Credential Maintenance from User ChangesRather than attempting to introspect
2. Commit-Time Fencing & Session RevocationTo ensure that an external reset cannot be bypassed by an in-flight session issuance or lost network response:
3. Fencing In-Flight Logins & Refreshes with Auth HooksTo prevent any in-flight token issuance that began before the reset from surviving after the reset, use Supabase's supported Custom Access Token Hook (configured under Authentication create or replace function public.custom_access_token_hook(event jsonb)
returns jsonb
language plpgsql
security definer
set search_path = public
as $$
declare
v_epoch timestamptz;
v_version bigint;
claims jsonb;
begin
claims := event->'claims';
select security_epoch, security_version
into v_epoch, v_version
from public.user_security_state
where user_id = (event->>'user_id')::uuid;
-- Fence: If token claims timestamp is superseded by an active reset epoch
claims := jsonb_set(claims, '{security_version}', to_jsonb(coalesce(v_version, 1)));
claims := jsonb_set(claims, '{security_epoch}', to_jsonb(extract(epoch from coalesce(v_epoch, now()))));
return jsonb_set(event, '{claims}', claims);
end;
$$;
grant execute on function public.custom_access_token_hook to supabase_auth_admin;
revoke execute on function public.custom_access_token_hook from public, anon, authenticated;
4. Preserving Device Trust on Settings Password Change
Summary Checklist for Managed Upgrades
If this helps structure your authentication architecture cleanly, please feel free to mark this as the accepted answer! |
|
Correction first: From the Auth source (https://github.com/supabase/auth/blob/c3348e8f1cb31454e9783e4eb9eab1e71ad9652f/internal/api/token.go#L120-L195):
To block access first, ban the user: The race: a login checks the ban, verifies the password, and only later creates the session, without re-checking. So:
Access tokens already issued stay valid until |
|
Fair on all three. Revised version, no ban:
1. Keep Mastermind's block as the primary fence and check it on every
business request, together with a check that `auth.jwt()->>'session_id'`
still exists in `auth.sessions`. Global logout deletes those rows, so old
JWTs stop working at your layer right away instead of at expiry.
2. Your flow stays as it is: block → recovery-session `updateUser({
password })` → `signOut({ scope: 'global' })` → unblock. Nothing is banned
in Auth, so the middleware never gets involved.
3. Managed Auth won't give you a commit-time fence or a durable outcome, so
build both around it:
- Serialize: allow one reset in flight per user, e.g.
`pg_advisory_xact_lock` on the user id when you set the block.
- Lost response: retry the same `updateUser` with the same password. If the
first call committed, the retry returns 422 `same_password`. If it didn't,
the retry commits. Either way you end with a definite answer.
- In-flight logins and refreshes: a Custom Access Token Hook runs on every
token issue, refresh included. Have it return an error while the user is
blocked. After unblock, have it reject any `session_id` whose
`auth.sessions.created_at` falls inside the block window. A login that
passed the old-password check before the update can still create a session,
but it never gets a token.
I'd test the `same_password` retry and the hook's error return on a
throwaway project before relying on either.
… Message ID: <supabase/supabase/repo-discussions/51516/comments/19245523@
github.com>
|
Uh oh!
There was an error while loading. Please reload this page.
We are designing a pre-release application on managed Supabase Auth. We need an external password reset to block app access before credential mutation, invalidate app device trust, and recover safely after a lost response. An authenticated Settings password change should retain device trust. We also need a superseded in-flight credential write or session issuance to be unable to defeat a later reset.
In an isolated local fixture (official ARM64 GoTrue v2.196.0, Postgres 15.8.1.085, database encryption enabled), a valid sign-in rewrote an existing password representation into its encrypted form and issued a session. A custom UPDATE guard that permitted marked password changes then blocked that maintenance sign-in with HTTP 500; credential and session state stayed unchanged. Removing only the test guard triggers allowed the same account to sign in and commit the rewrite under the same Auth process. The PostgreSQL error behind the 500 was not captured. The local Auth role was deliberately restricted, so this does not establish managed-project behavior or supported privileges.
What supported managed Supabase mechanism, if any, can: (1) reliably distinguish Auth's own credential maintenance from a user password change across all write routes; (2) provide commit-time fencing and a durable outcome for an interrupted reset without breaking maintenance; and (3) fence a login/refresh started before reset but completing afterward? Are triggers on auth.users plus private SECURITY DEFINER functions and role/temporary-schema restrictions supported across managed upgrades, or is a different documented integration recommended?
I am seeking a supported architecture and documentation, not treating the local HTTP 500 as proof of a hosted-provider defect. No production project or user data was involved.
All reactions