mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-10-14 02:37:27 +00:00
Part C of #2128: an admin area with an overview, the user table and a global audit log. Off by default like the rest of user management: with `userManagement` off nothing changes, and non-admins get no new UI. This is the first of 4 stacked PRs (C, D, E, then a small export/backup follow-up). Each later one contains this branch; review them in order. ## The situation - An admin could manage users one by one in `#/admin/users`, but nothing showed whether the instance needs attention: accounts stuck in activation, bouncing mail, someone guessing a password, an MQTT source that dropped. - The audit log existed (`internal/users/audit.go`) but could only be read per user, and logins were not recorded. ## What this PR adds **Store (`internal/users`)** - Schema v3: an index on `audit_log(at)`. - `AuditList` with filters (action or group prefix like `user.login.*`, user as actor or target, period) and keyset pagination; `PruneAudit`; `Stats` for the user figures. **Server** - `GET /api/admin/stats` (typed struct): accounts by status, admins, registrations and active users over 7 and 30 days, logins and failed logins in 24 hours, mail by final status, and the attention items computed server-side. - `GET /api/admin/audit`: filtered, newest first, `next` cursor. - `GET /api/admin/users` gains `bouncing=1`. - Logins are audited as `user.login` and `user.login.failed` (reason `wrong_password`, `pending` or `disabled`). An unknown address writes no row. The writes are asynchronous, so the login response does not wait on `users.db`. Login rows are pruned after 90 days. **Frontend** - `#/admin?tab=overview|users|audit`: `admin.js` (tab shell), `admin-overview.js` ("Needs attention", Users card, System card from the existing health, MQTT and observer endpoints), `admin-audit.js` (filters in the URL, "Load more"). `admin-users.js` becomes the Users tab; the old `#/admin/users` link rewrites to it. - `/api/healthz` is read on open and on Refresh only, not on the 60-second timer, because it walks every packet under a read lock. Spec: [`docs/specs/2026-10-07-admin-dashboard-design.md`](https://github.com/efiten/CoreScope/blob/feat/admin-dashboard/docs/specs/2026-10-07-admin-dashboard-design.md). ## Verification - `internal/users` and `cmd/server`: `go vet` and `go test` pass locally, 26 new Go tests. One upstream test, `TestSaveGeoFilterPreservesFileMode`, also fails on Windows on plain `master` (file modes) and is unrelated. - `sh test-all.sh` exits 0; the XSS gate in diff mode passes. - User-management E2E with the `e2etest` build: 13 of 13 steps locally. - Running on our staging and production instance since 7 October 2026. ## Not in this PR - Restricting existing pages (perf, MQTT status) to logged-in users or admins. The admin area adds no access mechanism of its own, so that stays a later router rule plus endpoint check. - Server-enforced customizer tab restrictions (#1508). - The 24-hour, 5-attempt and 10-minute attention thresholds are constants for now (AGENTS.md rule 8: customizer later). --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
303 lines
9.6 KiB
Go
303 lines
9.6 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/meshcore-analyzer/mailer"
|
|
"github.com/meshcore-analyzer/users"
|
|
)
|
|
|
|
const (
|
|
msgCheckMail = "If the address can receive mail, a message is on its way. Check your inbox."
|
|
msgBadLogin = "incorrect email or password"
|
|
msgMailFailed = "mail could not be sent, try again later"
|
|
)
|
|
|
|
func (s *Server) handleRegister(w http.ResponseWriter, r *http.Request) {
|
|
a := s.auth
|
|
var req registerRequest
|
|
if !decodeJSON(w, r, &req) {
|
|
return
|
|
}
|
|
email, err := users.NormalizeEmail(req.Email)
|
|
if writeValidation(w, err) {
|
|
return
|
|
}
|
|
name, err := users.ValidateDisplayName(req.DisplayName)
|
|
if writeValidation(w, err) {
|
|
return
|
|
}
|
|
if writeValidation(w, users.ValidatePassword(req.Password)) {
|
|
return
|
|
}
|
|
if !a.allow(w, r, a.signup, "email:"+email) {
|
|
return
|
|
}
|
|
// Hash before the lookup so new and existing addresses cost the same.
|
|
hash, err := users.HashPassword(req.Password)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal error")
|
|
return
|
|
}
|
|
if existing, err := a.st.GetByEmail(email); err == nil {
|
|
// Identical response either way (no account enumeration).
|
|
if existing.Status == users.StatusPending {
|
|
// Newest registration wins: the account takes the password and
|
|
// name of the latest request, and activation demands that
|
|
// password, so a squatter cannot plant credentials and the owner
|
|
// can recover a squatted address. Failures stay invisible to the
|
|
// client (same response as every other branch).
|
|
if err := a.st.SetPassword(existing.ID, hash); err != nil {
|
|
log.Printf("[users] register: update pending user #%d: %v", existing.ID, err)
|
|
} else if err := a.st.SetDisplayName(existing.ID, name); err != nil {
|
|
log.Printf("[users] register: update pending user #%d: %v", existing.ID, err)
|
|
} else {
|
|
existing.DisplayName = name
|
|
_ = a.mailToken(r.Context(), existing, users.PurposeActivate, 48*time.Hour, "", "activate",
|
|
func(tok string) mailer.Message { return a.activationMail(existing, tok) })
|
|
}
|
|
} else {
|
|
_ = a.sendMail(r.Context(), existing, "register-notice", a.registerNoticeMail(existing))
|
|
}
|
|
writeJSON(w, okResponse{OK: true, Message: msgCheckMail})
|
|
return
|
|
}
|
|
u, err := a.st.CreatePending(email, name, hash)
|
|
if errors.Is(err, users.ErrEmailTaken) { // lost a race with a concurrent register
|
|
writeJSON(w, okResponse{OK: true, Message: msgCheckMail})
|
|
return
|
|
}
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal error")
|
|
return
|
|
}
|
|
err = a.mailToken(r.Context(), u, users.PurposeActivate, 48*time.Hour, "", "activate",
|
|
func(tok string) mailer.Message { return a.activationMail(u, tok) })
|
|
if err != nil {
|
|
if derr := a.st.Delete(u.ID); derr != nil {
|
|
log.Printf("[users] rollback of user #%d failed: %v", u.ID, derr)
|
|
}
|
|
writeError(w, http.StatusServiceUnavailable, msgMailFailed)
|
|
return
|
|
}
|
|
a.audit(nil, "user.register", idPtr(u.ID), nil)
|
|
writeJSON(w, okResponse{OK: true, Message: msgCheckMail})
|
|
}
|
|
|
|
// handleActivate needs the token and the account password: a re-register
|
|
// of a pending address replaces the password, so whoever clicks must prove
|
|
// they know the newest one (a squatter's link cannot log the owner into an
|
|
// account carrying the squatter's password).
|
|
func (s *Server) handleActivate(w http.ResponseWriter, r *http.Request) {
|
|
a := s.auth
|
|
var req activateRequest
|
|
if !decodeJSON(w, r, &req) {
|
|
return
|
|
}
|
|
uid, err := a.st.TokenUser(req.Token, users.PurposeActivate)
|
|
if err != nil {
|
|
writeTokenError(w, err)
|
|
return
|
|
}
|
|
if !a.allow(w, r, a.login, "activate:#"+strconv.FormatInt(uid, 10)) {
|
|
return
|
|
}
|
|
u, err := a.st.GetByID(uid)
|
|
if err != nil && !errors.Is(err, users.ErrNotFound) {
|
|
log.Printf("[users] activate: load user #%d: %v", uid, err)
|
|
writeError(w, http.StatusInternalServerError, "internal error")
|
|
return
|
|
}
|
|
if err != nil || u.Status != users.StatusPending {
|
|
writeError(w, http.StatusGone, "this account is already activated, log in instead")
|
|
return
|
|
}
|
|
ok, err := users.VerifyPassword(u.PasswordHash, req.Password)
|
|
if err != nil {
|
|
log.Printf("[users] activate: verify password for user #%d: %v", u.ID, err)
|
|
writeError(w, http.StatusInternalServerError, "internal error")
|
|
return
|
|
}
|
|
if !ok {
|
|
writeError(w, http.StatusUnauthorized, "wrong password for this account")
|
|
return
|
|
}
|
|
// Activate only on the hash that was just verified: a re-register in
|
|
// between replaces it, and the newest password must be proven.
|
|
switch err := a.st.ActivateWithToken(req.Token, u.ID, a.roleFor(u.Email), u.PasswordHash); {
|
|
case err == nil:
|
|
case errors.Is(err, users.ErrAccountChanged):
|
|
writeError(w, http.StatusConflict, "account changed, try again")
|
|
return
|
|
case errors.Is(err, users.ErrTokenInvalid), errors.Is(err, users.ErrTokenExpired):
|
|
writeTokenError(w, err)
|
|
return
|
|
default:
|
|
log.Printf("[users] activate user #%d: %v", u.ID, err)
|
|
writeError(w, http.StatusInternalServerError, "internal error")
|
|
return
|
|
}
|
|
a.audit(nil, "user.activate", idPtr(u.ID), nil)
|
|
if u, err = a.st.GetByID(u.ID); err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal error")
|
|
return
|
|
}
|
|
a.startSession(w, r, u)
|
|
}
|
|
|
|
// startSession creates a session, sets the cookie and answers with /me.
|
|
// It reports whether the session was started.
|
|
func (a *authService) startSession(w http.ResponseWriter, r *http.Request, u *users.User) bool {
|
|
raw, sess, err := a.st.CreateSession(u.ID, a.set.sessionTTL, r.UserAgent())
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal error")
|
|
return false
|
|
}
|
|
_ = a.st.TouchLogin(u.ID)
|
|
a.setSessionCookie(w, raw, sess.ExpiresAt)
|
|
writeJSON(w, meFrom(u, sess))
|
|
return true
|
|
}
|
|
|
|
// loginFailReason is the audit reason of a refused login for an existing
|
|
// account. A wrong password wins over the account state.
|
|
func loginFailReason(passwordOK bool, st users.Status) string {
|
|
if !passwordOK {
|
|
return "wrong_password"
|
|
}
|
|
if st == users.StatusPending {
|
|
return "pending"
|
|
}
|
|
return "disabled"
|
|
}
|
|
|
|
func (s *Server) handleLogin(w http.ResponseWriter, r *http.Request) {
|
|
a := s.auth
|
|
var req loginRequest
|
|
if !decodeJSON(w, r, &req) {
|
|
return
|
|
}
|
|
email, emailErr := users.NormalizeEmail(req.Email)
|
|
key := "email:" + email
|
|
if emailErr != nil {
|
|
key = "email:invalid"
|
|
}
|
|
if !a.allow(w, r, a.login, key) {
|
|
return
|
|
}
|
|
var u *users.User
|
|
if emailErr == nil {
|
|
u, _ = a.st.GetByEmail(email)
|
|
}
|
|
if u == nil {
|
|
users.BurnPasswordCheck(req.Password)
|
|
writeError(w, http.StatusUnauthorized, msgBadLogin)
|
|
return
|
|
}
|
|
ok, err := users.VerifyPassword(u.PasswordHash, req.Password)
|
|
if err != nil || !ok || u.Status != users.StatusActive {
|
|
writeError(w, http.StatusUnauthorized, msgBadLogin)
|
|
// In the background after the answer is decided, so the write never
|
|
// changes response timing.
|
|
a.auditAsync(nil, "user.login.failed", idPtr(u.ID), map[string]string{"reason": loginFailReason(err == nil && ok, u.Status)})
|
|
return
|
|
}
|
|
// Config wins: an address in adminEmails is always admin.
|
|
if a.isConfigAdmin(u.Email) && u.Role != users.RoleAdmin {
|
|
if err := a.st.SetRole(u.ID, users.RoleAdmin); err == nil {
|
|
u.Role = users.RoleAdmin
|
|
a.audit(nil, "user.role.config", idPtr(u.ID), map[string]string{"role": "admin"})
|
|
}
|
|
}
|
|
if a.startSession(w, r, u) {
|
|
a.auditAsync(nil, "user.login", idPtr(u.ID), nil)
|
|
}
|
|
}
|
|
|
|
func (s *Server) handleLogout(w http.ResponseWriter, r *http.Request) {
|
|
if c, err := r.Cookie(sessionCookieName); err == nil && c.Value != "" {
|
|
_ = s.auth.st.DeleteSessionByToken(c.Value)
|
|
}
|
|
s.auth.clearSessionCookie(w)
|
|
writeJSON(w, okResponse{OK: true})
|
|
}
|
|
|
|
func (s *Server) handleMe(w http.ResponseWriter, r *http.Request) {
|
|
u, sess := s.auth.currentUser(w, r)
|
|
if u == nil {
|
|
writeError(w, http.StatusUnauthorized, "not logged in")
|
|
return
|
|
}
|
|
writeJSON(w, meFrom(u, sess))
|
|
}
|
|
|
|
func (s *Server) handleForgot(w http.ResponseWriter, r *http.Request) {
|
|
a := s.auth
|
|
var req emailRequest
|
|
if !decodeJSON(w, r, &req) {
|
|
return
|
|
}
|
|
email, err := users.NormalizeEmail(req.Email)
|
|
if err != nil {
|
|
writeJSON(w, okResponse{OK: true, Message: msgCheckMail})
|
|
return
|
|
}
|
|
if !a.allow(w, r, a.signup, "email:"+email) {
|
|
return
|
|
}
|
|
u, err := a.st.GetByEmail(email)
|
|
if err != nil || u.Status != users.StatusActive {
|
|
writeJSON(w, okResponse{OK: true, Message: msgCheckMail})
|
|
return
|
|
}
|
|
err = a.mailToken(r.Context(), u, users.PurposeReset, time.Hour, "", "reset",
|
|
func(tok string) mailer.Message { return a.resetMail(u, tok) })
|
|
if err != nil {
|
|
writeError(w, http.StatusServiceUnavailable, msgMailFailed)
|
|
return
|
|
}
|
|
writeJSON(w, okResponse{OK: true, Message: msgCheckMail})
|
|
}
|
|
|
|
func (s *Server) handleReset(w http.ResponseWriter, r *http.Request) {
|
|
a := s.auth
|
|
var req resetRequest
|
|
if !decodeJSON(w, r, &req) {
|
|
return
|
|
}
|
|
if writeValidation(w, users.ValidatePassword(req.Password)) {
|
|
return
|
|
}
|
|
uid, _, err := a.st.ConsumeToken(req.Token, users.PurposeReset)
|
|
if err != nil {
|
|
writeTokenError(w, err)
|
|
return
|
|
}
|
|
hash, err := users.HashPassword(req.Password)
|
|
if err == nil {
|
|
err = a.st.SetPassword(uid, hash)
|
|
}
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal error")
|
|
return
|
|
}
|
|
// Sessions first: a token-store failure below must not leave an
|
|
// attacker's session alive.
|
|
if err := a.st.DeleteUserSessions(uid, 0); err != nil {
|
|
log.Printf("[users] reset: end sessions for user #%d: %v", uid, err)
|
|
writeError(w, http.StatusInternalServerError, "internal error")
|
|
return
|
|
}
|
|
// A pending email change must not survive the owner's recovery.
|
|
if err := a.invalidateTokens("reset", uid, users.PurposeEmailChange); err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal error")
|
|
return
|
|
}
|
|
a.audit(idPtr(uid), "user.password.reset", idPtr(uid), nil)
|
|
writeJSON(w, okResponse{OK: true, Message: "Password changed. Log in with your new password."})
|
|
}
|