// middleware.go — requireAuth / requireAdmin (paridad auth.js:258-278). package auth import ( "context" "encoding/json" "net/http" "net/url" "strings" "github.com/gnacho/netpulse/server-go/internal/db" ) type ctxKey int const ( ctxUser ctxKey = iota ctxSessionID ctxAPIToken ) // UserFromContext devuelve el usuario autenticado (nil si no hay). func UserFromContext(ctx context.Context) *User { u, _ := ctx.Value(ctxUser).(*User) return u } // SessionIDFromContext devuelve el sessionId autenticado ("" si no hay). func SessionIDFromContext(ctx context.Context) string { s, _ := ctx.Value(ctxSessionID).(string) return s } // TokenValidator valida un bearer token y devuelve el usuario y scope. // Devuelve nil si el token no es válido. Implementado por apitoken.Store. type TokenValidator interface { ValidateBearer(raw string) (*User, string) } // Scopes de los API tokens. Un token read solo puede usar métodos seguros, // write puede mutar rutas no admin y admin alcanza también RequireAdmin. const ( ScopeRead = "read" ScopeWrite = "write" ScopeAdmin = "admin" ) // APITokenFromContext devuelve el scope del token API usado ("" si no es token). func APITokenFromContext(ctx context.Context) string { s, _ := ctx.Value(ctxAPIToken).(string) return s } // WriteError escribe el envelope de error {error, message?} con el status dado. func WriteError(w http.ResponseWriter, status int, code string, message ...string) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) body := map[string]any{"error": code} if len(message) > 0 { body["message"] = message[0] } // json.Marshal (no Encoder): el cuerpo debe ser literal como Hono, SIN // el '\n' final que añade Encode. data, _ := json.Marshal(body) _, _ = w.Write(data) } // isAgentReportPath: /api/agents/{slug}/binary|stream|apply-result|upgrade-result|upgrade-progress|firmware-progress|firmware-result|firmware-backup. func isAgentReportPath(path string) bool { return strings.HasPrefix(path, "/api/agents/") && (strings.HasSuffix(path, "/binary") || strings.HasSuffix(path, "/stream") || strings.HasSuffix(path, "/apply-result") || strings.HasSuffix(path, "/upgrade-result") || strings.HasSuffix(path, "/upgrade-progress") || strings.HasSuffix(path, "/firmware-progress") || strings.HasSuffix(path, "/firmware-result") || strings.HasSuffix(path, "/firmware-backup")) } // IsAgentRequest reports whether r is a route agents and embedders call with // their own token - pushes, reports, pairing, config backups - rather than a // person with a session or an API token. FORK: while HTTPS is being rolled // out these keep working over plain HTTP until each agent has moved, when // everything else is already refused there. func IsAgentRequest(r *http.Request) bool { p := r.URL.Path return p == "/api/ingest/agent" || p == "/api/agents/pair" || p == "/api/agents/pair/hello" || p == "/api/agents/executor-token" || p == "/api/agents/install-agent.sh" || isAgentReportPath(p) || (p == "/api/config-backup" && r.Method == http.MethodPost) } // isAnonymousPath indica si la ruta no requiere sesión (auth propia en el handler). func isAnonymousPath(path string) bool { if path == "/api/health" || path == "/api/auth/login" || path == "/api/ingest/agent" || path == "/api/agents/pair" || path == "/api/agents/pair/hello" || // FORK: proves the served key; no secret in or out path == "/api/agents/executor-token" || // #1751: instalador servido por el servidor; lo baja curl desde el // router antes de que exista nada que autenticar (el token viaja en // los argumentos del script, no en la descarga). path == "/api/agents/install-agent.sh" || isAgentReportPath(path) { return true } return false } // RequireAuth es middleware global: todo /api/* exige sesión salvo // /api/health, /api/auth/login, /api/ingest/agent (auth Bearer por token // de equipo), /api/agents/pair (pairing token de un solo uso, Fase 9 R3), // /api/agents/{slug}/binary (auth Bearer por token de agente, // Fase 6.2), /api/agents/{slug}/stream (SSE bidireccional, Fase 7.3), // /api/agents/{slug}/apply-result, /api/agents/{slug}/upgrade-result, // /api/agents/{slug}/upgrade-progress, /api/agents/{slug}/firmware-progress y // /api/agents/{slug}/firmware-result y /api/agents/{slug}/firmware-backup // (reportes del agente, auth Bearer), y /api/agents/install-agent.sh (#1751: // el instalador que baja curl en el router, sin secretos en el cuerpo). // POST /api/config-backup (issue #340): recibe snapshots UCI desde NetGrip; // auth Bearer por token de agente validado en el handler. // Acepta Bearer tokens de API (#330) como alternativa a la cookie de sesión. // Fallo → 401 {error:'unauthorized'}. func RequireAuth(d *db.DB, secret string, tv TokenValidator, next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { path := r.URL.Path if !strings.HasPrefix(path, "/api/") || isAnonymousPath(path) || (path == "/api/config-backup" && r.Method == http.MethodPost) { next.ServeHTTP(w, r) return } if tv != nil { if raw := bearerToken(r); raw != "" { if user, scope := tv.ValidateBearer(raw); user != nil { if !scopeAllowsMethod(scope, r.Method) { WriteError(w, http.StatusForbidden, "insufficient_scope") return } ctx := context.WithValue(r.Context(), ctxUser, user) ctx = context.WithValue(ctx, ctxAPIToken, scope) next.ServeHTTP(w, r.WithContext(ctx)) return } } } sess := SessionUserFromRequest(d, secret, r) if sess == nil { WriteError(w, http.StatusUnauthorized, "unauthorized") return } ctx := context.WithValue(r.Context(), ctxUser, sess.User) ctx = context.WithValue(ctx, ctxSessionID, sess.SessionID) next.ServeHTTP(w, r.WithContext(ctx)) }) } func bearerToken(r *http.Request) string { v := r.Header.Get("Authorization") if v == "" { return "" } if !strings.HasPrefix(v, "Bearer ") { return "" } return strings.TrimPrefix(v, "Bearer ") } // scopeAllowsMethod aplica el contrato de los scopes al método HTTP. Un token // read es de solo lectura: GET, HEAD y OPTIONS. write y admin pueden mutar, // pero solo admin supera además RequireAdmin. func scopeAllowsMethod(scope, method string) bool { switch scope { case ScopeWrite, ScopeAdmin: return true case ScopeRead: switch method { case http.MethodGet, http.MethodHead, http.MethodOptions: return true } } return false } // RequireAdmin exige role === 'admin' (tras RequireAuth) → 403 {error:'forbidden'}. // Con un API token exige además scope admin: un token read o write emitido por // un admin no hereda el rol y no entra en rutas de administración. func RequireAdmin(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { user := UserFromContext(r.Context()) if user == nil || user.Role != "admin" { WriteError(w, http.StatusForbidden, "forbidden") return } if scope := APITokenFromContext(r.Context()); scope != "" && scope != ScopeAdmin { WriteError(w, http.StatusForbidden, "insufficient_scope") return } next.ServeHTTP(w, r) }) } // RequireSameOrigin es la defensa CSRF (issue #213). SameSite=Lax cubre el // envío de cookies cross-site de fetch/XHR, pero no hay defensa en // profundidad si la app se sirve desde otro origen o un navegador sin // SameSite. En mutaciones (POST/PUT/DELETE) se valida el header Origin contra // el host efectivo de la petición (X-Forwarded-Host si TRUST_PROXY, si no // r.Host). Un Origin ausente (cliente no-navegador: agente, curl, CLI) se // deja pasar; si el navegador envía Sec-Fetch-Site: cross-site sin Origin, se // rechaza igualmente. Un Origin que no casa → 403 {error:'cross_origin'}. func RequireSameOrigin(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost || r.Method == http.MethodPut || r.Method == http.MethodDelete { if !originAllowed(r) { WriteError(w, http.StatusForbidden, "cross_origin") return } } next.ServeHTTP(w, r) }) } // originAllowed valida el header Origin (si viene) contra el host efectivo. func originAllowed(r *http.Request) bool { origin := r.Header.Get("Origin") if origin == "" { if sf := r.Header.Get("Sec-Fetch-Site"); sf != "" { return sf != "cross-site" } return true } u, err := url.Parse(origin) if err != nil { return false } // Origin "null" (iframe sandbox / redirect) no tiene host → no casa. if u.Host == "" { return false } return strings.EqualFold(u.Host, effectiveHost(r)) } // effectiveHost devuelve el host al que el cliente cree estar llamando: // X-Forwarded-Host (si TRUST_PROXY) o r.Host. Sin TRUST_PROXY, un header // falseado no puede eludir la comprobación. func effectiveHost(r *http.Request) string { if trustProxy { if xfh := r.Header.Get("X-Forwarded-Host"); xfh != "" { if first := strings.TrimSpace(strings.Split(xfh, ",")[0]); first != "" { return first } } } return r.Host }