auth

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 8 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

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrForbidden marks a denial by a Rule. Match it with errors.Is.
	ErrForbidden = errors.New("auth: forbidden")

	// ErrUnauthenticated marks a request without an authenticated subject.
	ErrUnauthenticated = errors.New("auth: unauthenticated")
)

Functions

func Forbidden

func Forbidden(reason string) error

Forbidden denies a request with the given reason.

func Get

func Get[T, Q any](handler GetHandler[T, Q], rules ...Rule[T]) func(thx.Context, Q) thx.Result

Get wraps an authenticated GET handler into a standard handler function, enforcing the given rules before the handler runs. Unlike rules on a guard, these run after the route matched, so they can read path parameters. Panics if the request is not authorized — use WithGuard to protect routes.

func Reason

func Reason(err error) string

Reason returns the denial reason carried by err, or "" if err is not a denial.

func Route

func Route[T, Q, I any](handler Handler[T, Q, I], rules ...Rule[T]) func(thx.Context, Q, I) thx.Result

Route wraps an authenticated handler (with body) into a standard handler function, enforcing the given rules before the handler runs. Panics if the request is not authorized — use WithGuard to protect routes.

func Subject

func Subject[T any](ctx thx.Context) T

Subject returns the authenticated entity stored on the context. It returns the zero value of T for unauthenticated requests.

func WithGuard

func WithGuard[T any](path string, routes []thx.Route, rule Rule[T], opts ...GuardOption) thx.Routes

WithGuard scopes the given routes under a path and denies access to requests that are unauthenticated or rejected by the rule.

The guard runs as middleware, before the request is matched against a route, so it also covers not-found responses and any handler registered directly on the sub-router's mux. The flip side is that path parameters are not yet parsed: a rule evaluated here sees an empty ctx.Param. Rules that depend on path parameters belong on the route itself, via Get or Route.

Pass Authenticated[T]() as the rule to require a login without further authorization.

Types

type Context

type Context[T any] = internal.AuthContext[T]

Context is an authenticated request context that provides access to the auth entity of type T via the Auth() method.

type DeniedError

type DeniedError struct {
	Reason string
}

DeniedError is the error returned by a denying Rule. It carries a human-readable reason that denial handlers can render.

func (*DeniedError) Error

func (e *DeniedError) Error() string

Error implements the error interface.

func (*DeniedError) Unwrap

func (e *DeniedError) Unwrap() error

Unwrap makes errors.Is(err, ErrForbidden) report true for denials.

type GetHandler

type GetHandler[T, Q any] func(Context[T], Q) thx.Result

GetHandler is a handler function for authenticated GET requests. T is the auth entity type, Q is the query parameter type.

type GuardOption

type GuardOption func(*guardOptions)

GuardOption configures how a guard responds to denied requests.

func OnError

func OnError(handler func(thx.Context, error) thx.Result) GuardOption

OnError replaces the response for rules that failed to reach a decision (for example a permission lookup that could not query the database). The default response is 500 with no body.

func OnForbidden

func OnForbidden(handler func(thx.Context, error) thx.Result) GuardOption

OnForbidden replaces the response for requests denied by a Rule. The handler receives the denial error; read its reason with auth.Reason.

func OnUnauthorized

func OnUnauthorized(handler func(thx.Context, error) thx.Result) GuardOption

OnUnauthorized replaces the response for unauthenticated requests. The handler receives ErrUnauthenticated.

func RedirectForbidden

func RedirectForbidden(to string) GuardOption

RedirectForbidden redirects requests denied by a Rule to the given URL instead of returning 403.

func RedirectUnauthorized

func RedirectUnauthorized(to string) GuardOption

RedirectUnauthorized redirects unauthenticated requests to the given URL instead of returning 401. HTMX requests receive an HX-Redirect header so the login page replaces the whole document instead of being swapped into a fragment.

func RedirectWithCurrentPath

func RedirectWithCurrentPath(param string) GuardOption

RedirectWithCurrentPath appends the original request URI as a query parameter on the unauthorized redirect URL, enabling post-login return navigation.

type Handler

type Handler[T, Q, I any] func(Context[T], Q, I) thx.Result

Handler is a handler function for authenticated requests with a body. T is the auth entity type, Q is the query parameter type, I is the input type.

type Rule

type Rule[T any] func(ctx thx.Context, subject T) error

Rule decides whether an authenticated subject may proceed with a request. It returns nil to allow the request, an error wrapping ErrForbidden to deny it, and any other error to signal that the decision could not be made — the latter is treated as an internal failure, never as a silent denial.

Rules only run for authenticated requests: the guard verifies authentication before evaluating them, so the subject is always populated.

func All

func All[T any](rules ...Rule[T]) Rule[T]

All allows a request only if every rule allows it. Rules are evaluated in order and evaluation stops at the first denial or internal error. All with no rules always allows.

func Any

func Any[T any](rules ...Rule[T]) Rule[T]

Any allows a request if at least one rule allows it. Internal errors are not swallowed: a rule that fails to reach a decision aborts the evaluation. Any with no rules always denies.

func Authenticated

func Authenticated[T any]() Rule[T]

Authenticated is a Rule that allows every authenticated subject. Use it when a guard should require a login but no further authorization.

func Check

func Check[T any](predicate func(subject T) bool, reason string) Rule[T]

Check builds a Rule from a predicate over the subject, denying with the given reason when the predicate returns false.

func Not

func Not[T any](rule Rule[T], reason string) Rule[T]

Not inverts a rule: a request the rule allows is denied with the given reason, and a request it denies is allowed. Internal errors still abort.

Jump to

Keyboard shortcuts

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