Phase 10, part 1: users, sessions and form tokens

Schema 32 adds users, memberships and sessions, on both databases.
internal/users keeps accounts (Argon2id passwords), roles and modules,
and sessions whose rows hold only a hash of the cookie's value; they end
after a week unused or 30 days. On the server edition, every page but the
public ones needs a session and sends others to /signin. Every edition's
forms now carry a CSRF token (a cookie repeated in each form, added as
pages render; htmx sends it as a header), besides the same-origin check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 08:49:45 -07:00
co-authored by Claude Opus 5.5
parent d537fb367f
commit 96404a3580
21 changed files with 1475 additions and 21 deletions
+26
View File
@@ -2159,6 +2159,32 @@ imported into Postgres, shows the same pages.
This makes the server edition usable by a team. A commit after each part.
**Part 1 built 2026-11-25** on branch `phase10`.
- Schema 32 (both databases): `users` (email without case, kind, status,
an Argon2id hash in the PHC format, title, time zone, photo, emails
wanted, last sign-in), `memberships` (team 1 until Phase 13, role, a
member's modules as `{"seo":"edit"}`) and `sessions`.
- `internal/users`: accounts, memberships (`Can(module, write)`; owners and
admins can do everything) and sessions. A session's row keeps the
SHA-256 of the cookie's random value, never the value. A session ends
after 7 days unused or 30 days in all, or when its user is disabled or
leaves the team. Passwords need 10 characters, and can't be the email;
a sign-in for an unknown address takes as long as a real one.
- The cookie is `HttpOnly`, `SameSite=Lax`, and over https `Secure` with
the `__Host-` prefix. On the server edition every page but the public
ones (`/healthz`, `/static/`, `/mcp`, `/signin`, `/setup`) sends someone
not signed in to `/signin?next=…`; htmx gets a 401 with `HX-Redirect`.
- The local edition is unchanged: every request is the owner's.
- CSRF, on every edition: a random token in its own cookie, repeated in
every form (double-submit). Pages are rendered into a buffer and each
`method="post"` form to one of the app's own addresses gets a hidden
field (and a file upload, the token in its address, so its handler
still limits the body); htmx sends it as `X-CSRF-Token` (app.js). Forms
that post elsewhere never get it. `/mcp` has its key instead. The
same-origin check stays.
- Sessions aren't exported (`supermarket export`); users and memberships
are.
1. **Users and sessions.**
- Tables:
- `users`: email, name, kind (person or agent), status (invited,