Files
archie/internal/webui/auth.go
T
sam 4d44ef76f5 feat(webui): rebuild the dashboard
The old UI was a single 200-line Go file serving one hand-written HTML
page of task rows. It showed that archied was running and almost nothing
about what it was doing -- skills, workflows, memory, channels, logs and
configuration were all invisible unless you read the source or the
config file.

The dashboard now covers each of those as its own section, built as a
Vite app under ui/ at the repo root: it is a separate artefact from the
Go daemon, not an internal package of it. dist/ is committed so building
archied never needs a Node toolchain, and the !no_ui build tag keeps an
embed-free build available.

Both sides are split by concern rather than by layer -- one Go file per
API area, one folder per UI section with its own CSS -- so a change to
logs cannot disturb tasks.

Pages report consequences, not just state: an empty memory page says
Archie starts every conversation with no recollection of earlier ones,
and the setup checklist names what is missing in plain language instead
of showing a grid of zeroes. Nothing greets by a hardcoded name; the
operator comes from [chat], which is deployment data.

Auth is exposure control, not capability control -- the agent already
holds this user's authority through its own tools. So a loopback bind
needs no token, and only a reachable bind mints one and logs a URL,
which means an instance cannot be exposed merely by omitting config.

Container startup now degrades instead of aborting, for the reason
resolveForge already does: a missing agent image should not deny the
operator chat and the dashboard, and under Restart=on-failure it turns a
recoverable problem into a crash loop with the real error scrolling past.

`task dev` runs the daemon under air and the Vite server together in one
terminal.
2026-08-04 01:45:36 +10:00

143 lines
4.4 KiB
Go

package webui
import (
"crypto/rand"
"crypto/subtle"
"encoding/base64"
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"strings"
)
// tokenCookie carries the dashboard token once it has been exchanged from the
// URL, so the token is not left sitting in browser history or referrers.
const tokenCookie = "archie_ui"
// IsLoopback reports whether a listen address is reachable only from this
// machine.
//
// Loopback binds need no token. Archie's agent already runs shell, write and
// edit tools on the host, so anyone with local access has the capability
// regardless; a lock here would only obstruct the operator. What a token
// protects is *exposure* -- an instance reachable from a network -- so that is
// the only case where one is required.
func IsLoopback(listen string) bool {
host, _, err := net.SplitHostPort(strings.TrimSpace(listen))
if err != nil {
host = strings.TrimSpace(listen)
}
switch host {
case "", "localhost":
return true
}
ip := net.ParseIP(host)
return ip != nil && ip.IsLoopback()
}
// DashboardURL renders a listen address as a URL a human can actually open.
//
// A wildcard bind ("0.0.0.0:8484", ":8484", "[::]:8484") is a valid thing to
// listen on but not a valid thing to visit, and printing it verbatim gives the
// operator a link that goes nowhere. The host is substituted for localhost,
// which is correct for the machine reading the log.
func DashboardURL(listen, token string) string {
host, port, err := net.SplitHostPort(strings.TrimSpace(listen))
if err != nil {
host, port = "", strings.TrimPrefix(strings.TrimSpace(listen), ":")
}
switch host {
case "", "0.0.0.0", "::", "[::]":
host = "localhost"
}
url := "http://" + net.JoinHostPort(host, port) + "/"
if token != "" {
url += "?t=" + token
}
return url
}
// LoadOrCreateToken returns the dashboard token, generating and persisting one
// on first use. The token is created rather than configured so a non-loopback
// bind cannot be brought up unprotected by omission: there is no setup step to
// forget.
func LoadOrCreateToken(path string) (string, error) {
if b, err := os.ReadFile(path); err == nil {
if tok := strings.TrimSpace(string(b)); tok != "" {
return tok, nil
}
} else if !os.IsNotExist(err) {
return "", fmt.Errorf("webui: read token: %w", err)
}
raw := make([]byte, 32)
if _, err := rand.Read(raw); err != nil {
return "", fmt.Errorf("webui: generate token: %w", err)
}
tok := base64.RawURLEncoding.EncodeToString(raw)
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return "", fmt.Errorf("webui: token dir: %w", err)
}
if err := os.WriteFile(path, []byte(tok+"\n"), 0o600); err != nil {
return "", fmt.Errorf("webui: write token: %w", err)
}
return tok, nil
}
// requireToken wraps h with token authentication. A zero token disables the
// check entirely, which is how loopback binds stay frictionless.
//
// The token may arrive as ?t=... once; it is then moved into a
// SameSite=Strict, HttpOnly cookie and the caller redirected to the clean URL.
// One click on the URL archied logs is the whole setup.
// The token is read per request rather than captured when the handler is
// built, so a Server whose Token is set after Handler() is still protected.
func (s *Server) requireToken(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if s.Token == "" {
h.ServeHTTP(w, r)
return
}
if tok := r.URL.Query().Get("t"); tok != "" {
if !tokenEqual(tok, s.Token) {
http.Error(w, "unauthorised", http.StatusUnauthorized)
return
}
http.SetCookie(w, &http.Cookie{
Name: tokenCookie,
Value: tok,
Path: "/",
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
Secure: r.TLS != nil,
})
clean := *r.URL
q := clean.Query()
q.Del("t")
clean.RawQuery = q.Encode()
http.Redirect(w, r, clean.RequestURI(), http.StatusSeeOther)
return
}
if c, err := r.Cookie(tokenCookie); err == nil && tokenEqual(c.Value, s.Token) {
h.ServeHTTP(w, r)
return
}
if hdr := r.Header.Get("Authorization"); strings.HasPrefix(hdr, "Bearer ") &&
tokenEqual(strings.TrimPrefix(hdr, "Bearer "), s.Token) {
h.ServeHTTP(w, r)
return
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
http.Error(w, "unauthorised: open the dashboard URL archied logged at startup", http.StatusUnauthorized)
})
}
func tokenEqual(got, want string) bool {
return subtle.ConstantTimeCompare([]byte(got), []byte(want)) == 1
}