Skip to content

feat(auth): sign in with an emailed code, sign-in only (F-A04, D-042) - #220

Merged
aRustyDev merged 11 commits into
mainfrom
feat/email-code
Nov 16, 2026
Merged

aRustyDev merged 11 commits into
mainfrom
feat/email-code

Conversation

@aRustyDev

Copy link
Copy Markdown
Owner

What

E4 of the email recovery (F-A04, P1): signing in with an emailed code, sign-in only (the owner: "E4 is sign-in only"). POST /auth/magic-generate/ and POST /auth/magic-sign-in/, the two routes behind the stock app's "Sign in with unique code" button. It is on wherever the password reset by email is, and dark everywhere else: one switch (a mail service and THRUST_PUBLIC_BASE_URL), nothing new to configure, no web change.

  • A code is six digits (the pin's range, drawn without bias), lives ten minutes, allows five tries (the fifth wrong one ends it), and a ten-minute window makes at most four codes for an address (the fifth request is EMAIL_CODE_ATTEMPT_EXHAUSTED_SIGN_IN). A right code is used up with the count. These are the pin's own numbers, measured.
  • A code is made and mailed only for an address an account has; asking for any other address is answered as a sent one and mails nothing. A mail service that refuses the message leaves no code and no count against the person.
  • The form is the pin's native post, answered by a 302 in the pin's order of refusals; its CSRF token (field and cookie) is always checked first.
  • Store: login_codes (migration 0018), one row an address, the digest of the code and its counts; three atomic calls (issue_login_code, check_login_code, discard_login_code).

Why this matters now

With E1 to E3 merged, any deployment that has mail configured says is_smtp_configured: true, and the stock app then draws "Sign in with unique code" under the password. That button called a route Thrust did not have. Staging reaches that state at its first deploy. This PR gives the button its routes.

For the owner to decide

D-042 is proposed (.agents/plans/contrail/DEVIATIONS.md) and needs your confirmation. It differs from the pin in nine ways; the ones that matter:

  1. No sign-up by code (/auth/magic-sign-up/ is not built). The pin's MAGIC_CODE answer of email-check for an unknown address is not given, so the app only offers the code under the password of an address that has an account.
  2. An address that is not one is 400 INVALID_EMAIL; the pin's own answer is a 500 with an HTML body.
  3. The form's CSRF token is always asked for and a form without it is a 403 that tries nothing; the pin shows its own failure page. This is what stops a page that is not ours from signing a visitor in.
  4. The tries of an address are limited like a password's; the pin throttles every auth POST per IP.
  5. One switch (mail); the pin can turn the code off and leave the reset on (ENABLE_MAGIC_LINK_LOGIN).
  6. Plain-text mail sent in the request (so a refusing mail service is the generic 500 of D-041 (5)), relative redirects, Thrust's cookies.

How it was checked

  • The pin, recorded as X12-dark (3 exchanges), X12 (10), X13-dark (5), X13 (17), X13-deactivated (5). The pin's code is read from its Valkey, so the fixtures say {code}. A second recording of the whole journey from a fresh oracle reproduced all 78 fixtures byte for byte. Acceptance list: 61 rows. Parity: 111 of 111, with three controls of the row (a success that sets no session, a refusal with no error_code, a changed recording).
  • Generated tests first. thrust-core/tests/login_code.rs: a model_ sequence of requests, right and wrong tries, time (the edges of a code's life and of a window drawn on purpose), a mail service going down and up, accounts deactivated and reactivated, against a model of the pin's rules after every step, with a control that every outcome was reached. The memory adapter runs all 55,986 sequences of up to six steps against a reference written as the pin stores it (three Redis-like keys). The Postgres adapter's generated model has the three calls and four sequences that reach the edges (two hand-broken lines of SQL first survived the random sequences, so they now reach them on purpose). thrust-compat-plane/tests/magic.rs (55): every body, every combination of CSRF state by address by code, next_path at eight values, the cap with a stranger never counted, deactivation, the limiter, and two properties (any form field text and any request bytes: never a server error, always a redirect that stays in the app).
  • Every contract cell. main 71 of 71 and mail 7 of 7 on native-memory, native-pg, worker-memory and worker-cf. The four new mail tests ask by the route, read the code out of the message the target sent (Mailpit on the native targets, wrangler's send_email simulator on the Worker's), and sign in with it.
  • Controls. Four core and two compat planted bugs, six hand controls of the memory store, eight of the Postgres SQL, and six scripts/mutants.sh rows run by hand on native-memory (each exit 101 with the one test aimed at it). The hermetic suite, clippy (native, wasm32, openapi), deny, fmt, the whole Postgres adapter suite (82 of 82) and all 44 steps of the ci-scripts and deps jobs pass locally; the book builds with no error.
  • What only a real cell showed. worker-cf answered 500 to the form on the first version: the Cloudflare limiter has two bindings and refuses any key that does not start with signin: or v1:, which no in-memory limiter notices. The key is signin:code:<address> now, and a core test holds it to the two classes (it fails with the old key). Main's migration ledger tests had made-up probe files numbered 0018, the number this change's migration took; they start at 0019 now.

Not covered (G-41 says the same)

  • A browser journey of the form: the e2e targets have no mail service, so the app does not draw the button and the sign-in journey holds that it does not. What only a browser shows (the page's hidden field, its cookie, the app's own step) is untested. A journey needs a target with mail and a way to read the code: a follow-up if wanted.
  • A code mailed from a deployed Worker: nothing is deployed; the first staging deploy after E3 is the first real send of the reset too.
  • The pin's per-IP throttle; two tries at one code at the same instant (each Postgres statement is atomic, the model is sequential); the pin's HTML failure page for a missing CSRF token, its 500 for a bad address and a form-encoded request for a code (measured, not recorded: the format holds JSON bodies); scripts/mutants.sh as a whole (its six new rows by hand).

Refs: #46

🤖 Generated with Claude Code

aRustyDev and others added 10 commits November 16, 2026 01:56
The store under the sign-in with an emailed code, in the port and both adapters, before anything uses it.

One row a user in `login_codes` (migration 0018): the digest of the code that works, its tries, and the count of codes made in the window the
first of them opened. Three calls, each atomic: `issue_login_code` (a new code replaces the old and starts the tries again; at most `max_issued` in a
window of `window_secs` that the later ones do not lengthen; the next is `Capped` and stores nothing), `check_login_code` (every try at a code that works is
counted first; the last wrong one removes the code and keeps the count of codes made; the right one at the last allowed try still counts and is used up
by the statement that finds it, with the count, so one code signs one person in once) and `discard_login_code` (a code the mail service refused is taken
back with its place in the count). They are the pin's own rules (`MagicCodeProvider`: ten minutes, five tries, four codes in ten minutes).

Held three ways: the memory adapter's own test runs every one of the 55,986 sequences of up to six steps against a reference written as the pin stores it
(three Redis-like keys with their own expiries); the Postgres adapter's generated model has the three calls and four sequences that reach the edges on
purpose (two hand-broken lines of SQL first survived the random sequences: a take-back of the only code, and a new code that kept the old one's tries,
so `CodeCap` and `CodeAttempts` now do both); and the schema snapshot has the table and its two constraints. Six hand controls of the memory store and
eight of the SQL each fail their suite.

The migration ledger's tests made up probe files numbered 0018 and above; 0018 is a real file now, so theirs start at 0019.

Refs: #46
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
`request_login_code` and `sign_in_with_code`, over the store of the commit before and the `EmailSender` port of the reset (D-041). Sign-in only
(the owner, 2026-11-16: "E4 is sign-in only"): a code is made, and mailed, only for an address an account has, and the request for one nobody has is
answered as a sent one, so it says nothing that email-check does not and sends no mail to a stranger.

The code is six digits, 100000 to 999999 as the pin draws them, taken from 32 random bits with the biased tail thrown away; it lives ten minutes, allows five
tries, a window makes at most four, and the tries of an address are limited like a password's, under `signin:code:<address>` (the Worker's Cloudflare limiter
has two bindings, `signin:` and `v1:`, and refuses any other key). The order of refusals is the pin's. A right code for a deactivated account is used up and
refused. A mail service that refuses the message leaves no code and no count against the person. `mail_configured` is the one switch of the reset and of the
code, because the stock app draws both of its controls from one flag.

Tests: a `model_` sequence of requests, right and wrong tries, time (the edges of a code's life and of a window drawn on purpose), a mail service that goes
down and up, and accounts deactivated and reactivated, against a model of the pin's rules after every step, with a control that every outcome was reached;
the pin's order for a request and a try over every combination; the digits; the limiter's key held to the Worker's two classes by a limiter that refuses
the others (it fails with the key of the first version, which only the `worker-cf` cell had noticed). Four planted bugs (`mutant_code_*`) each fail the model.

Refs: #46
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… (D-042)

`POST /auth/magic-generate/` (JSON) and `POST /auth/magic-sign-in/` (the native form `unique-code.tsx` posts, answered by a 302 whatever it comes to),
the two routes behind the stock app's "Sign in with unique code", with `is_magic_login_enabled` following `is_smtp_configured`: on wherever the reset
by email is, dark everywhere else, and no web change.

Recorded from the pinned Plane as X12-dark, X12, X13-dark, X13 and X13-deactivated (40 exchanges; the pin's code is read from its Valkey, so the fixtures
say `{code}`; the 73 other fixtures came out byte-identical; the recorder's `set_smtp` changes its row in place now, because the model deletes softly and the
second set of a deleted row collided with its unique key). The replay types the code its own mail service was given, checks that a right code sets the
session and a refused one sets none, and has three controls (no session on a success, no `error_code` on a refusal, a changed recording). The acceptance
list is 61 rows. 111 parity tests pass.

Deviations, in D-042 (proposed): no sign-up by code; an address that is not one is a 400 where the pin has a 500; the form's CSRF token is always asked for
(403, nothing tried) where the pin shows its own failure page; the tries are limited per address where the pin throttles per IP; the mail is plain text and
in the request; one switch, mail, where the pin has `ENABLE_MAGIC_LINK_LOGIN`; relative redirects.

`crates/thrust-compat-plane/tests/magic.rs` (55 tests) states what a recording cannot: every body, every combination of CSRF state, address and code, the
`next_path` at eight values by a right and a wrong code, the five tries, the cap with a stranger never counted, deactivation, the limiter, and two properties
(any form, any bytes: never a server error, always a redirect that stays in the app). Two compat mutants (`mutant_magic_*`) fail 11 and 12 of them.

Refs: #46
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The main pass holds the route dark on a target with no mail (`EXPECT_MAIN` 71, and stage 2's 65): the request is 5025 with the address, the form is the pin's
order (5085, 5060, 5025 with its `magic_` quirk), a form without the page's CSRF token is a 403, and the app is told both are off.

The `mail` pass (`EXPECT_MAIL` 7) has four tests that ask by the route, read the code out of the message the target sent, and sign in with it: a real mail
server (Mailpit) on the native targets and wrangler's `send_email` simulator on the Worker's. The mail and its code, a wrong code and a form with no CSRF token
first, the session, the code used up; a stranger mailed nothing, four codes and the fifth refused, only the newest of four signing in; five wrong tries and a
new code that works; a deactivated account. 71 of 71 and 7 of 7 on `native-memory`, `native-pg`, `worker-memory` and `worker-cf`.

Six `scripts/mutants.sh` rows (and the feature forwards in `thrust-server`), run by hand on `native-memory`: each exit 101 with the one contract test aimed at it.

Refs: #46
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
D-042 (proposed, for the owner: the nine ways it differs from the pin) and the earlier rows that said the email code had no route; G-41 with what was
measured and what was not (no browser journey, nothing from a deployed Worker, no per-IP throttle); the runbook's section, the port table, F-A04's row,
Q-0050.3, the mutation and contract-suite pages and the CI page's counts, and the sign-in journey's comments.

Refs: #46
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…odicity check

The property over any 32-bit draw added 900000 to a draw that can be within 900000 of `u32::MAX`, which a generated draw found on the first run after main was
merged in (attempt to add with overflow). The overflow was in the test, not in the mapping (`code_from_bits` is a comparison and a remainder): the check
uses `checked_add` now, and the four draws that have no later draw of the same code are pinned as a `regression_` test.

Refs: #46
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…ge 0.2.0)

main's lock names thrust-edge 0.1.0 while the workspace is at 0.2.0 (the release commit bumped the version, and the edge crate was added after it with a lock
made before): every `cargo ... --locked` step of CI refuses it, so this branch's lint, hermetic and contract jobs would too. One line, what cargo itself writes.

Refs: #46
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
#221 moved every crate (crates/thrust-core is crates/core/usecases, and
so on), so this branch conflicted with main.

- The 11 files this branch adds under an old directory go to the new one,
  where git's directory-rename detection put them (login_code.rs and its
  tests, magic.rs and its tests, the X12 and X13 fixtures, migration 0018,
  the memory adapter's login_codes.rs).
- The 3 content conflicts (docs/src/testing/status.md,
  docs/src/testing/techniques/mutation.md,
  tools/compat-record/gen-acceptance.py) take this branch's side, whose
  only difference from main's was main's path rewrite. Then the same
  rewrite #221 used gives those lines, and two comments this branch added
  (crates/adapters/memory/tests/login_codes.rs,
  crates/api/compat-plane/tests/common/mod.rs), the new paths.

Checks:
- No file outside .agents/plans and CHANGELOG.md names crates/thrust-.
- status.md: every one of main's 60 rows is byte-identical to main's, and
  G-42 is the only row added.
- Against main, this branch now changes the same 47 files as before the
  merge, at their new paths, with the same line counts. The 48th, the
  Cargo.lock line for thrust-edge 0.2.0, is already on main.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@aRustyDev
aRustyDev merged commit 4681c27 into main Nov 16, 2026
72 of 74 checks passed
@aRustyDev
aRustyDev deleted the feat/email-code branch November 16, 2026 20:14
aRustyDev added a commit that referenced this pull request Nov 16, 2026
#226)

* ci: guard ADR-0009's crate layout in the deps job (scripts/check-layout.sh)

The layout PR 3 (#221) made is kept by a check, run by CI's deps job with
its --self-test beside it. It reads the tracked files (git ls-files) and
the workspace (cargo metadata --no-deps) and refuses:

1. a workspace member outside crates/;
2. a directory under crates/ that is neither a crate nor a group; a crate
   inside another; a group with no README.md, with a file other than its
   README.md (a manifest beside it: a crate and a group at once), or whose
   README.md does not name a directory in it as `<name>/`;
3. a normal or build dependency on a crate in testing/ or spikes/;
4. a normal or build dependency of a crate in core/ on one outside core/.
   Dev-dependencies are allowed in 3 and 4: a test uses a fixture, and
   thrust-core's tests run against the memory adapters (the scope
   check-deps.sh already uses for core);
5. a stale path where nothing else would notice one: a workflow's
   paths:/paths-ignore: entry that names no tracked file (or a glob over
   a directory with none), a .cargo/mutants.toml exclude_globs or
   examine_globs entry likewise, and a .gitignore line under crates/
   that lies inside no crate. These are the three silent traps found
   while planning the move.

It exits 2, not 0, when there is nothing to check (no member, no tracked
file under crates/). Its success line counts what it read: on this tree,
19 crates, 9 groups, 23 workflow filter entries, 5 mutants.toml globs and
9 .gitignore lines.

plan-ci.py: the guard joins the deps job's guards. A group's README.md,
which ran nothing, and .gitignore, which matched no rule, now run deps,
because the guard reads them. test_plan_ci.py follows: passed=5016.

Controls: --self-test runs 23 cases on a small world. The consistent world
passes; each broken one must exit 1 with the message of the rule it breaks,
not merely fail, and an empty world must exit 2. On the real tree, a
tracked crates/stray.txt makes the check exit 1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* docs: the layout guard, and the plan's status after the move

- docs/src/testing/techniques/architecture-gates.md: check-layout.sh as
  the second architecture gate (what it reads, its rules, its control).
- docs/src/testing/running.md: the deps job's row named only check-deps
  and check-topology; it now names every guard the job runs.
- docs/src/contributing/crates.md, crates/README.md: the guard checks
  what "Adding a crate" asks for. crates/core/README.md: core/'s rule is
  on normal and build dependencies; thrust-core's dev-dependency on the
  memory adapters is allowed.
- The plan: PR 3 merged in #221, and how #220 was brought across the
  move; PR 4's choices as built.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
aRustyDev added a commit that referenced this pull request Nov 16, 2026
The sign-in with an emailed code (E4, PR #220, merged as 4681c27) went in with D-042
proposed. The owner confirmed it on 2026-11-16 ("confirm D42"), all nine
deviations, in the same words D-041's confirmation used. Only the decision cell of
its row changes.

Refs: #46

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant