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
+14 -1
View File
@@ -32,6 +32,9 @@ import (
const testToken = "0123456789abcdef0123456789abcdef"
// testCSRF is the form token test requests carry.
const testCSRF = "test-csrf-token-0123456789abcdef0123"
func newTestServer(t *testing.T, c *core.Core) (*Server, http.Handler) {
t.Helper()
dir := t.TempDir()
@@ -63,8 +66,18 @@ func newTestServer(t *testing.T, c *core.Core) (*Server, http.Handler) {
func do(h http.Handler, method, target string, body io.Reader, hdr map[string]string) *httptest.ResponseRecorder {
req := httptest.NewRequest(method, target, body)
req.Host = "localhost:8470"
// Requests that change something carry the form token, as the app's
// pages do; a test that sets X-CSRF-Token itself ("" for none) checks it.
if !safeMethod(method) {
req.AddCookie(&http.Cookie{Name: csrfCookie, Value: testCSRF})
if _, ok := hdr[csrfHeader]; !ok {
req.Header.Set(csrfHeader, testCSRF)
}
}
for k, v := range hdr {
req.Header.Set(k, v)
if v != "" {
req.Header.Set(k, v)
}
}
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)