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
+20 -6
View File
@@ -2,6 +2,7 @@
package web
import (
"bytes"
"context"
"embed"
"fmt"
@@ -311,7 +312,7 @@ func (s *Server) Handler() (http.Handler, error) {
})
}
return s.guard(mux), nil
return s.guard(s.csrf(s.sessions(mux))), nil
}
// guard protects the app, which has no login because only this machine can
@@ -509,6 +510,9 @@ type page struct {
NewProspects int // companies and people waiting for review, shown in the menu
Drafts int // outreach drafts waiting, shown in the menu
CSRF string // the form token, for htmx (app.js)
Viewer *viewer // who's looking
}
// Section is the top bar's first breadcrumb, from Nav.
@@ -563,23 +567,33 @@ func (s *Server) render(w http.ResponseWriter, r *http.Request, name string, p p
p.Flash = flashText(r.URL.Query())
}
p.Error = sentence(p.Error)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err := t.ExecuteTemplate(w, "layout.html", p); err != nil {
p.CSRF = csrfToken(r)
p.Viewer = viewerOf(r)
out, err := withCSRF(r, func(b *bytes.Buffer) error { return t.ExecuteTemplate(b, "layout.html", p) })
if err != nil {
s.Log.Error("render", "page", name, "err", err)
http.Error(w, "The page couldn't be shown: "+err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(out)
}
// fragment writes one named template (for htmx swaps).
func (s *Server) fragment(w http.ResponseWriter, page, name string, data any) {
func (s *Server) fragment(w http.ResponseWriter, r *http.Request, page, name string, data any) {
t, ok := s.pages[page]
if !ok {
http.Error(w, "no template "+page, http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err := t.ExecuteTemplate(w, name, data); err != nil {
out, err := withCSRF(r, func(b *bytes.Buffer) error { return t.ExecuteTemplate(b, name, data) })
if err != nil {
s.Log.Error("render fragment", "page", page, "name", name, "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(out)
}
// Messages shown after a redirect, by code, so no user text is reflected.