feat(auth): membership authority endpoint + fail-closed API auth (RBAC Phase 1)
ci / shared (pull_request) Failing after 12s
ci / test (pull_request) Successful in 21m20s
ci / image (pull_request) Skipped

GET /v1/users/{id}/memberships answers 'which tenants does this JWT
subject belong to, with which org_roles and entitlements' (ratified
auth design, model B2). Keycloak supplies user->tenant links and roles
via the Adapter (attribute projection until the realm migrates to
Organizations); registered tenants override status/plan/products from
registry tables.

New internal/authn verifies Keycloak bearer tokens (OIDC discovery +
JWKS, audience AUTH_EXPECTED_AUDIENCE, default tenant-registry). With
AUTH_ENABLED=true all routes except /healthz + /readyz require a token
and the server refuses to start if the verifier cannot initialize;
false (dev default) keeps the API open. Completes the M5.2-deferred
org_roles lookup and replaces the 'M4.3 adds JWT validation' TODO in
the spec.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Sharang Parnerkar
2026-08-24 16:09:53 +02:00
co-authored by Claude Fable 5
parent 31cb06cf3d
commit 4a49c630d4
14 changed files with 797 additions and 7 deletions
+44 -5
View File
@@ -5,9 +5,11 @@
package server
import (
"errors"
"log/slog"
"net/http"
"gitea.meghsakha.com/platform/tenant-registry/internal/authn"
"gitea.meghsakha.com/platform/tenant-registry/internal/config"
"gitea.meghsakha.com/platform/tenant-registry/internal/keycloak"
"gitea.meghsakha.com/platform/tenant-registry/internal/store"
@@ -19,15 +21,24 @@ type Server struct {
Log *slog.Logger
Store store.Store
Keycloak keycloak.Adapter // never nil — main wires Mock when KC env is unset
Auth *authn.Verifier // nil ⇒ AUTH_ENABLED=false, API is open (dev only)
}
// NewRouter builds the http.Handler with logging middleware applied.
//
// Route auth classes (ratified auth design): /healthz and /readyz are
// PUBLIC_EXPLICIT (orca probes, no auth by design); every other route is
// INTERNAL_SERVICE_ONLY and sits behind requireAuth. New routes land in
// the protected mux by construction — registering one on the root mux is
// the exception and needs a PUBLIC_EXPLICIT justification comment.
func NewRouter(s *Server) http.Handler {
mux := http.NewServeMux()
root := http.NewServeMux()
// health + status
mux.HandleFunc("GET /healthz", s.healthz)
mux.HandleFunc("GET /readyz", s.readyz)
// PUBLIC_EXPLICIT: health + status probes.
root.HandleFunc("GET /healthz", s.healthz)
root.HandleFunc("GET /readyz", s.readyz)
mux := http.NewServeMux()
// tenants
mux.HandleFunc("POST /v1/tenants", s.createTenant)
@@ -60,7 +71,35 @@ func NewRouter(s *Server) http.Handler {
// the "pull" complement for when the realm is reconfigured to fetch.
mux.HandleFunc("POST /v1/internal/keycloak/claims", s.kcClaims)
return logRequest(s.Log)(mux)
// memberships — the B2 membership authority: which tenants does a
// JWT subject belong to, with which org_roles and entitlements.
mux.HandleFunc("GET /v1/users/{id}/memberships", s.getUserMemberships)
root.Handle("/", s.requireAuth(mux))
return logRequest(s.Log)(root)
}
// requireAuth gates the INTERNAL_SERVICE_ONLY routes. With Auth nil
// (AUTH_ENABLED=false) it passes through — the startup log carries the
// warning, and the Auth-5 activation flips the env, not the code.
func (s *Server) requireAuth(next http.Handler) http.Handler {
if s.Auth == nil {
return next
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
p, err := s.Auth.Verify(r.Context(), r.Header.Get("Authorization"))
if err != nil {
if errors.Is(err, authn.ErrNoToken) {
writeError(w, http.StatusUnauthorized, "TOKEN_MISSING", "bearer token required")
return
}
// Signature, issuer, expiry, and audience failures all land
// here; the message says which without echoing the token.
writeError(w, http.StatusUnauthorized, "TOKEN_INVALID", err.Error())
return
}
next.ServeHTTP(w, r.WithContext(authn.WithPrincipal(r.Context(), p)))
})
}
func (s *Server) healthz(w http.ResponseWriter, _ *http.Request) {