feat(19.7-01): in-process dispatch marker and compiled handler lookup

- bouncer.Dispatch, WithDispatch, Dispatched: context-only pre-resolved
  principal marker honoured by Registry.Middleware for listed guard names
- cabana.ResolveBackend honours a backend-kind marker; additive
  ResolveBackendToken, ScopeLimitFor and BackendPrincipal resolvers
- surf.Router.Handler and compileHandlers: one compiled chain per route,
  shared by BuildRouter validation and compile
- smoke tests TestDispatchSmoke, TestDispatchedSmoke, TestDispatchMarkerSmoke
- READMEs and docs/services/{authentication,routing}.md updated
This commit is contained in:
Jakub Zych
2026-10-22 16:00:19 +02:00
parent a74a218cd9
commit 1bd91a4522
14 changed files with 522 additions and 11 deletions

View File

@@ -196,8 +196,32 @@ type backendResolver struct {
// cover code. Routes registered before cabana.Activate must call this per
// request (lookup the published resolver each time). Returns
// ErrUnauthenticated or ErrTokenScope.
//
// A request whose context carries a bouncer.Dispatch marker for the "backend"
// guard (an in-process dispatch; no HTTP request can set one) is answered
// from the marker: its principal must be a backend one (a frontend marker is
// ErrUnauthenticated) and a scoped principal must still cover every cover
// code. No header is read in that case.
func ResolveBackend(app *backpack.App, r *http.Request, cover ...string) (*http.Request, *bouncer.Principal, error) {
if app == nil || r == nil || authorizationBearer(r) == "" {
if app == nil || r == nil {
return nil, nil, ErrUnauthenticated
}
if d, ok := bouncer.Dispatched(r.Context(), "backend"); ok {
if !d.Principal.Backend {
return nil, nil, ErrUnauthenticated
}
if d.Principal.Scope != nil && !d.Principal.Scope.Full {
if !scopeCoversAll(d.Principal.Scope, cover) {
return nil, nil, ErrTokenScope
}
}
ctx := bouncer.WithUser(r.Context(), d.Principal)
if d.Credential != nil {
ctx = bouncer.WithCredential(ctx, d.Credential)
}
return r.WithContext(ctx), d.Principal, nil
}
if authorizationBearer(r) == "" {
return nil, nil, ErrUnauthenticated
}
res, ok := app.Lookup[*backendResolver]()
@@ -240,6 +264,13 @@ func logTokenAuth(r *http.Request, outcome string, adminID, tokenID uint) {
remote = host
}
}
logTokenAuthAt(method, path, remote, outcome, adminID, tokenID)
}
// logTokenAuthAt writes the admin.auth line for a token check that has no
// request (ResolveBackendToken passes path "mcp"); logTokenAuth delegates
// here so both keep one shape.
func logTokenAuthAt(method, path, remote, outcome string, adminID, tokenID uint) {
args := []any{"outcome", outcome, "method", method, "path", path, "remote", remote}
if adminID != 0 {
args = append(args, "admin_id", adminID)