feat(webui): give the dashboard an actionable auth failure state
An unauthenticated document request answered with a plain-text 401 -- and index.html, the bundle and every asset sit behind requireToken -- so a bookmarked or bare URL was a dead end: the dashboard's own authentication banner could never load to explain it. Document requests now get a self-contained 401 page whose one-field form reuses the ?t= exchange, so a pasted token sets the cookie and the redirect lands on the dashboard. A rejected token re-renders the page with the reason instead of answering a document with text. API and event-stream callers keep the plain-text 401: classifyActionError maps it to "session-expired", and an EventSource error body is never seen. The auth banner gains a Retry that re-runs the reads which report their own 401s, so a recovered session clears it without a document reload. The retry is extracted to lib/session-retry.ts with an in-flight guard and unit tests.
This commit is contained in:
6 files changed
+300
-11
No files matched your search
@@ -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 = `<p class="error">` + reason + `</p>`
|
||||
}
|
||||
_, _ = io.WriteString(w, strings.Replace(authPageHTML, "<!--auth-error-->", 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 = `<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Archie dashboard</title>
|
||||
<style>
|
||||
:root { color-scheme: dark light; }
|
||||
body { margin: 0; min-height: 100vh; display: grid; place-items: center; font: 15px/1.5 system-ui, sans-serif; background: #0b0b10; color: #e8e8ee; }
|
||||
main { width: min(26rem, calc(100% - 2rem)); padding: 2rem; border: 1px solid #2a2a38; border-radius: 0.75rem; background: #14141c; }
|
||||
h1 { margin: 0 0 0.75rem; font-size: 1.25rem; }
|
||||
p { margin: 0 0 1rem; color: #b9b9c8; }
|
||||
label { display: block; margin-bottom: 0.5rem; font-weight: 600; }
|
||||
input { width: 100%; box-sizing: border-box; padding: 0.6rem 0.7rem; border: 1px solid #3a3a4c; border-radius: 0.5rem; background: #0b0b10; color: inherit; font: inherit; }
|
||||
button { margin-top: 1rem; width: 100%; padding: 0.6rem 0.7rem; border: 0; border-radius: 0.5rem; background: #7c5cff; color: #fff; font: inherit; font-weight: 600; cursor: pointer; }
|
||||
.error { padding: 0.6rem 0.7rem; border: 1px solid #7f2b3b; border-radius: 0.5rem; background: #2a1118; color: #ffb4c0; }
|
||||
.hint { margin: 1rem 0 0; font-size: 0.85rem; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<h1>Archie dashboard</h1>
|
||||
<p>This dashboard needs its access token before it will load.</p>
|
||||
<!--auth-error-->
|
||||
<form method="get" action="/">
|
||||
<label for="t">Access token</label>
|
||||
<input id="t" name="t" type="password" autocomplete="off" autofocus required>
|
||||
<button type="submit">Open the dashboard</button>
|
||||
</form>
|
||||
<p class="hint">Paste the token from the dashboard URL archied printed at startup, or the token your deployment issued.</p>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
|
||||
// sameOriginPath rebuilds a redirect target from a request path and raw
|
||||
// query, collapsing any leading "//" down to a single slash first.
|
||||
//
|
||||
|
||||
@@ -75,3 +75,96 @@ func TestRequireToken_RedirectStripsTokenFromCleanURL(t *testing.T) {
|
||||
t.Fatalf("Location = %q, want /dashboard?foo=bar", loc)
|
||||
}
|
||||
}
|
||||
|
||||
// An unauthenticated browser navigation must land on something a human can act
|
||||
// on. Everything -- index.html, the assets, every API -- sits behind
|
||||
// requireToken, so a raw text 401 left a bookmarked URL with no way forward and
|
||||
// no way to reach the dashboard's own authentication banner.
|
||||
func TestRequireToken_ServesAnAuthPageToDocumentRequests(t *testing.T) {
|
||||
s := &Server{Token: "secret-token"}
|
||||
h := s.requireToken(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
t.Error("an unauthenticated document request reached the protected handler")
|
||||
}))
|
||||
|
||||
req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "http://archie.local/", nil)
|
||||
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("status = %d, want 401; an auth page served as 200 is cached and reads as success to a status-only check", rec.Code)
|
||||
}
|
||||
if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/html") {
|
||||
t.Fatalf("Content-Type = %q, want text/html", ct)
|
||||
}
|
||||
if cc := rec.Header().Get("Cache-Control"); !strings.Contains(cc, "no-store") {
|
||||
t.Fatalf("Cache-Control = %q, want no-store", cc)
|
||||
}
|
||||
body := rec.Body.String()
|
||||
for _, want := range []string{"<form", `action="/"`, `name="t"`, "token"} {
|
||||
if !strings.Contains(body, want) {
|
||||
t.Fatalf("auth page is missing %q:\n%s", want, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A rejected token posts back through the same exchange, so the page has to
|
||||
// say so and offer another try rather than answering a document with text.
|
||||
func TestRequireToken_AuthPageReportsARejectedToken(t *testing.T) {
|
||||
s := &Server{Token: "secret-token"}
|
||||
h := s.requireToken(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
t.Error("a request with a bad token reached the protected handler")
|
||||
}))
|
||||
|
||||
req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "http://archie.local/?t=wrong", nil)
|
||||
req.Header.Set("Accept", "text/html")
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("status = %d, want 401", rec.Code)
|
||||
}
|
||||
if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/html") {
|
||||
t.Fatalf("Content-Type = %q, want text/html", ct)
|
||||
}
|
||||
if body := rec.Body.String(); !strings.Contains(body, "not accepted") {
|
||||
t.Fatalf("auth page does not report the rejected token:\n%s", body)
|
||||
}
|
||||
}
|
||||
|
||||
// API and event-stream callers read a status, not a page: classifyActionError
|
||||
// maps 401 to "session-expired", and an HTML body on an EventSource error would
|
||||
// never be seen. The document/API split is the contract this guards.
|
||||
func TestRequireToken_KeepsTextUnauthorisedForAPIRequests(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
accept string
|
||||
}{
|
||||
{name: "json api", accept: "application/json"},
|
||||
{name: "fetch default", accept: "*/*"},
|
||||
{name: "event stream", accept: "text/event-stream"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s := &Server{Token: "secret-token"}
|
||||
h := s.requireToken(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
t.Error("an unauthenticated API request reached the protected handler")
|
||||
}))
|
||||
|
||||
req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "http://archie.local/api/tasks", nil)
|
||||
req.Header.Set("Accept", tt.accept)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("status = %d, want 401", rec.Code)
|
||||
}
|
||||
if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/plain") {
|
||||
t.Fatalf("Content-Type = %q, want text/plain", ct)
|
||||
}
|
||||
if body := rec.Body.String(); !strings.Contains(body, "unauthorised") {
|
||||
t.Fatalf("body = %q, want the plain unauthorised text", body)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user