Files
meshcore-analyzer/cmd/server/admin_users_handlers.go
T
efitenandClaude Opus 5.5 d232072f85 feat: optional user accounts (part A: foundation) (#2129)
Part A of #2128: optional, off-by-default user accounts. With the
feature off nothing changes; with it on, visitors can register and log
in, and admins manage users and use the operator actions without the API
key.

PR #2130 (settings sync) builds on this one. The two are meant to be
merged together.

## The situation

- Operator actions (geofilter save and prune, backup, perf reset) need
the shared `apiKey`. There is no per-person right.
- Nothing in CoreScope knows who a visitor is, so the requests in #2128
that need that (#1835, #2092, #1508, #730) have nothing to build on.

## What this PR adds

**Two new Go modules**
- `internal/users`: a separate `users.db` (SQLite through
`modernc.org/sqlite`) with users, sessions, single-use tokens, an audit
log and a mail log. Passwords use argon2id.
- `internal/mailer`: a `Mailer` interface with a Brevo client (send,
delivery events, webhook parsing) and an in-memory fake for tests.

**Server (`cmd/server`)**, active only with `userManagement.enabled`
- 24 routes, all documented in OpenAPI under the `users` tag
([`auth_routes.go`](https://github.com/efiten/CoreScope/blob/feat/user-management/cmd/server/auth_routes.go)):
  - auth: register, activate, login, logout, me, forgot, reset;
- account: profile, password, email change with confirmation, sessions,
self-delete;
- admin: list, detail, disable, enable, delete, role, resend activation,
manual activation, mail status refresh;
  - a Brevo webhook, registered only when `mail.webhookSecret` is set.
- `requireAdmin` replaces `requireAPIKey` at the 7 operator call sites:
the API key **or** an admin session. With the feature off it is the old
API-key gate (`TestRequireAdminWithoutUserManagementIsAPIKeyGate`).
- `/api/config/client` gets `userManagement: {enabled: true}` only when
the service started; with the feature off the response is
byte-identical.

**Frontend**
- `auth.js` (header account control, request helper that adds the CSRF
header), `account.js` (login, register, activate, forgot, reset, confirm
email, my account), `admin-users.js` (`#/admin/users`, deep-linked
filters), `account.css` (theme tokens only).
- On phones the top-bar control is hidden, so a conditional entry goes
into the bottom-nav "More" sheet and the nav drawer.
- The customizer geofilter tab and the Perf "Reset stats" button use the
admin session when there is one.

**Config.** A `userManagement` block (`config.example.json`,
[`docs/user-guide/accounts.md`](https://github.com/efiten/CoreScope/blob/feat/user-management/docs/user-guide/accounts.md)).
The Brevo key can come from `CORESCOPE_BREVO_API_KEY`. The server
refuses to start when the block is enabled but incomplete.

## Security choices

- Session cookie `cs_session`: HttpOnly, SameSite=Lax, Secure when
`publicBaseUrl` is https. Every cookie-authenticated state change needs
the `X-CS-CSRF` header and a matching Origin.
- Activation needs the token **and** the account password. Without the
password, an attacker who keeps re-registering a known address could get
the owner to activate an account that carries the attacker's password.
- Register, forgot and email change answer identically for known and
unknown addresses. A password reset ends all sessions, a password change
ends all other sessions, and both end outstanding email-change links.
- Rate limits: login 10 per 15 minutes, register and forgot 5 per hour,
per IP and per address. The bucket count is capped. `trustedProxies`
makes the per-IP limits see real client IPs behind a proxy.
- Server logs carry `#<user id>`, never addresses, tokens or passwords;
mail-provider error texts are redacted before logging.

## Performance

No change to an existing hot path with the feature off. With it on:
- One `users.db` lookup per authenticated request (session by token
hash).
- The admin user table rebuilds its `tbody` on each filter change.
`users.List` caps the result at 1000 rows (`internal/users/users.go`),
which bounds the rebuild.
- `map[string]interface{}` in `openapi.go`: 79 before, 78 after.

## Verification

- `internal/users`, `internal/mailer` and `cmd/server`: `go vet` and `go
test -race` pass locally. 121 new Go tests.
- `cmd/server` with `-tags e2etest`: vet and the e2e hook tests pass.
- `sh test-all.sh` exits 0. `tests/unit/test-user-management-ui.js`: 67
passing (vm, real modules).
- `tests/e2e/test-user-management-e2e.js` (6 steps) passed locally
against an `e2etest` build with the fake mailer and against a
feature-off build. CI builds the `e2etest` binary and runs the suite on
a second server (`deploy.yml`).
- On a staging instance with a real Brevo key: register, activation mail
delivered, activate, admin table, "Refresh status" showing sent,
deferred, delivered, opened and clicked.

## Not in this PR

- Settings sync (#2130), the admin dashboard, approval flows and
notifications (parts B to E of #2128).
- A `requireReadAuth` mode (#1835). Sessions from this PR are what such
a mode would accept.
- Binary size and build time with `modernc.org/sqlite` linked next to
`mattn/go-sqlite3` were not measured. Their driver names do not collide.
#1992 discusses the driver choice.
- No Brevo webhook was configured on staging; delivery status there came
from "Refresh status".

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-07 09:25:29 +02:00

329 lines
10 KiB
Go

package main
import (
"errors"
"log"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
"github.com/meshcore-analyzer/mailer"
"github.com/meshcore-analyzer/users"
)
// adminStoreFail answers a failed store call: ErrNotFound is 404, anything
// else is logged (user id only) and answered 500.
func adminStoreFail(w http.ResponseWriter, op string, id int64, err error) {
if errors.Is(err, users.ErrNotFound) {
writeError(w, http.StatusNotFound, "user not found")
return
}
log.Printf("[users] admin %s for user #%d: %v", op, id, err)
writeError(w, http.StatusInternalServerError, "internal error")
}
// adminTarget loads {id} from the path; writes 400/404/500 and returns nil
// on failure.
func (s *Server) adminTarget(w http.ResponseWriter, r *http.Request) *users.User {
id, err := strconv.ParseInt(mux.Vars(r)["id"], 10, 64)
if err != nil {
writeError(w, http.StatusBadRequest, "invalid user id")
return nil
}
u, err := s.auth.st.GetByID(id)
if err != nil {
adminStoreFail(w, "load", id, err)
return nil
}
return u
}
// guardRemoval blocks disabling/deleting/demoting yourself (except a
// self-demotion, allowed via allowSelf), config admins, and the last active
// admin. Writes 409 (or 500 if the admin count fails) and returns false
// when blocked. Count-then-mutate is not atomic; two admins removing each
// other concurrently can still both pass.
func (s *Server) guardRemoval(w http.ResponseWriter, actor, target *users.User, allowSelf bool) bool {
if target.ID == actor.ID && !allowSelf {
writeError(w, http.StatusConflict, "use My account to change your own account")
return false
}
if target.Role == users.RoleAdmin && s.auth.isConfigAdmin(target.Email) {
writeError(w, http.StatusConflict, "this admin is listed in adminEmails; remove them from the config first")
return false
}
if target.Role == users.RoleAdmin && target.Status == users.StatusActive {
n, err := s.auth.st.CountActiveAdmins()
if err != nil {
log.Printf("[users] admin guard: count admins for user #%d: %v", target.ID, err)
writeError(w, http.StatusInternalServerError, "internal error")
return false
}
if n <= 1 {
writeError(w, http.StatusConflict, "this is the last admin; promote someone else first")
return false
}
}
return true
}
func (s *Server) writeAdminRow(w http.ResponseWriter, id int64) {
u, err := s.auth.st.GetByID(id)
if err != nil {
adminStoreFail(w, "reload", id, err)
return
}
last, err := s.auth.st.LatestMailByUser()
if err != nil {
log.Printf("[users] admin latest mail: %v", err)
}
writeJSON(w, s.auth.adminRow(*u, last))
}
func (s *Server) handleAdminUsers(w http.ResponseWriter, r *http.Request, _ *users.User, _ *users.Session) {
q := r.URL.Query()
f := users.ListFilter{Status: users.Status(q.Get("status")), Role: users.Role(q.Get("role")), Query: q.Get("q")}
switch f.Status {
case "", users.StatusPending, users.StatusActive, users.StatusDisabled:
default:
writeError(w, http.StatusBadRequest, "invalid status filter")
return
}
if f.Role != "" && !f.Role.Valid() {
writeError(w, http.StatusBadRequest, "invalid role filter")
return
}
list, err := s.auth.st.List(f)
if err != nil {
log.Printf("[users] admin list: %v", err)
writeError(w, http.StatusInternalServerError, "internal error")
return
}
last, err := s.auth.st.LatestMailByUser()
if err != nil {
log.Printf("[users] admin latest mail: %v", err)
}
out := make([]adminUserJSON, 0, len(list))
for _, u := range list {
out = append(out, s.auth.adminRow(u, last))
}
writeJSON(w, out)
}
func (s *Server) handleAdminUserDetail(w http.ResponseWriter, r *http.Request, _ *users.User, _ *users.Session) {
u := s.adminTarget(w, r)
if u == nil {
return
}
a := s.auth
last, err := a.st.LatestMailByUser()
if err != nil {
log.Printf("[users] admin latest mail: %v", err)
}
d := adminUserDetailJSON{User: a.adminRow(*u, last), Sessions: []sessionJSON{}, Mail: []mailJSON{}, Audit: []auditJSON{}}
list, err := a.st.ListSessions(u.ID)
if err != nil {
adminStoreFail(w, "list sessions", u.ID, err)
return
}
for _, x := range list {
d.Sessions = append(d.Sessions, sessionToJSON(x, 0))
}
mails, err := a.st.MailForUser(u.ID, 50)
if err != nil {
adminStoreFail(w, "list mail", u.ID, err)
return
}
for _, m := range mails {
d.Mail = append(d.Mail, mailToJSON(m))
}
entries, err := a.st.AuditFor(u.ID, 100)
if err != nil {
adminStoreFail(w, "list audit", u.ID, err)
return
}
for _, e := range entries {
d.Audit = append(d.Audit, auditJSON{ID: e.ID, At: rfc3339(e.At), ActorUserID: e.ActorUserID,
Action: e.Action, TargetUserID: e.TargetUserID, Detail: e.Detail})
}
writeJSON(w, d)
}
func (s *Server) handleAdminDisable(w http.ResponseWriter, r *http.Request, actor *users.User, _ *users.Session) {
t := s.adminTarget(w, r)
if t == nil || !s.guardRemoval(w, actor, t, false) {
return
}
if t.Status != users.StatusActive {
writeError(w, http.StatusConflict, "only active accounts can be disabled; delete or activate a pending account instead")
return
}
if err := s.auth.invalidateTokens("disable", t.ID, users.PurposeActivate, users.PurposeReset, users.PurposeEmailChange); err != nil {
writeError(w, http.StatusInternalServerError, "internal error")
return
}
if err := s.auth.st.SetStatus(t.ID, users.StatusDisabled); err != nil {
adminStoreFail(w, "disable", t.ID, err)
return
}
if err := s.auth.st.DeleteUserSessions(t.ID, 0); err != nil {
// currentUser rejects non-active users, so the sessions are dead
// anyway; keep going but leave a trace.
log.Printf("[users] disable: delete sessions for user #%d: %v", t.ID, err)
}
s.auth.audit(idPtr(actor.ID), "user.disable", idPtr(t.ID), nil)
s.writeAdminRow(w, t.ID)
}
func (s *Server) handleAdminEnable(w http.ResponseWriter, r *http.Request, actor *users.User, _ *users.Session) {
t := s.adminTarget(w, r)
if t == nil {
return
}
if t.Status != users.StatusDisabled {
writeError(w, http.StatusConflict, "only disabled accounts can be enabled")
return
}
if err := s.auth.st.SetStatus(t.ID, users.StatusActive); err != nil {
adminStoreFail(w, "enable", t.ID, err)
return
}
s.auth.audit(idPtr(actor.ID), "user.enable", idPtr(t.ID), nil)
s.writeAdminRow(w, t.ID)
}
func (s *Server) handleAdminDelete(w http.ResponseWriter, r *http.Request, actor *users.User, _ *users.Session) {
t := s.adminTarget(w, r)
if t == nil || !s.guardRemoval(w, actor, t, false) {
return
}
if err := s.auth.st.Delete(t.ID); err != nil {
adminStoreFail(w, "delete", t.ID, err)
return
}
s.auth.audit(idPtr(actor.ID), "user.delete", idPtr(t.ID), nil)
writeJSON(w, okResponse{OK: true})
}
func (s *Server) handleAdminRole(w http.ResponseWriter, r *http.Request, actor *users.User, _ *users.Session) {
t := s.adminTarget(w, r)
if t == nil {
return
}
var req roleRequest
if !decodeJSON(w, r, &req) {
return
}
if !req.Role.Valid() {
writeError(w, http.StatusBadRequest, "role must be user or admin")
return
}
// Activation sets the role from adminEmails, so a change now would be
// silently lost.
if t.Status == users.StatusPending {
writeError(w, http.StatusConflict, "activate the account first")
return
}
if req.Role == t.Role {
s.writeAdminRow(w, t.ID)
return
}
if req.Role == users.RoleUser && !s.guardRemoval(w, actor, t, true) {
return
}
if err := s.auth.st.SetRole(t.ID, req.Role); err != nil {
adminStoreFail(w, "set role", t.ID, err)
return
}
s.auth.audit(idPtr(actor.ID), "user.role", idPtr(t.ID), map[string]string{"role": string(req.Role)})
s.writeAdminRow(w, t.ID)
}
func (s *Server) handleAdminResendActivation(w http.ResponseWriter, r *http.Request, actor *users.User, _ *users.Session) {
a := s.auth
t := s.adminTarget(w, r)
if t == nil {
return
}
if t.Status != users.StatusPending {
writeError(w, http.StatusConflict, "only pending accounts need activation")
return
}
if err := a.mailToken(r.Context(), t, users.PurposeActivate, 48*time.Hour, "", "activate",
func(tok string) mailer.Message { return a.activationMail(t, tok) }); err != nil {
writeError(w, http.StatusServiceUnavailable, msgMailFailed)
return
}
a.audit(idPtr(actor.ID), "user.activation.resend", idPtr(t.ID), nil)
s.writeAdminRow(w, t.ID)
}
// handleAdminActivate activates a pending user without the mail link, for
// when mail keeps failing. The address stays unverified; activated_by and
// the audit row keep that visible.
func (s *Server) handleAdminActivate(w http.ResponseWriter, r *http.Request, actor *users.User, _ *users.Session) {
a := s.auth
t := s.adminTarget(w, r)
if t == nil {
return
}
if t.Status != users.StatusPending {
writeError(w, http.StatusConflict, "only pending accounts can be activated")
return
}
if err := a.st.InvalidateTokens(t.ID, users.PurposeActivate); err != nil {
log.Printf("[users] manual activate: invalidate tokens for user #%d: %v", t.ID, err)
writeError(w, http.StatusInternalServerError, "internal error")
return
}
if err := a.st.Activate(t.ID, a.roleFor(t.Email), idPtr(actor.ID)); err != nil {
adminStoreFail(w, "activate", t.ID, err)
return
}
a.audit(idPtr(actor.ID), "user.activate.manual", idPtr(t.ID), nil)
s.writeAdminRow(w, t.ID)
}
func (s *Server) handleAdminMailRefresh(w http.ResponseWriter, r *http.Request, actor *users.User, _ *users.Session) {
a := s.auth
t := s.adminTarget(w, r)
if t == nil {
return
}
mailID, err := strconv.ParseInt(mux.Vars(r)["mailId"], 10, 64)
if err != nil {
writeError(w, http.StatusBadRequest, "invalid mail id")
return
}
rec, err := a.st.MailByID(mailID)
if err != nil && !errors.Is(err, users.ErrNotFound) {
log.Printf("[users] admin mail refresh: load mail %d for user #%d: %v", mailID, t.ID, err)
writeError(w, http.StatusInternalServerError, "internal error")
return
}
if err != nil || rec.UserID == nil || *rec.UserID != t.ID {
writeError(w, http.StatusNotFound, "mail not found")
return
}
if rec.ProviderMessageID == "" {
writeError(w, http.StatusConflict, "this mail has no provider message id")
return
}
evs, err := a.mail.Events(r.Context(), rec.ProviderMessageID)
if err != nil {
log.Printf("[users] admin mail refresh: provider events for user #%d: %s", t.ID, redactAddrs(err))
writeError(w, http.StatusBadGateway, "mail provider unavailable")
return
}
a.ingestMailEvents(evs)
a.audit(idPtr(actor.ID), "user.mail.refresh", idPtr(t.ID), nil)
rec, err = a.st.MailByID(mailID)
if err != nil {
log.Printf("[users] admin mail refresh: reload mail %d for user #%d: %v", mailID, t.ID, err)
writeError(w, http.StatusInternalServerError, "internal error")
return
}
writeJSON(w, mailToJSON(*rec))
}