Repository navigation
Expand file tree
/
Copy pathroute.go
More file actions
113 lines (101 loc) · 5.73 KB
/
Copy pathroute.go
File metadata and controls
113 lines (101 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package router
import "webtyp.com/model"
// Route describes a registered route and allows annotating it. It is returned by
// each Router registration method. Annotations are declarative: the contract does
// not enforce them — each concrete implementer (native server, edge runtime) enforces them.
type Route interface {
// Requires binds an RBAC permission to the route: the (resource, action) pair.
//
// Both are typed (model.Resource, model.Action). They used to be two bare strings in a
// row, so swapping them compiled — and the failure was not an error but a SILENT denial
// at runtime, in the one place where silence is unacceptable. Now it does not compile.
//
// The resource is open vocabulary: the app declares its own ("service_catalog"). The
// action is a closed CRUD set (model.Read, model.Update, …): persistence has four verbs
// and no tool in this ecosystem ever needed a fifth.
Requires(resource model.Resource, action model.Action) Route
// Authenticated marks the route as reachable by any identity, with no permission check.
// For operations on the CALLER themselves, where authentication already is the check.
Authenticated() Route
// Public marks the route as reachable with no identity at all.
Public() Route
// RequiresMFA demands that the caller proved a second, phishing-resistant
// factor (Context.Assurance() == model.AssuranceMultiFactor). It composes
// with Requires and Authenticated; on a Public route it is a contradiction
// and the enforcer refuses to start. A single-factor caller gets 401 with
// WWW-Authenticate: ChallengeMFARequired (RFC 9470), which a client uses to
// start a step-up ceremony.
RequiresMFA() Route
// Accepts declares the typed schema of the request body — the Args a caller
// must send. It is the counterpart of RouteInfo.Args: a transport that needs
// to advertise a schema (mcp's tools/list) reads it from here instead of the
// module hand-rolling wire metadata. nil means "no args" (a Route that never
// calls Accepts has Args == nil, the same as passing nil explicitly).
Accepts(args model.Fielder) Route
// Describe says what the route does, in one or two sentences a person or a language model
// reads to decide whether to call it: what it returns or changes, and the units of its data.
Describe(text string) Route
}
// ErrMsgPublicRequiresMFA is what an enforcer reports, at startup, for a route
// that is Public and RequiresMFA: a caller with no identity cannot present a
// second factor.
const ErrMsgPublicRequiresMFA = "is Public and RequiresMFA: a route with no identity cannot demand a second factor"
// RouteInfo is the read-only view of a registered route — for introspection.
type RouteInfo struct {
Method string // e.g. "GET", "POST"
Path string // e.g. "/api/users", "/api/orders/{id}"
Resource model.Resource // required by AccessGuarded; must be empty otherwise
Action model.Action // e.g. model.Read; 0 = none
// Access is what the route declared. The ZERO VALUE is model.AccessGuarded: a route that
// annotates nothing is unreachable until it declares a Resource, and an enforcer must
// reject it loudly at startup.
//
// It replaced a `Public bool` alongside an empty-or-not Resource. That encoding made an
// illegal state writable — a route could be Public AND carry a Requires, and the gate
// silently dropped the permission check: a route that looked protected and was not.
Access model.Access
// MFA is true when the route declared RequiresMFA.
MFA bool
// Description is what Route.Describe declared; "" when the route declared nothing.
Description string
// Dir is the directory served by PublicDir; "" for every other route.
// It exists so a whole served directory is visible to introspection instead of
// being smuggled past the router by a file-server fallback.
Dir string
// Args is the schema a caller must send, set via Route.Accepts; nil = no args.
// It is a Go-side value read directly by a transport that needs it (mcp builds
// its tools/list schema from it) — deliberately NOT part of EncodeFields: a
// Fielder's schema is a different shape of data than this route-metadata wire
// view, and serializing it is that transport's concern, not RouteInfo's.
Args model.Fielder
}
// IsPublic reports whether the route is reachable with no identity.
func (r RouteInfo) IsPublic() bool { return r.Access == model.AccessPublic }
// EncodeFields makes RouteInfo a model.Encodable, so it is serialized by webtyp/json
// through this DECLARED shape instead of by reflection over its Go fields.
//
// Reflection got it actively wrong, and wrong in the worst direction. `Access` and
// `Action` are numeric types, so a reflection-based encoder emitted them as bare numbers:
// the ZERO value of Access is AccessGuarded, so the MOST protected route in the server
// reported itself as `"Access":0` — which any human or agent reading the routes endpoint
// takes for "nothing declared", the exact opposite of the truth. `"Action":6` was equally
// unreadable. An endpoint whose whole job is to expose the security posture of a server
// must not invert it.
//
// Here the shape is stated, not guessed: the enums travel as the words they already know
// how to render, and `Dir` — an internal detail of PublicDir — stays out of the wire.
func (r RouteInfo) EncodeFields(w model.FieldWriter) {
w.String(keyMethod, r.Method)
w.String(keyPath, r.Path)
w.String(keyResource, string(r.Resource))
w.String(keyAction, r.Action.String()) // "ru", never 6
w.String(keyAccess, r.Access.String()) // "guarded", never 0
if r.MFA {
w.Bool(keyMFA, true)
}
if r.Description != "" {
w.String(keyDescription, r.Description)
}
}
// IsNil satisfies model.Encodable; a RouteInfo is a value and never nil.
func (r RouteInfo) IsNil() bool { return false }