authz

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const Root = "/"

Root is the root path: a grant on it covers every path, and an absent selector value means it.

Variables

View Source
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

func CleanPath(path string) (string, error)

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

func TxFrom[Tx any](ctx context.Context) (Tx, bool)

TxFrom returns the transaction value of the context, and whether the context carries 1.

func WithCaller

func WithCaller(ctx context.Context, caller Caller) context.Context

WithCaller returns a context that carries the caller.

func WithTx

func WithTx[Tx any](ctx context.Context, tx Tx) context.Context

WithTx returns a context that carries the transaction value. The generated permission check calls it before it runs an object-id RPC.

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

func CallerFrom(ctx context.Context) (Caller, bool)

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

func GrantFrom(ctx context.Context) (Grant, error)

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

func NewGrant(items ...Item) (Grant, error)

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

func ParseGrant(data []byte) (Grant, error)

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

func (g Grant) Allows(cap, selector, path string) bool

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

func (g Grant) Check(cap string, sels ...Sel) error

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

func (g Grant) CheckAny(cap string) error

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) Items

func (g Grant) Items() []Item

Items returns the grant's items. The local grant has none.

func (Grant) MarshalJSON

func (g Grant) MarshalJSON() ([]byte, error)

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.

func (*PathError) Error

func (e *PathError) Error() string

Error implements error. The text is `invalid <selector> path "<path>": <reason>`, or `invalid path "<path>": <reason>` when the path belongs to no selector.

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.

type TxFunc

type TxFunc[Tx any] func(ctx context.Context, fn func(ctx context.Context, tx Tx) error) error

TxFunc runs 1 app transaction: it calls fn with the context and the transaction's value, and returns fn's error, committed or rolled back.

An app's object-id hooks and their RPC run inside 1 transaction, so that the object a hook checked is the object the RPC changes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL