mirror of
https://github.com/hanzoai/bootnode.git
synced 2026-10-25 02:31:30 +00:00
Adds a Go subsystem alongside the existing Python FastAPI service so
bootnode can fold into `superbase` per HIP-0106. The Python service in
`api/bootnode/` stays untouched.
What landed:
- `schema/bootnode.zap` — public typed surface (chains, RPC, tokens,
NFTs, transfers, smart wallets, ERC-4337, gas, webhooks, fleets,
nodes, usage). No `.capnp` anywhere — ZAP only, per brand policy.
- `zap/gen/` — hand-rolled Go bindings that match what `zapc generate`
emits, so `go build` works without zapc installed locally.
- `api/mount.go` — the HIP-0106 contract:
func Mount(r chi.Router, deps Deps) error
mounts every FastAPI-parity route group through `api/handlers/`.
- `api/deps.go` — cross-subsystem ZAP client interfaces (IAM, KMS,
Base, O11y, Commerce). No business logic, no hard imports — keeps
the Bootnode module independent of sibling subsystems until they
ship their typed clients.
- `api/handlers/` — 149 chi routes mounted (Python has 150). Each
handler body is a 501 stub with a `TODO(bootnode-port)` pointing at
the Python file + symbol to port.
- `api/middleware/` — IAM-backed auth, OTEL-friendly telemetry,
per-org rate limit (stub).
- `api/store/` — per-tenant SQLite access via `deps.Base.Open(orgID,
"bootnode")` per HIP-0302.
- `api/web3/` — chain registry + RPC pool surface (production version
prefers luxfi/* over upstream go-ethereum).
- `cmd/bootnode/main.go` — standalone shim; same `api.Mount` superbase
calls in production.
- `docs/PYTHON_TO_GO_PORT.md` — endpoint-by-endpoint port map.
Build: `go build ./...` and `go vet ./...` clean. Standalone shim
smokes 200 on /health, /ready, /v1/chains; 401 on auth-required
endpoints with no JWT; 501 on every TODO route.
Out of scope (not in this commit):
- Handler bodies (Phase 2 — porting in RPC → tokens → NFTs → ...
priority order).
- superbase wiring (lands when the superbase repo opens its main).
- Python service removal (stays until handlers reach prod parity).
112 lines
3.2 KiB
Go
112 lines
3.2 KiB
Go
// Package middleware holds the chi middleware Bootnode mounts.
|
|
//
|
|
// All identity decisions go through deps.IAM — Bootnode never decodes
|
|
// JWTs itself. Per the Hanzo header convention (LLM.md), incoming
|
|
// X-Org-Id / X-User-Id / X-User-Email are TRUSTED only when minted by
|
|
// the gateway from a validated JWT. When Bootnode runs standalone it
|
|
// derives them from the Authorization bearer via deps.IAM.
|
|
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// ctxKey is unexported so external packages can't accidentally clobber
|
|
// our context values.
|
|
type ctxKey int
|
|
|
|
const (
|
|
ctxOrgID ctxKey = iota
|
|
ctxUserID
|
|
ctxEmail
|
|
)
|
|
|
|
// IAMVerifier matches api.Deps.IAM but lets this package avoid an
|
|
// import cycle.
|
|
type IAMVerifier interface {
|
|
VerifyJWT(ctx context.Context, bearer string) (orgID, userID, email string, err error)
|
|
}
|
|
|
|
// AuthOptional wires identity into request context when the
|
|
// Authorization header parses; missing/invalid tokens fall through
|
|
// (handlers that require auth call MustAuth on the context).
|
|
func AuthOptional(iam IAMVerifier) func(http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
|
|
// 1) Gateway-trusted headers (per HTTP Header Convention
|
|
// 2026-03-27): only honored when the upstream gateway
|
|
// has already validated the JWT. We trust them iff IAM
|
|
// is nil (split-deploy behind gateway) or the bearer
|
|
// matches.
|
|
if orgID := r.Header.Get("X-Org-Id"); orgID != "" && iam == nil {
|
|
ctx = context.WithValue(ctx, ctxOrgID, orgID)
|
|
ctx = context.WithValue(ctx, ctxUserID, r.Header.Get("X-User-Id"))
|
|
ctx = context.WithValue(ctx, ctxEmail, r.Header.Get("X-User-Email"))
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
return
|
|
}
|
|
|
|
// 2) Authorization: Bearer ... → IAM verify.
|
|
if iam != nil {
|
|
if bearer := bearerToken(r); bearer != "" {
|
|
orgID, userID, email, err := iam.VerifyJWT(ctx, bearer)
|
|
if err == nil {
|
|
ctx = context.WithValue(ctx, ctxOrgID, orgID)
|
|
ctx = context.WithValue(ctx, ctxUserID, userID)
|
|
ctx = context.WithValue(ctx, ctxEmail, email)
|
|
}
|
|
}
|
|
}
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
})
|
|
}
|
|
}
|
|
|
|
// OrgID returns the org slug attached by AuthOptional, or "".
|
|
func OrgID(ctx context.Context) string {
|
|
if v, ok := ctx.Value(ctxOrgID).(string); ok {
|
|
return v
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// UserID returns the user ID attached by AuthOptional, or "".
|
|
func UserID(ctx context.Context) string {
|
|
if v, ok := ctx.Value(ctxUserID).(string); ok {
|
|
return v
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// Email returns the user email attached by AuthOptional, or "".
|
|
func Email(ctx context.Context) string {
|
|
if v, ok := ctx.Value(ctxEmail).(string); ok {
|
|
return v
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// RequireAuth wraps a handler so it 401s when no org is attached.
|
|
func RequireAuth(h http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if OrgID(r.Context()) == "" {
|
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
h(w, r)
|
|
}
|
|
}
|
|
|
|
func bearerToken(r *http.Request) string {
|
|
h := r.Header.Get("Authorization")
|
|
const p = "Bearer "
|
|
if !strings.HasPrefix(h, p) {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(h[len(p):])
|
|
}
|