Documentation
¶
Overview ¶
Package authz is hearth's permission runtime: a grant holds capabilities, each on a path, and a check answers whether a caller may do 1 thing to 1 object.
The runtime is the code side of docs/specs/permissions.md. A generated check (protoc-gen-hearth-authz) reads a request's selector fields and asks the grant; a generated hook resolves a stored object's selector inside the app's transaction. A caller arrives in the context: the lodge adapter sets it from lodge's headers, an app's private socket and its stdio MCP server set the local user.
Index ¶
- Constants
- Variables
- func CleanPath(path string) (string, error)
- func TxFrom[Tx any](ctx context.Context) (Tx, bool)
- func WithCaller(ctx context.Context, caller Caller) context.Context
- func WithTx[Tx any](ctx context.Context, tx Tx) context.Context
- type Caller
- type DeniedError
- type Grant
- type Item
- type PathError
- type Sel
- type TxFunc
Constants ¶
const Root = "/"
Root is the root path: a grant on it covers every path, and an absent selector value means it.
Variables ¶
var ErrNoCaller = errors.New("permission denied: no caller")
ErrNoCaller is the error of a check that finds no caller: the permission runtime fails closed.
Functions ¶
func CleanPath ¶
CleanPath returns the canonical form of 1 path: its Unicode NFC form, with the rules of docs/specs/permissions.md checked.
A path starts with "/", and "/" alone is the root. A path has no empty segment, no "." segment, no ".." segment and no trailing "/". The empty string is not a path: an absent selector value is the root path, and the caller of CleanPath substitutes it.
func TxFrom ¶
TxFrom returns the transaction value of the context, and whether the context carries 1.
func WithCaller ¶
WithCaller returns a context that carries the caller.
Types ¶
type Caller ¶
type Caller struct {
// Principal is the caller's name: lodge's principal, or "local" for the
// app's own user. An empty Principal is not set; the adapters refuse it.
Principal string
// Grant is what the caller may do.
Grant Grant
}
Caller is who calls an app's API: the principal's name, and the grant that bounds it.
func CallerFrom ¶
CallerFrom returns the caller of the context, and whether the context carries 1.
func LocalCaller ¶
func LocalCaller() Caller
LocalCaller returns the caller of the local user: the principal "local" with the local grant.
type DeniedError ¶
type DeniedError struct {
// Cap is the capability the caller needs.
Cap string
// Selector is the selector of the refused value, or "" for a check that
// names no selector.
Selector string
// Path is the canonical path of the refused value.
Path string
}
DeniedError is a refused check: the capability the caller needs, and the selector and path the grant does not cover when the check was on a selector value.
func (*DeniedError) Error ¶
func (e *DeniedError) Error() string
Error implements error. The text is `permission denied: needs <cap> on <selector>=<path>`, or `permission denied: needs <cap>` for a check that names no selector.
type Grant ¶
type Grant struct {
// contains filtered or unexported fields
}
Grant is a set of grant items. A check passes when 1 item covers it, so the grant is the union of its items. The zero Grant allows nothing.
func GrantFrom ¶
GrantFrom returns the grant of the context's caller. A context with no caller gives ErrNoCaller, so a missing caller refuses every check.
func Local ¶
func Local() Grant
Local returns the grant of the local user: every capability on the root path. An app's stdio MCP server and its private socket run as this user.
func NewGrant ¶
NewGrant returns a grant of items, with each item checked: the capability matches `^[a-z][a-z0-9]{0,15}:[a-z][a-z0-9_]*$`, each selector name matches `^[a-z][a-z0-9_]*$`, and each path is valid (see CleanPath). The items are copied, and their paths are canonically spelled.
func ParseGrant ¶
ParseGrant reads a grant: a JSON array of items, each with a "cap" and an optional "sel". Every item is checked as NewGrant checks it, and an unknown member of an item is an error, so that a misspelled "sel" cannot widen the grant.
An absent "sel" is the root path for every selector. A "sel" that is there must be a JSON object: "sel": null or "sel": [ ] would read as no selector and so as the root, which widens the grant.
func (Grant) Allows ¶
Allows reports whether the grant covers the capability cap on 1 selector value, for filtering a list. An invalid path allows nothing.
func (Grant) Check ¶
Check reports whether the grant covers the capability cap on every selector value. One item must cover each value: the union of the grant's items is what a caller holds.
An absent value (an empty Path) is the root path. An invalid path returns a *PathError before any item is read. With no selector value at all, the check passes only when 1 item with cap holds the root path for every selector it names, or names none.
The local grant passes every check.
func (Grant) CheckAny ¶
CheckAny reports whether the grant holds the capability cap on any path. A generated check calls it before the app's hook runs, for an RPC on an object id: the hook then checks the object's stored selector value.
The local grant passes every check.
func (Grant) MarshalJSON ¶
MarshalJSON writes the grant as the JSON array of its items. The local grant has no items, so a marshalled local grant allows nothing: an app never sends it, and lodge never reads it.
type Item ¶
type Item struct {
// Cap is the capability: "<app>:<capability>".
Cap string `json:"cap"`
// Sel maps a selector's name to the path the item holds for it.
Sel map[string]string `json:"sel,omitempty"`
}
Item is 1 grant item: a capability, and the path it holds for each selector it names. A selector the item does not name is the root path for that selector.
type PathError ¶
type PathError struct {
// Selector is the selector the path belongs to, or "" when it belongs to
// no selector.
Selector string
// Path is the path as it was given.
Path string
// Reason is why the path is invalid.
Reason string
}
PathError is an invalid path: a grant item's path, or a request's.
A request's path is the *PathError that Check returns, and 1 grant item's path is the error that NewGrant or ParseGrant wraps.
type Sel ¶
type Sel struct {
// Selector is the selector's name.
Selector string
// Path is the value the request carries.
Path string
}
Sel is 1 selector value of a request: the selector's name, and the path the request carries for it. An empty Path is the root.