guard

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package guard provides Celerity's authorisation model.

A guard is a layer that returns a decision rather than a result, so a denial is a 401 or a 403 the framework writes rather than an error every handler has to remember to shape the same way.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithIdentity

func WithIdentity(ctx context.Context, decision Decision) context.Context

WithIdentity returns a context carrying the identity a guard established.

Types

type Decision

type Decision struct {
	// Allowed reports whether the event may reach the handler.
	Allowed bool
	// Reason is reported to the caller when the event is refused, and should say
	// what is missing without saying what would satisfy it.
	Reason string
	// Identity is the authenticated principal, reached from a handler through
	// [IdentityFrom]. Nil when the guard authorises without identifying.
	Identity any
	// Claims are the verified token claims, when the guard verified a token.
	Claims map[string]any
}

Decision is a guard's answer.

func Allow

func Allow(identity any) Decision

Allow returns a decision permitting the event, carrying an identity.

func Deny

func Deny(reason string) Decision

Deny returns a decision refusing the event.

func IdentityFrom

func IdentityFrom(ctx context.Context) (Decision, bool)

IdentityFrom returns the identity the guard protecting this handler established, and whether there was one.

type Guard

type Guard func(ctx context.Context, req *Request) (Decision, error)

Guard decides whether an event may reach its handler.

Returning an error means the decision could not be made, which is a 500, as distinct from a decision to refuse, which is a 401 or 403.

type Request

type Request struct {
	Kind    handler.Kind
	Method  string
	Route   string
	Headers handler.Params
	Query   handler.Params
	// ConnectionID is set for WebSocket events.
	ConnectionID string
	SourceIP     string
	RequestID    string
}

Request is what a guard decides about.

It carries the parts of an event that authorisation is decided from, so one guard can protect handlers of different kinds.

Jump to

Keyboard shortcuts

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