package server import ( "encoding/json" "net/http" "strconv" "time" "git.a-wan.net/alan/pipelines/internal/protocol" ) // requireElevationHTTP guards an HTTP route that changes the operator's files. // The upload travels outside the websocket, so the elevated connection's token // (protocol.ElevationHeader) authorises it; it is valid only while that // connection stays open and elevated. func (g *elevationGate) requireElevationHTTP(next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if g.tokenValid(r.Header.Get(protocol.ElevationHeader)) || !g.enabled() { next(w, r) return } w.Header().Set("Content-Type", "application/json") w.Header().Set("Cache-Control", "no-store") w.WriteHeader(http.StatusForbidden) _ = json.NewEncoder(w).Encode(map[string]string{ "code": protocol.ErrorElevationRequired, "error": "Confirm your password to upload files to this machine.", }) } } // ElevationTTLFromEnvironment reads PIPELINES_ELEVATION_MINUTES (1 to 720); // anything else keeps DefaultElevationTTL. func ElevationTTLFromEnvironment(getenv func(string) string) time.Duration { minutes, err := strconv.Atoi(getenv("PIPELINES_ELEVATION_MINUTES")) if err != nil || minutes < 1 || minutes > 720 { return DefaultElevationTTL } return time.Duration(minutes) * time.Minute }