Archie dashboard
+This dashboard needs its access token before it will load.
+ + +Paste the token from the dashboard URL archied printed at startup, or the token your deployment issued.
+diff --git a/internal/webui/auth.go b/internal/webui/auth.go index ab738a29a..63b896155 100644 --- a/internal/webui/auth.go +++ b/internal/webui/auth.go @@ -5,6 +5,7 @@ import ( "crypto/subtle" "encoding/base64" "fmt" + "io" "net" "net/http" "net/url" @@ -104,6 +105,11 @@ func (s *Server) requireToken(h http.Handler) http.Handler { } if tok := r.URL.Query().Get("t"); tok != "" { if !tokenEqual(tok, s.Token) { + if wantsDocument(r) { + s.authPage(w, "That token was not accepted.") + return + } + w.Header().Set("Cache-Control", "no-store") http.Error(w, "unauthorised", http.StatusUnauthorized) return } @@ -133,11 +139,82 @@ func (s *Server) requireToken(h http.Handler) http.Handler { return } + if wantsDocument(r) { + s.authPage(w, "") + return + } w.Header().Set("Content-Type", "text/plain; charset=utf-8") + w.Header().Set("Cache-Control", "no-store") http.Error(w, "unauthorised: open the dashboard URL archied logged at startup", http.StatusUnauthorized) }) } +// wantsDocument reports whether a request is a browser navigation rather than an +// API or stream call. A navigation sends text/html in Accept; fetch() sends */* +// or a JSON type, and EventSource sends text/event-stream. +func wantsDocument(r *http.Request) bool { + return strings.Contains(r.Header.Get("Accept"), "text/html") +} + +// authPage answers an unauthenticated or rejected browser navigation with a page +// a human can act on: the access token is pasted once, the existing ?t= +// exchange sets the cookie, and the redirect lands on the dashboard. +// +// The page is deliberately self-contained. index.html, the bundle and every +// other asset sit behind requireToken too, so a page that referenced anything +// would render unstyled for exactly the visitor who needs it. +// +// The status stays 401: a login page served as 200 is cached, and reads as +// success to anything that only checks the status. +func (s *Server) authPage(w http.ResponseWriter, reason string) { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.Header().Set("Cache-Control", "no-store") + w.Header().Set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'") + w.WriteHeader(http.StatusUnauthorized) + notice := "" + if reason != "" { + notice = `
` + reason + `
` + } + _, _ = io.WriteString(w, strings.Replace(authPageHTML, "", notice, 1)) +} + +// authPageHTML is the unauthenticated document response. The form reuses the +// ?t= exchange, so its action is the same path a startup URL uses. +const authPageHTML = ` + + + + +This dashboard needs its access token before it will load.
+ + +Paste the token from the dashboard URL archied printed at startup, or the token your deployment issued.
+