Skip to content

Commit d5ad8f9

Browse files
committed
Add auth/me route officially
1 parent ccebf0f commit d5ad8f9

3 files changed

Lines changed: 33 additions & 19 deletions

File tree

cmd/forge-proxy/main.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,14 @@ func run() error {
321321
authMux.HandleFunc("GET /healthz", healthz)
322322
authMux.Handle("GET /readyz", readyzHandler(database.Writer, database.Reader, oidcClient, sweeper))
323323

324-
// Register auth routes with per-endpoint rate limits.
325-
authMux.Handle("GET /auth/login",
326-
httplog.RateLimitMiddleware(loginLimiter, http.HandlerFunc(authH.HandleLogin)))
327-
authMux.Handle("GET /auth/callback",
328-
httplog.RateLimitMiddleware(callbackLimiter, http.HandlerFunc(authH.HandleCallback)))
329-
authMux.HandleFunc("POST /auth/logout", authH.HandleLogout)
324+
// Mount auth routes via the single source of truth in package auth.
325+
// Per-endpoint rate limits ride along via MountOptions. Adding a new
326+
// route is a one-line change in auth.Handler.Mount — it then shows
327+
// up in both the running binary and the test fixture automatically.
328+
authH.Mount(authMux, auth.MountOptions{
329+
LoginLimiter: loginLimiter,
330+
CallbackLimiter: callbackLimiter,
331+
})
330332

331333
webH := web.NewHandler()
332334
authMux.Handle("GET /", webH)

internal/auth/handlers.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,30 @@ func NewHandler(cfg *config.Config, o *OIDC, users *user.Store, sessions *sessio
4343
return &Handler{Cfg: cfg, OIDC: o, Users: users, Sessions: sessions}
4444
}
4545

46-
// Register wires the three /auth/* routes onto the supplied mux. The
47-
// already-signed-in check at GET /{$} is owned by internal/web (U7) —
48-
// keeping the root route there lets the web layer own asset serving and
49-
// the session-redirect compose cleanly without circular imports.
50-
//
51-
// The proxy host routing (U8) will eventually differentiate auth-host
52-
// paths from upstream-app paths, but for U6 every route serves regardless
53-
// of host — the binary listens on a single port and the mux dispatches
54-
// by path.
55-
func (h *Handler) Register(mux *http.ServeMux) {
56-
mux.HandleFunc("GET /auth/login", h.HandleLogin)
57-
mux.HandleFunc("GET /auth/callback", h.HandleCallback)
46+
// MountOptions configures per-route middleware applied by Mount. A nil
47+
// limiter skips rate limiting for that route — fine in tests, never in
48+
// production (main.go must always pass real limiters).
49+
type MountOptions struct {
50+
LoginLimiter *httplog.RateLimiter
51+
CallbackLimiter *httplog.RateLimiter
52+
}
53+
54+
// Mount wires every /auth/* route onto mux. This is the single source
55+
// of truth for auth-route registration — both production (cmd/forge-proxy)
56+
// and tests call it. Adding a new route here means it appears in both
57+
// the running binary and the test fixture, which prevents the
58+
// "registered in tests, 404s in prod" trap.
59+
func (h *Handler) Mount(mux *http.ServeMux, opts MountOptions) {
60+
var login http.Handler = http.HandlerFunc(h.HandleLogin)
61+
if opts.LoginLimiter != nil {
62+
login = httplog.RateLimitMiddleware(opts.LoginLimiter, login)
63+
}
64+
var callback http.Handler = http.HandlerFunc(h.HandleCallback)
65+
if opts.CallbackLimiter != nil {
66+
callback = httplog.RateLimitMiddleware(opts.CallbackLimiter, callback)
67+
}
68+
mux.Handle("GET /auth/login", login)
69+
mux.Handle("GET /auth/callback", callback)
5870
mux.HandleFunc("POST /auth/logout", h.HandleLogout)
5971
mux.HandleFunc("GET /auth/me", h.HandleMe)
6072
}

internal/auth/handlers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func newFixture(t *testing.T) *testFixture {
206206

207207
handler := NewHandler(cfg, o, users, sessions)
208208
mux := http.NewServeMux()
209-
handler.Register(mux)
209+
handler.Mount(mux, MountOptions{})
210210

211211
return &testFixture{t: t, slack: slack, cfg: cfg, handler: handler, mux: mux, dbobj: d}
212212
}

0 commit comments

Comments
 (0)