Two-step sign-in

3 min read

Who may turn on a code from an authenticator app, who must use one and where, how it is enforced on the server, and resetting it for a lost phone.

Two-step sign-in asks for a 6-digit code from an authenticator app as well as a password. It is the two-factor module, on unless the site's config says two-factor: false. It uses the database platform's own multi-factor sign-in, so the site never keeps a secret or checks a code itself.

The policy

Settings → Two-step sign-in edits one file, content/two-factor.md (rows in cloud mode, published like any content). It answers two questions.

  • Who may turn it on: everyone, nobody, or only the people you choose. They do it from their account page.

  • Where it is required: a list of requirements. Each one says:

    • who it covers, ticked from the same list a team's membership uses (administrators, everyone on the staff, coaches, a membership tier, a live membership); none means everyone signed in;
    • the addresses it applies to: /admin is that page, /admin/* is the admin and everything under it, /app/* the app;
    • the server functions it applies to, by name: payouts, social-* for every function whose name starts that way, * for all.

    A requirement naming neither addresses nor functions applies everywhere the person is signed in.

With no file, it is offered to everyone and required of nobody. Three examples:

# administrators, in the admin only
offer: nobody
require:
  - who: { role: admin }
    routes: [/admin/*]
# anyone may turn it on; nobody has to
offer: everyone
# everyone on the staff, everywhere; members may choose it
offer: { entitlement: subscribed }
require:
  - who: { role: [admin, provider, editor, support, analyst] }

The rules are the same grammar every audience on the site uses, so a file can also name a tier, an offer someone bought (with a window), a booking or a course. A tier no membership declares, or an offer the site does not sell, stops the build with its name rather than matching nobody.

What a person sees

  • On their account page: a Two-step sign-in section, while it is offered to them or required of them. They turn it on by scanning a code (or typing a key) into an authenticator app and confirming with its first code. Turning it on signs them out of their other devices.
    • Once it is on, they can add a backup app on another device.
    • They can remove an app, but not the last one while it is required of them.
  • The code screen: it stands in front of the page, and nothing behind it shows.
    • Someone who has turned it on is asked on every page after signing in with a password.
    • Someone a requirement covers is asked on the pages it names. If they have no app yet, they are walked through setting one up first.
    • Sign out is always there.

How it holds on the server

Hiding a page is not protection, so every requirement is also enforced where data is served.

  • Someone who has turned it on, or whom a requirement covers everywhere: until they enter the code, the database answers every request with 403 two_factor_required. A check runs before every request the database API serves (two_factor_pre_request, which the module points the API at in its data file). Every server function refuses them the same way. The one exception is the read the code screen needs to know where they stand.
  • A function a requirement names: it refuses them with 403 two_factor_required.
  • A requirement covering any address under /admin: it also holds their staff powers until the code.
    • The database's staff checks (has_role, is_staff) answer no for them.
    • The server functions see them as having no role.
    • So the admin's data cannot be read with a password alone, through the admin or around it.

Every server function starts through serve() in _shared/serve.ts, which is what lets the caller check in getUser apply to all of them. serve-everywhere.test.mts refuses a function that starts any other way.

A lost phone

An administrator opens the person in People and chooses Reset two-step sign-in. This removes every app they set up, signs them out everywhere, and is recorded with who did it and when (two_factor_resets). Nobody can reset their own; another administrator does.

If you require it of every administrator, keep at least two administrators, so one can always reset the other.

Hosting

The local stack's sign-in service enables authenticator apps while the module is on: the assembler keeps that block of supabase/config.toml in step. Hosted projects have authenticator apps on by default, at no cost. If yours has been changed, it is under Authentication → Multi-factor in the Supabase dashboard.

Testing

  • two-factor-policy.test.mts (Node) covers the policy, the patterns and the config.
  • two-factor-db.test.mts covers the served flow, with real enrolment and codes computed in the test: required pages, named functions, staff data, the check before every request, and the reset.
  • two-factor.test.mts drives the screens in a headless browser.