The last gap in the identity boundary: enabling a provider used to lock browsers out, because nothing could turn a sign-in into the credential the dashboard already verifies. Now a browser completes the authorization code flow with PKCE and the identity reaching the request context is the person who signed in. The audience is requested AT THE AUTHORIZATION ENDPOINT, not only at the token endpoint. Infra measured that a provider asked otherwise returns the audience empty, and this dashboard then refuses a perfectly valid token -- a failure that reads as a signing fault and is actually a missing query parameter. A test asserts the parameter is present, so it cannot quietly go missing. The flow is the libraries': discovery, the code exchange, PKCE generation and the challenge transformation are go-oidc and x/oauth2, so archie writes no protocol. The token returned by the exchange is verified through the SAME Verifier a presented token goes through, and the identity is then resolved through the same rule -- one Resolve now shared by Authenticate and the callback, so a browser sign-in cannot be accepted on terms a presented token would be refused on. Archie still issues no session. The callback stores the PROVIDER's token in an HttpOnly cookie and every later request re-verifies it, so what archie holds cannot outlive the provider's own decision. The callback checks the state it issued, and expires the state and PKCE verifier cookies on use. The sign-in routes mount on the bypass mux because they are how a caller becomes authenticated; gating them behind the credential they obtain would make signing in impossible. The client secret is never a flag: -oidc-client-secret-env names the environment variable holding it, so it cannot reach a process listing or a log line. No client id means no sign-in route and token verification is unaffected, which is what an agent-only deployment wants. Evidence, produced by driv·ng the flow rather than the seam: internal/webui/api_tasks_actor_test.go's TestBrowserSignInRecordsTheSignedInPerson walks sign-in -> callback -> cookie -> action and asserts the recorded actor is the signed-in person with human_approved. TestCallbackRefusesAMismatchedState asserts a replayed state is refused before any credential is verified. internal/infrastructure/oidc/flow_test.go runs the real flow against a test provider's own authorize and token endpoints, asserting the audience parameter, the PKCE challenge, a successful exchange, and a refused wrong verifier. Verified: task check exit 0 on this tree.
123 lines
3.8 KiB
Go
123 lines
3.8 KiB
Go
package archieui
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"slices"
|
|
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
|
"github.com/samcharles93/archie-core/internal/webui"
|
|
)
|
|
|
|
// Run serves the dashboard until ctx is cancelled. It resolves its own
|
|
// options, dials the Gateway and State Store contracts itself, composes the
|
|
// HTTP surface over them, and unwinds listener then clients in reverse order
|
|
// on shutdown.
|
|
func Run(ctx context.Context, options Options) error {
|
|
log := slog.New(slog.NewJSONHandler(os.Stderr, nil)).With("component", "ui")
|
|
opts, err := Resolve(options, log)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var cleanups []func()
|
|
defer func() {
|
|
for _, cleanup := range slices.Backward(cleanups) {
|
|
cleanup()
|
|
}
|
|
}()
|
|
|
|
tasks, closeState, err := staterpc.Dial(opts.State.Target, opts.State.Token)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cleanups = append(cleanups, closeState)
|
|
chat, closeGateway, err := gatewayrpc.Dial(opts.Gateway.Target, opts.Gateway.Token)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cleanups = append(cleanups, closeGateway)
|
|
|
|
authenticate, err := dashboardAuthenticator(ctx, opts, tasks, log)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
login, err := dashboardLoginFlow(ctx, opts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
srv := compose(deps{
|
|
Options: opts,
|
|
Log: log,
|
|
Store: tasks,
|
|
Chat: chat,
|
|
Health: newReadinessRegistry(opts, tasks, chat),
|
|
ControlPlane: tasks.ControlPlane(),
|
|
Identities: tasks,
|
|
Authenticate: authenticate,
|
|
Login: login,
|
|
})
|
|
|
|
// Live activity has no in-process bus in this process: the pump reads
|
|
// events back out of the State Store over the same cursor SSE catch-up
|
|
// uses. Backgrounded so priming past a large events table cannot delay
|
|
// the listener (docs/architecture/migration-decisions.md, "Dashboard
|
|
// live event delivery").
|
|
go func() {
|
|
if err := srv.PumpEvents(ctx, opts.EventPollInterval); err != nil {
|
|
log.Error("event pump stopped; the activity feed will only show history", "err", err)
|
|
}
|
|
}()
|
|
|
|
listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", opts.Listen)
|
|
if err != nil {
|
|
return fmt.Errorf("listen for ui: %w", err)
|
|
}
|
|
cleanups = append(cleanups, func() { _ = listener.Close() })
|
|
log.Info(
|
|
"archie-ui running",
|
|
"addr", listener.Addr().String(),
|
|
"open", webui.DashboardURL(listener.Addr().String(), opts.Token),
|
|
"gateway", opts.Gateway.Target,
|
|
"state", opts.State.Target,
|
|
)
|
|
|
|
return serve(ctx, listener, srv.Handler(), opts)
|
|
}
|
|
|
|
// serve runs handler on listener until ctx ends, then shuts the HTTP server
|
|
// down within ShutdownTimeout so in-flight dashboard requests finish before
|
|
// the contract clients close. A graceful shutdown that overruns its deadline
|
|
// is a normal shutdown under load, not a process failure: the remaining
|
|
// connections are force-closed and serve reports clean.
|
|
func serve(ctx context.Context, listener net.Listener, handler http.Handler, opts Options) error {
|
|
server := &http.Server{Handler: handler, ReadHeaderTimeout: opts.ReadHeaderTimeout}
|
|
serveErr := make(chan error, 1)
|
|
go func() { serveErr <- server.Serve(listener) }()
|
|
select {
|
|
case <-ctx.Done():
|
|
shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), opts.ShutdownTimeout)
|
|
defer cancel()
|
|
if err := server.Shutdown(shutdownCtx); err != nil {
|
|
// Shutdown returned once the drain deadline elapsed with a request
|
|
// still in flight. Force-close what is left so shutdown finishes
|
|
// and the process exits cleanly rather than carrying a deadline
|
|
// error to the operator as a fatal exit (archie-core-u4xu).
|
|
_ = server.Close()
|
|
}
|
|
return nil
|
|
case err := <-serveErr:
|
|
if errors.Is(err, http.ErrServerClosed) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
}
|