Documentation
¶
Index ¶
- Variables
- func Forbidden(reason string) error
- func Get[T, Q any](handler GetHandler[T, Q], rules ...Rule[T]) func(thx.Context, Q) thx.Result
- func Reason(err error) string
- func Route[T, Q, I any](handler Handler[T, Q, I], rules ...Rule[T]) func(thx.Context, Q, I) thx.Result
- func Subject[T any](ctx thx.Context) T
- func WithGuard[T any](path string, routes []thx.Route, rule Rule[T], opts ...GuardOption) thx.Routes
- type Context
- type DeniedError
- type GetHandler
- type GuardOption
- func OnError(handler func(thx.Context, error) thx.Result) GuardOption
- func OnForbidden(handler func(thx.Context, error) thx.Result) GuardOption
- func OnUnauthorized(handler func(thx.Context, error) thx.Result) GuardOption
- func RedirectForbidden(to string) GuardOption
- func RedirectUnauthorized(to string) GuardOption
- func RedirectWithCurrentPath(param string) GuardOption
- type Handler
- type Rule
Constants ¶
This section is empty.
Variables ¶
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 Get ¶
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 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 ¶
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 ¶
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 ¶
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 ¶
OnForbidden replaces the response for requests denied by a Rule. The handler receives the denial error; read its reason with auth.Reason.
func OnUnauthorized ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Authenticated is a Rule that allows every authenticated subject. Use it when a guard should require a login but no further authorization.