The core package defines the fundamental types shared across all modules: Authentication, Authority, context helpers, and sentinel errors.

Import

Authentication

Authentication is the central interface representing the security state of a request — either a credential-carrying request token (unauthenticated) or a verified principal token (authenticated).

Implementations

NewAuthenticatedToken(principal any, name string, authorities []Authority) returns a *UsernamePasswordToken with Authenticated() == true — there is no separate AuthenticatedToken type.

Sanitized

Returns a copy with the plaintext password zeroed out. The manager sanitizes tokens before publishing failure events so passwords never appear in audit logs.

Authority

An Authority is a defined string type representing a granted permission. Roles are authorities with the ROLE_ prefix (core.RolePrefix).

Helpers

HasRole and HasAnyRole in the authz package auto-prefix with ROLE_, so HasRole("ADMIN") checks for "ROLE_ADMIN".

Security Context

The current authentication is stored on context.Context. The middleware chain places it there before your handlers execute.

When to use MustAuthentication

MustAuthentication never panics: if no Authentication is on the context it returns a fresh *AnonymousToken (Authenticated() == false), so downstream authorization code can always call methods on the result without a nil check. Use AuthenticationFrom when you need to distinguish “no authentication was ever set” from “set but not authenticated.” Use IsAuthenticated as a shorthand when all you need is the boolean.

Sentinel Errors

All authentication and authorization failures wrap one of these sentinels. Match them with errors.Is:

Events

core/event defines the Publisher interface for authentication lifecycle hooks. See observe for integration with metrics and audit logs.
FuncPublisher adapts plain functions — either field can be left nil:
NopPublisher discards every event — the safe zero-config default.

Authorization events

AuthzPublisher is the parallel interface for authorization decisions (granted/denied), deliberately kept separate from Publisher so implementing one doesn’t force implementing the other:
FuncAuthzPublisher{OnGranted, OnDenied} and NopAuthzPublisher mirror the authentication-event helpers above.