carlos / platform Public

spec: console email sign-in (magic links), Keymail kept beside it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 49eaacf0ccabbda66ce6df96ff92488df794bbf6 parent 4d6f0e6
1 file changed, +353 −0
  • docs/superpowers/specs/2026-10-21-console-email-signin-design.md +353 −0

CI passed — run details

diff --git a/docs/superpowers/specs/2026-10-21-console-email-signin-design.md b/docs/superpowers/specs/2026-10-21-console-email-signin-design.md
new file mode 100644
index 000000000..0d4a37844
--- /dev/null
+++ b/docs/superpowers/specs/2026-10-21-console-email-signin-design.md
@@ -0,0 +1,353 @@
+# Console email sign-in (magic links), with Keymail as the alternative
+
+Status: draft rev 1, 2026-10-21. Follows
+[console invite delivery](2026-09-28-console-invite-delivery-design.md),
+whose §4 console mailer this reuses and whose line 4 named this spec.
+
+## 1. Why
+
+The console's only door is "Sign in with Keymail". Anyone without a Keymail
+mailbox cannot sign up, cannot accept an invitation (invites now reach
+plain email addresses, but accepting needs a session as that address),
+and cannot finish `carlos auth login`. For Carloku, a general-use product,
+that is the front door shut to almost everyone.
+
+Success: a person with any email address can sign up and sign in to the
+console with a link sent to that address; an owner can invite any email
+address and the invitee can accept; `carlos auth login` and every
+re-confirmed action work for that person; Keymail users carry on exactly
+as today.
+
+## 2. Rulings (Paul, 2026-10-21)
+
+- **R1. Re-confirmation for email users is a fresh email link.** Sensitive
+ actions that today demand a fresh Keymail passkey gesture
+ (`requireFreshAuth`) demand, for a session that signed in by email, a
+ fresh link sent to the same address. Passkeys for email users are a
+ later piece of work.
+- **R2. Sign-up by email is open to anyone, rate-limited.** Same as
+ Keymail today: the first successful link for a new address creates the
+ user, and the existing first-landing path mints their personal account.
+ Caps per address, per client IP and overall stop the form being a way to
+ send mail to strangers.
+- **R3. Console email comes from a carloku.com address on carloku.com.**
+ `CARLOS_CONSOLE_MAIL_FROM=noreply@carloku.com` (builder's call on the
+ local part; see §9). The platform default
+ (`noreply@console-mail.bab.<suffix>`) stays the default for other
+ deployments.
+- **R4. Enable it.** Paul's instruction ("finish it out and enable it")
+ is the security ruling the invite-delivery rollout was waiting on for
+ the `ses:SendEmail` grant (`console_mail_from` / `console_mail_region`).
+ Roll order stays dev first, then flagship.
+
+## 3. Goals, non-goals, anti-goals
+
+Goals
+- Email magic-link sign-in and sign-up on the console's front page, with
+ "Sign in with Keymail" kept beside it.
+- A code in the same email, so a link opened on a phone can still finish
+ a sign-in started on a laptop.
+- Re-confirmation (step-up) by email link or code for email sessions.
+- Invites a non-Keymail invitee can accept, with the invite page sending
+ the invitee through sign-in and back.
+- `carlos auth login` works end to end for an email user.
+- Enabled and verified live on dev, then carloku.com.
+
+Non-goals
+- Passkeys / WebAuthn in the console (R1 defers it).
+- CAPTCHA or bot scoring. Rate limits only.
+- Linking two addresses into one person. The address is the identity, as
+ today.
+- Changing the Keymail OAuth flow, Keymail step-up, or the existing
+ Keymail Apps-inbox confirmation link beyond routing it through the new
+ sender.
+- Tito's console (blocked on the store boundary; it picks this up with
+ its next release like any other deployment).
+
+Anti-goals (a failure even if it works)
+- **A downgrade.** A session that signed in with Keymail must never be
+ able to re-confirm by email instead of its passkey. A stolen Keymail
+ session gains nothing from this feature.
+- **An oracle.** The front page must not reveal whether an address has an
+ account, a Keymail mailbox, or was rate-limited.
+- **A mail cannon.** Nobody can make the console send more than a handful
+ of messages to an address they do not control.
+- **Link scanners burning links.** Corporate mail scanners fetch every
+ URL in a message. A GET must never consume a token.
+- Mail to `@keymail.dev`-style addresses by SMTP. Keymail's MX refuses or
+ screens unknown senders, so Keymail mailboxes are reached through
+ Keymail, never through SES.
+
+## 4. How a link reaches the person
+
+One sender, `signinSender`, used for sign-in links and for step-up links.
+It reuses the invite chain's order and its rules, minus the owner-visible
+fallback link:
+
+1. **The address has a stored Keymail grant** (`store.GrantToken` non-empty):
+ `SendApp` into its Keymail Apps inbox. This is what `/auth/continue`
+ does today.
+2. **Otherwise, Keymail federation** (`sendFederated`, as
+ `console@<origin host>`). `IsKeymailBlocked` stops here: the person
+ blocked the console, and email to the same address would go round
+ them. `ErrNoKeymailMailbox` moves on.
+3. **Otherwise, the console mailer** (`ConsoleMailer.Send`, SES).
+4. Nothing worked: log it. The person sees the same page they would have
+ seen on success (anti-oracle); the page says what to do if nothing
+ arrives.
+
+A send that fails at step 1 with a dead grant (401) falls through to
+step 2, as a revoked grant is not a refusal.
+
+**Sends run off the request.** The handler records the challenge, starts
+the send on a background goroutine with its own bounded context
+(20 s total, Keymail's share 12 s, the invite numbers), and renders the
+"check your email" page at once. This removes the timing oracle (a
+federation lookup takes visibly longer than a grant send) and keeps a
+slow PoW off the request. A process-wide semaphore (builder's call on the
+size; 4 is fine) bounds concurrent sends; a send that cannot get a slot
+within its context is dropped and logged.
+
+## 5. The challenge
+
+A **challenge** is one emailed link + code, for one purpose, bound to the
+browser that asked.
+
+Table (new, in its own file with a `migrateEmailSignin()` per the
+authstore/step-up precedent, and classified in `claimSQLTables`):
+
+```
+email_challenges(
+ id_hash TEXT PRIMARY KEY, -- sha256 of the browser-held id
+ link_hash TEXT UNIQUE, -- sha256 of the emailed link token
+ code_hash TEXT NOT NULL, -- sha256(id || code)
+ purpose TEXT NOT NULL, -- 'signin' | 'stepup'
+ address TEXT NOT NULL,
+ session_hash TEXT NOT NULL DEFAULT '', -- stepup: the session it serves
+ pending TEXT NOT NULL DEFAULT '', -- stepup: pending_actions token hash
+ next TEXT NOT NULL DEFAULT '', -- signin: local path to land on
+ attempts INTEGER NOT NULL DEFAULT 0,
+ created_at INTEGER NOT NULL,
+ expires_at INTEGER NOT NULL
+)
+```
+
+- **Lifetime 15 minutes** (`ConfirmationTTL`'s number). Single use: a
+ successful link or code deletes the row in the same transaction that
+ checks it (burn-then-check, the `TakeConfirmation` shape).
+- **The browser holds the id** in an HttpOnly cookie
+ (`carlos_console_email`, `Secure` on https, `SameSite=Lax`, 15 min).
+ Only hashes are stored, so a database read is not a sign-in.
+- **The code** is 6 digits from crypto/rand, hashed with the challenge id
+ so a code table leak is not a list of codes. **5 wrong attempts** burn
+ the challenge. A code is only ever checked against the challenge named
+ by this browser's cookie, so there is no global code space to guess in.
+- **The link token** is 32 random bytes, base64url, the `randomToken`
+ shape.
+- One live challenge per (browser, purpose): asking again replaces the
+ browser's previous one.
+
+### 5.1 Sign-in by link
+
+`GET /auth/email/link?t=<token>` never consumes anything. It looks the
+token up and renders a confirm page ("Sign in as you@example.com", one
+button) carrying the token in a POST form. Unknown or expired tokens get
+the "link expired" message.
+
+`POST /auth/email/link` (token in the body) consumes the challenge and
+signs **this** browser in, whichever browser it is. That is what a magic
+link means: possession of the mailbox. The confirm page names the
+address, so a forwarded link (login CSRF) shows whose account it opens.
+No CSRF token exists before sign-in; the POST is protected by the
+unguessable token itself and by `SameSite=Lax` on the cookies it sets.
+
+### 5.2 Sign-in by code
+
+The "check your email" page has a code box. `POST /auth/email/code`
+checks the code against the challenge named by this browser's cookie and
+signs this browser in. Wrong code: the page again, with an error, the
+attempts counter up. This is the cross-device path.
+
+### 5.3 What signing in does
+
+Exactly what the Keymail callback does after it has an address, so both
+doors share one function (`finishSignin(w, r, address, method, next)`):
+`EnsureUser` (sign-up when `created`), `CreateSession`, session cookie,
+welcome cookie when created, then `next` if set and safe, else
+`redirectToCurrentAccount`. No grant token is stored for an email sign-in
+(it has none), and an existing grant is left alone.
+
+### 5.4 The session remembers how it was proved
+
+`sessions` gains `method TEXT NOT NULL DEFAULT 'keymail'`. Existing rows
+read as `keymail`, which is true of every session today (the
+Apps-inbox confirmation link is a Keymail channel and stays `keymail`). A
+session made by §5.1 or §5.2 is `email`. `session(r)` (or a sibling)
+exposes it.
+
+## 6. Re-confirmation (R1)
+
+`requireFreshAuth` branches on the session's method:
+
+- **`keymail`**: unchanged. Keymail step-up, fail closed when Keymail
+ cannot do it. No email path. (Anti-goal: no downgrade.)
+- **`email`**: stash the pending action exactly as today
+ (`StashPendingAction`), create a `stepup` challenge bound to this
+ session hash and that pending token, send it through `signinSender`,
+ and render a "check your email" page with a code box.
+ - `GET /auth/email/link?t=` for a stepup challenge renders a confirm
+ page; `POST` requires a session cookie whose hash equals the
+ challenge's `session_hash`. Opened in another browser it refuses
+ with "open this link in the browser where you started, or type the
+ code there". It never signs anyone in.
+ - `POST /auth/email/stepup/code` checks the code for the stepup
+ challenge named by this browser's cookie, and requires the same
+ session.
+ - On success: `RecordStepUp(raw, addr, now, now)` (the gesture time is
+ the moment the link or code was used), `TakePendingAction`, then
+ `resume`, the same tail as `handleStepUpCallback`.
+
+The five-minute `StepUpWindow` applies from that moment, as for Keymail.
+
+The emailed text for a step-up names the action ("confirm: approve a
+`carlos auth login` request", etc.) where the pending action's path maps
+cleanly to a description; otherwise a generic "confirm an action in the
+console". Builder's call on the mapping; it is copy, so it goes through
+copy review.
+
+## 7. Rate limits (R2)
+
+In-memory sliding windows, the `inviteWindow` shape (one console process
+per deployment). Refusals are invisible to the requester: the "check your
+email" page renders as normal and the send is skipped and logged. The
+numbers (builder may tune within reason):
+
+| Key | Limit |
+| --- | --- |
+| per address, any purpose | 1 per 60 s, 5 per hour, 20 per day |
+| per client IP (`s.clientIP`, edge-proof aware) | 10 per hour |
+| whole console | 200 per hour |
+
+Step-up challenges count against the address but not the IP or global
+caps (the requester is already signed in as that address). Code attempts
+are bounded per challenge (§5), not here.
+
+Bounces and complaints: the console's sending identity is an ordinary
+platform mail domain, so its configuration set and the account-level SES
+suppression list apply. Nothing new here.
+
+## 8. Where people come back to (`next`)
+
+Signed-out visits to pages that need a session today redirect to `/` and
+lose their place. Two of them matter for this feature:
+
+- `GET /invite/{token}` signed out: render the front page with the
+ invited address pre-filled ("Sign in as x@y to accept"), and `next` set
+ to the invite page. Keymail's button carries the same `next`.
+- `GET /auth/cli` signed out: `next=/auth/cli` (so a CLI user can sign in
+ and land on the code entry).
+
+`next` is accepted only as a local path: starts with `/`, not `//`, no
+backslash, parsed by `url.Parse` with no scheme or host. For email it is
+stored on the challenge row; for Keymail in a short cookie beside the
+auth cookie (`carlos_console_next`, 10 min). Anything else is dropped
+silently and the usual landing applies.
+
+## 9. Mail on carloku.com (R3)
+
+- `CARLOS_CONSOLE_MAIL_FROM=noreply@carloku.com` on the flagship console.
+ `mail.EnsureDomain` creates the SES identity for `carloku.com` under the
+ ops account (`bab`, referrer `console-mail`) and logs the DKIM records
+ it needs. Publish them in the carloku.com Route53 zone (in
+ carloku-infrastructure, where that zone is managed, unless the
+ console's own DNS writer already owns it).
+- Before relying on it, check that no tenant has a mail-domain claim on
+ `carloku.com` (it is served by an app; a claim conflict refuses
+ `EnsureDomain`) and that SES in the console's mail region is out of the
+ SES sandbox.
+- carloku-infrastructure's region-host module call for the console's box
+ sets `console_mail_from = "noreply@carloku.com"` and
+ `console_mail_region` to the console's mail region, a targeted apply
+ (whole-root applies restart every box).
+- Dev: the platform default From, verified on the dev platform suffix.
+- `CARLOS_HOST_CLAIM` still disables the console mailer (#250). If the
+ flagship console runs claims-on by the time this rolls, that is a
+ blocker to raise, not to work around.
+
+Message text says "CARLOS platform console" today. Whether carloku.com's
+messages say "Carloku" is a copy question for the copy review; if it
+needs a deployment display name, that is a small config addition the
+builder may make (`CARLOS_CONSOLE_NAME`, default "CARLOS platform").
+
+## 10. Front page and copy
+
+- Home: an email field and "Email me a sign-in link" as the primary
+ action; "Sign in with Keymail" as a second button below it. A console
+ with no console mailer configured still shows the email form, because
+ Keymail addresses are reached through Keymail; the page copy must not
+ promise plain email will arrive when no mailer exists (builder: a
+ `page` flag).
+- "Check your email" page (replaces `check.html`'s Keymail-only text):
+ where it was sent, the code box, "nothing arriving?" advice, and the
+ Keymail button.
+- Link confirm page, step-up "check your email" page, the three email
+ bodies (sign-in, step-up, invite), the invite page's signed-out
+ variant, the `team.html` invite copy (still says "the platform never
+ emails anyone"), and the invite form's "keymail address" error.
+- Every user-facing string goes through the copy-review skill before it
+ lands in a file. No em dashes ([[copy voice]]).
+
+## 11. Smaller changes that fall out
+
+- `notifyCLIIssued`: when the address has no Keymail grant, send the
+ notice through the console mailer instead of skipping it.
+- `handleContinue` (`POST /auth/continue`): becomes the email form's
+ target (it already takes `address`), so no new route is needed for
+ "request a link". The empty-address → `/auth/signin` redirect stays.
+- `handleConfirm` (`/auth/confirm?token=`, the old Apps-inbox link) keeps
+ working for links already sent; new sign-in links use §5.1. It must
+ stop signing in on GET (§3 anti-goal); give it the same confirm-page
+ shape.
+- `PurgeExpired` also clears expired `email_challenges`.
+- New routes go into `testdata/claim-routes.txt`, the invariant fixtures
+ and probes, like every route.
+
+## 12. Tests that prove it
+
+Through real wiring (the console fixture, the real store, the router),
+with fakes only at the edges (fake Keymail, fake console mailer, a
+controllable clock):
+
+- Sign-up by link for a new address: user created, personal account
+ minted, session `method=email`, welcome shown.
+- Link GET does not consume; POST does; second POST fails.
+- Code: right code signs in; 5 wrong codes burn; a code from another
+ browser's challenge does not work.
+- Delivery order: grant → SendApp; no grant, Keymail mailbox → federation;
+ `ErrNoKeymailMailbox` → console mailer; blocked → nothing sent; the
+ page is byte-identical in every case, including rate-limited.
+- Rate limits at each key.
+- Step-up: an email session doing a gated action gets an email challenge,
+ the pending action resumes after the code or same-session link; the link
+ in another browser refuses; a **keymail** session never gets an email
+ challenge (downgrade test).
+- `next`: invite page round trip for an email invitee from signed-out to
+ member; unsafe `next` values ignored.
+- `carlos auth login` approval by an email user end to end.
+- Migration on an existing database: old sessions read `keymail`.
+
+## 13. Rollout
+
+1. Merge (CI green on a fresh main merge), release, roll dev boxes → dev
+ console. Live check on dev: sign up with a plain email address, accept
+ an invite, approve a CLI login with an emailed code.
+2. Flagship: SES identity for carloku.com verified, region-host grant
+ applied (targeted), env set on the console unit, roll flagship boxes →
+ console. The same three live checks on carloku.com, plus a Keymail
+ sign-in to prove nothing regressed.
+
+## 14. Left to the builder
+
+Names of files, functions and routes beyond those fixed above; the
+semaphore size; the step-up action descriptions; tuning the rate numbers
+within the same order of magnitude; template structure.