Repository navigation
Commit 65dcbe1
authored
Adds two-factor sign-in with authenticator apps (TOTP). Anyone can turn
it on for themselves, and admins can require it of everyone on the
instance. Until now a password, or a password reset link, was enough to
get into an account that holds a Claude subscription, starts agents that
push to the project's repositories and reads its secrets.
## What people get
- **Settings → Two-factor sign-in.** "Set up" opens a dialog with a QR
code that ripples in from the middle, the key in groups of four for apps
that can't scan, and an "Open in your app" link on phones (iOS and
Android hand `otpauth://` links to the passwords app or the
authenticator). The code field is a single input drawn as two groups of
three boxes, so pasting, the phone's one-time-code suggestions and
password managers fill it at once, and it submits on the sixth digit.
Then come ten recovery codes to copy, download as a text file, or print
on a page of their own. **Replace** (new phone) asks for a code from the
current app or a recovery code first. **Turn off** asks for a code too.
Recovery codes show how many are left and suggest new ones at three. A
switch, **Ask for a code when I sign in with Google too**, sits beside
them.
- **Signing in.** After a correct password the page asks for the code,
showing who is signing in ("Not you?"). A wrong code shakes the boxes,
and after the second miss the page says to check that the phone sets its
clock automatically. A recovery code works in place of the app code.
Five wrong codes in a row make codes from the app wait a minute, then
twice as long each time, up to an hour, with a live countdown; recovery
codes still work meanwhile. Signing in with a recovery code says how
many are left.
- **Google.** Google sign-ins don't ask for a code, since Google has its
own two-step verification, unless the person turns that on or the admins
do for everyone. Then Google's callback lands on `/two-factor`, which
finishes the sign-in and goes on to where it started.
- **Password resets.** A reset link for an account with two-factor asks
for a code, and the new password takes effect only once the code checks
out. Whoever reads someone's email can't lock them out with a reset link
alone.
- **Admin → Settings → Sign-in.** **Require two-factor sign-in** applies
to everyone who signs in with a password. **Ask for codes on Google
sign-in too** applies to Google sign-ins, and with the requirement on,
to people who sign in only with Google. A meter shows how many people
use it and who doesn't yet. Turning either switch on when it would stop
people asks first and names them, including the admin. Someone it is
required of who hasn't set it up sees a page that sets it up instead of
the app, and the control plane refuses their other requests, from
browser sessions and CLI and access tokens alike, with
`two_factor_setup_required`.
- **Admin → Users** shows who has two-factor, notes who still has to set
it up, and has **Reset two-factor** for people who lost their phone and
their recovery codes. Only owners can reset an owner's.
- **CLI.** `fctl account two-factor` (or `2fa`) has `status`, `enable`
(prints the QR code in the terminal, asks for the code, shows recovery
codes; `--json` plus `confirm` without a terminal), `disable`,
`recovery-codes` and `google on|off`. Admins get `fctl admin set
--require-two-factor on --two-factor-google on` and `fctl admin
reset-two-factor <email>`. `fctl whoami` and `fctl admin users` show
two-factor too. `fctl login` approves the device in the browser, so the
codes are asked for there.
- **Email**, when it's set up: turning two-factor on, replacing the app,
turning it off, an admin's reset, a sign-in with a recovery code, and
the first wait after wrong codes (whoever it was knew the password)
email the person. All of these go in the audit log.
## How it works
TOTP per RFC 6238 (HMAC-SHA1, six digits, 30-second steps, one step of
drift either way), in `fctl-domain` with RFC 4226 and RFC 6238 test
vectors. The QR code is made in Rust (`qrcode` crate, no default
features), so the web app and the terminal draw the same modules.
- **Setup.** Nothing is stored until the app's first code confirms the
secret. Until then the secret travels sealed for the person (AES-GCM
under the master key, bound to `totp-setup:<user>`, 15 minutes). D1 then
keeps it sealed again (`authenticator_apps`, bound to `totp:<user>`),
with SHA-256 hashes of the recovery codes (`recovery_codes`). Setting up
an app takes the password unless the browser signed in within 15
minutes; CLI and access tokens always count as old.
- **Sign-in.** A sign-in that asks for a code starts no session. It
leaves a challenge (`sign_in_challenges`, ten minutes, kept by its hash)
in an `HttpOnly` `__Host-fctl-2fa` cookie, so the second step works the
same after a password, after Google's redirect and after a reset link,
and survives a reload. Each code works once (`last_step`). A code is
checked, then the challenge claimed, then the code used up, so racing
requests yield one session and burn nothing.
- **The requirement** is checked on every request without an extra round
trip: the token lookup reads the instance's settings in the same query.
The reasoning, and what it leaves open (enrollment races, older CLIs
showing `two_factor_setup_required` as an HTTP 403), is in [ADR
0022](docs/adr/0022-two-factor-sign-in-with-authenticator-apps.md).
docs/architecture.md, docs/security.md, docs/cli.md,
docs/self-hosting.md and docs/getting-started.md describe the behavior.
**Compatibility.** `AuthResponse.me` becomes optional, since login and
reset answer with `two_factor` when a code is needed. Successful
sign-ins answer exactly as before, and nothing but the web app reads it.
New fields (`Me.two_factor`, `User.two_factor`, the instance settings)
are optional with defaults. Migration `0011_two_factor.sql` adds three
tables and a column. `ErrorCode::TwoFactorSetupRequired` is new; a CLI
from before it shows such errors as an HTTP 403 with the message.
## How I verified it
- `cargo xtask check` passes: fmt, generated types, Biome and
TypeScript, Clippy native and wasm, `cargo test`, Vitest, production
builds. New tests cover the TOTP vectors, base32, replay, the lockout
schedule, the policy (who needs it, when Google asks), recovery codes,
the QR matrix, the terminal QR (read back module by module), the code
input (paste, autofill, six-digit cap), the QR component and the web
helpers.
- End to end on `cargo xtask dev`, with the CLI, curl and Playwright,
computing codes from the secrets:
- setting up in Settings and from the CLI (with and without a terminal),
and replacing the app (current code first);
- signing in with password and code, wrong codes, five wrong codes sent
at once (all counted), the wait (app codes refused, recovery codes
accepted), a recovery code used twice (refused);
- a reset link with two-factor (the old password keeps working until the
code is entered);
- Google's second step (challenge inserted the way the callback leaves
it);
- requiring two-factor (the confirmation names who isn't set up), the
setup-only page, an access token refused until setup, and an admin reset
putting someone back on that page;
- the downloaded recovery codes file, and the print view, which shows
only the codes, from the dark theme too.
- The QR codes in the screenshots decode to the expected `otpauth://`
links (jsQR), with the key shown under "Can't scan it?".
- A code review pass found seven issues, all fixed here: tokens counting
as fresh sign-ins, replacing without a code, wrong codes counted by
read-modify-write, the wait being usable to lock owners out, a code
burned when a racing request claimed the challenge, the terminal quiet
zone, and `fctl admin set` when the requirement also stops the admin.
## Screenshots
At 31c7878, from `cargo xtask dev`'s accounts.
The code step after the password, and after five wrong codes:
| Code | Waiting |
|---|---|
| 
| 
|
Setting it up in Settings: the QR code with the key behind "Can't scan
it?", then the recovery codes, and the section once it's on:
| Scan | Recovery codes |
|---|---|
| 
| 
|

When the instance requires it, someone without it sees this instead of
the app (dark theme, and at 390 px):
| Desktop | Phone |
|---|---|
| 
| 
|
Admin → Settings → Sign-in, the confirmation that names who isn't set
up, and Admin → Users:

| Confirmation | Users |
|---|---|
| 
| 
|
The CLI's `enable`, in a terminal (the QR code prints dark on a light
ground with ANSI colors):
```
$ fctl account two-factor enable
Scan this with your authenticator app:
█▀▀▀▀▀█ █ ▄ ▄█▄ ▀█▄▄▀ █▀▀▀ ▀█▄ █▀▀▀▀▀█
█ ███ █ ▄▀█ ▀▄▀▀ ▀ ▀▄▄ █▄ ▄ ▀▄▄▀ █ ███ █
…
Or type in this key: M3K3 E46S BSGG LMTY KBWJ TFXL APJ3 K57W
Code from the app: 123456
✓ Two-factor sign-in is on: signing in with your password now also takes a code.
Recovery codes: each one signs you in once if you lose your phone.
Save them somewhere safe now, such as your password manager: they are shown once.
WCQJK-QRVVQ DJHTP-FXPTT
…
```
## Related
- hyperstructure/fctl-site#22: the privacy policy says what two-factor
sign-in keeps. Merge the two together.
- #104: passkeys, stacked on this pull request.
Session: https://app.fctl.sh/s/ses_01M58KEJWPFJKBCENF5HKRV5HV
---------
Co-authored-by: fctl-sh[bot] <343971779+fctl-sh[bot]@users.noreply.github.com>
1 parent 8e4bc48 commit 65dcbe1
113 files changed
Lines changed: 9460 additions & 202 deletions
File tree
- crates
- fctl-client/src
- fctl-control
- migrations
- src
- api
- fctl-domain
- src
- fctl-proto/src
- fctl/src
- cmd
- docs
- adr
- web/src
- components
- passkeys
- two-factor
- ui
- gen
- lib
- routes
- styles
- xtask/src
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| 58 | + | |
58 | 59 | | |
59 | 60 | | |
60 | 61 | | |
61 | 62 | | |
62 | 63 | | |
| 64 | + | |
63 | 65 | | |
64 | 66 | | |
65 | 67 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
154 | 154 | | |
155 | 155 | | |
156 | 156 | | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
157 | 255 | | |
158 | 256 | | |
159 | 257 | | |
| |||
611 | 709 | | |
612 | 710 | | |
613 | 711 | | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
614 | 718 | | |
615 | 719 | | |
616 | 720 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
169 | 169 | | |
170 | 170 | | |
171 | 171 | | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
172 | 177 | | |
173 | 178 | | |
174 | 179 | | |
| |||
190 | 195 | | |
191 | 196 | | |
192 | 197 | | |
193 | | - | |
| 198 | + | |
194 | 199 | | |
195 | 200 | | |
196 | 201 | | |
| |||
398 | 403 | | |
399 | 404 | | |
400 | 405 | | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
401 | 441 | | |
402 | 442 | | |
403 | 443 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
444 | 444 | | |
445 | 445 | | |
446 | 446 | | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
447 | 458 | | |
448 | 459 | | |
449 | 460 | | |
| |||
0 commit comments