The Profile tab was a read-only avatar/name/email stub. It is now a real
form, modeled on the profile surfaces of GitHub/Slack/Nextcloud and scoped
to what a knowledge tool uses: display name, bio (280 chars with counter),
location, occupation, organization and website; email stays read-only
(sign-in identity). Copy explains where the name appears (shares) and
that public bookmark links never reveal identity.
Schema: migration 024_user_profile.sql + schema.sql add five nullable
text columns to User (db-converge green, fresh and upgraded paths).
models.User, userFromObject and the auth identity projection carry them,
so GET /api/me serves the stored truth (Go re-reads identity per request).
PATCH /api/me applies partial allowlisted edits: empty string clears to
NULL, rune-based caps (120/280/120/120/120), website <=2048 and http(s)
with a host; the answer is the fresh identity projection. User has no
version column (identity, not a collaborative collection), so this follows
the settings-save shape rather than the 409 contract; view-as writes stay
blocked by the middleware.
Session propagation: the auth.ts jwt callback gains an "update" trigger
that re-syncs name/email/image from Go via the same assertion the data
proxy signs (fail-quiet when Go is unreachable). The component calls
useSession().update({}) after a save — the {} argument is load-bearing:
in next-auth v5 a bare update() only re-fetches the session, it does not
fire the trigger (caught by the new browser regression). The top bar name
refreshes without a re-login.
Verified: full Go integration suite green incl. new TestProfileUpdate
(partial edits, clearing, rune caps, URL validation, auth gate);
db-converge green; web lint/SSR-lint/build 0 errors; full browser
regressions pass incl. the new profile round-trip (save → toast → top-bar
refresh → form values intact → Save re-disables on a clean baseline).
167 lines
6.3 KiB
Go
167 lines
6.3 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/andrew/superbrain/internal/config"
|
|
"github.com/andrew/superbrain/internal/db"
|
|
"github.com/andrew/superbrain/internal/models"
|
|
"log"
|
|
"net/http"
|
|
"sync"
|
|
)
|
|
|
|
const ViewAsCookie = "superbrain_view_as"
|
|
|
|
type SessionUser struct {
|
|
ID, Role, RealID, RealRole string
|
|
Name, Email *string
|
|
RealUser map[string]any
|
|
// Ver is the tokenVersion embedded in the credential (0 = no claim:
|
|
// pre-revocation legacy cookies only — assertions must carry it), checked
|
|
// against the user row so signout revokes cookie sessions AND NextAuth
|
|
// assertions alike (review SEC-02). API-key sessions always carry 0:
|
|
// keys are deliberately independent of tokenVersion (explicit revocation
|
|
// only — see apikeys.go).
|
|
Ver int
|
|
// APIKeyID is set when the request authenticated with a personal API
|
|
// key ("sb_..." Bearer) instead of a browser session. Machine requests
|
|
// never impersonate (view-as is cookie-driven and stays browser-only).
|
|
APIKeyID string
|
|
}
|
|
|
|
func identity(u *models.User) map[string]any {
|
|
return map[string]any{"id": u.ID, "name": u.Name, "email": u.Email, "image": u.Image, "role": u.Role, "tenantId": u.TenantID,
|
|
"bio": u.Bio, "location": u.Location, "occupation": u.Occupation, "organization": u.Organization, "website": u.Website}
|
|
}
|
|
func AuthMiddleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// Bearer channel fork: an "sb_..." API key resolves against the
|
|
// database (hash lookup + plan gate); anything else must be a
|
|
// short-lived NextAuth assertion (ReadSession). A DB failure on the
|
|
// key path is 503, never 401 (review CORR-03 discipline).
|
|
var s *SessionUser
|
|
var e error
|
|
if tok, ok := APIKeyFromRequest(r); ok {
|
|
s, e = ResolveAPIKey(r.Context(), db.FromContext(r.Context()), tok)
|
|
if e != nil {
|
|
switch {
|
|
case errors.Is(e, ErrAPIKeyPlan):
|
|
failure(w, 403, "Not available on your plan")
|
|
case errors.Is(e, ErrUnauthorized):
|
|
failure(w, 401, "Unauthorized")
|
|
default:
|
|
log.Printf("auth: api key lookup failed: %v", e)
|
|
failure(w, 503, "Service unavailable")
|
|
}
|
|
return
|
|
}
|
|
} else if s, e = ReadSession(r, config.Load().Secret()); e != nil {
|
|
failure(w, 401, "Unauthorized")
|
|
return
|
|
}
|
|
repo := db.NewUserRepo(db.FromContext(r.Context()))
|
|
u, e := repo.FindByID(r.Context(), s.ID)
|
|
if e != nil {
|
|
// Review CORR-03: a missing user is a credential problem (401),
|
|
// but backend degradation must not masquerade as one — surface it
|
|
// as 503 so client retry logic and monitoring see the difference.
|
|
if errors.Is(e, db.ErrNotFound) {
|
|
failure(w, 401, "Unauthorized")
|
|
} else {
|
|
log.Printf("auth: user lookup failed for session %s: %v", s.ID, e)
|
|
failure(w, 503, "Service unavailable")
|
|
}
|
|
return
|
|
}
|
|
// Revoked session: the credential's tokenVersion predates a signout
|
|
// bump. Applies to cookie sessions and NextAuth assertions alike.
|
|
if s.Ver > 0 && s.Ver != u.TokenVersion {
|
|
failure(w, 401, "Unauthorized")
|
|
return
|
|
}
|
|
// Promotion via ADMIN_EMAILS is persistent by design: removing an
|
|
// address from the env var later does NOT demote the account (use the
|
|
// Admin screen). Silent demotion on env drift would be worse than an
|
|
// extra explicit revocation step.
|
|
//
|
|
// Code review SEC-04 flagged that this re-promotes an account demoted
|
|
// via the Admin screen while its address remains in ADMIN_EMAILS. The
|
|
// env-driven promotion is a deliberate architectural decision here —
|
|
// demoting an operator account means editing ADMIN_EMAILS too; do not
|
|
// "fix" this silently.
|
|
if u.Role != "ADMIN" && u.Email != nil && u.EmailVerified != nil && config.IsAdminEmail(*u.Email) {
|
|
if e = repo.SetRole(r.Context(), u.ID, "ADMIN"); e != nil {
|
|
failure(w, 500, "Unable to resolve role")
|
|
return
|
|
}
|
|
u.Role = "ADMIN"
|
|
}
|
|
s.RealID, s.RealRole, s.RealUser = u.ID, u.Role, identity(u)
|
|
s.ID, s.Role, s.Name, s.Email = u.ID, u.Role, u.Name, u.Email
|
|
// View-as impersonation is a browser affordance; a machine credential
|
|
// always acts as its owner (a cookie on an API-key request is not a
|
|
// trusted signal — the key IS the identity).
|
|
if u.Role == "ADMIN" && s.APIKeyID == "" {
|
|
if c, e := r.Cookie(ViewAsCookie); e == nil && c.Value != "" && c.Value != u.ID {
|
|
if target, e := repo.FindByID(r.Context(), c.Value); e == nil {
|
|
s.ID, s.Role, s.Name, s.Email = target.ID, target.Role, target.Name, target.Email
|
|
// Review SEC-6: impersonation is audited. Log at most once
|
|
// per admin/target/method combination per process so a
|
|
// polling UI does not flood the log.
|
|
auditViewAs(u.ID, target.ID, r.Method)
|
|
}
|
|
}
|
|
}
|
|
// Review SEC-6: view-as is read-only. Mutations while impersonating
|
|
// are attributed to the target user, so writes must come from the
|
|
// admin's own identity — exit view-as first. The view-as endpoint
|
|
// itself is exempt: it controls the impersonation session and must
|
|
// be writable to both start and stop the session.
|
|
if s.ID != s.RealID && r.Method != http.MethodGet && r.Method != http.MethodHead && r.URL.Path != "/api/admin/view-as" {
|
|
failure(w, 403, "Read-only while viewing as another user")
|
|
return
|
|
}
|
|
next.ServeHTTP(w, r.WithContext(WithSession(r.Context(), s)))
|
|
})
|
|
}
|
|
|
|
type sessionKey struct{}
|
|
|
|
// auditViewAs logs each distinct (admin, target, method) impersonation
|
|
// observation once per process (review SEC-6). Ids only — never names,
|
|
// emails, or request payloads.
|
|
var (
|
|
auditViewAsMu sync.Mutex
|
|
auditViewAsLog = map[string]bool{}
|
|
)
|
|
|
|
func auditViewAs(adminID, targetID, method string) {
|
|
key := adminID + "|" + targetID + "|" + method
|
|
auditViewAsMu.Lock()
|
|
defer auditViewAsMu.Unlock()
|
|
if auditViewAsLog[key] {
|
|
return
|
|
}
|
|
auditViewAsLog[key] = true
|
|
log.Printf("audit: admin %s viewing as %s (%s)", adminID, targetID, method)
|
|
}
|
|
|
|
func WithSession(ctx context.Context, s *SessionUser) context.Context {
|
|
return context.WithValue(ctx, sessionKey{}, s)
|
|
}
|
|
func SessionFromContext(ctx context.Context) (*SessionUser, bool) {
|
|
s, ok := ctx.Value(sessionKey{}).(*SessionUser)
|
|
return s, ok
|
|
}
|
|
func RequireAdmin(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
s, ok := SessionFromContext(r.Context())
|
|
if !ok || s.RealRole != "ADMIN" {
|
|
failure(w, 403, "Forbidden")
|
|
return
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|