go-security models authentication and authorization around three core concepts: an Authentication carrying the current security state, an Authority granted to that principal, and a SecurityContext threaded through context.Context. Interfaces stand in for annotations, and plain middleware functions replace filter chains.

Authentication

An authentication (core.Authentication) represents the security state of a request. It is either:
  • Unauthenticated — a request token carrying credentials yet to be verified (e.g. *core.UsernamePasswordToken)
  • Authenticated — a result token with a verified principal and granted authorities (e.g. *core.AuthenticatedToken)
core.MustAuthentication panics if no authentication is on the context — use core.AuthenticationFrom for the optional form:

Authority

An authority (core.Authority) is a string value granted to a principal. Roles are authorities with the ROLE_ prefix:
The HasRole authorization rule auto-prefixes with ROLE_:

The Security Context

The authentication lives on context.Context, scoped to the request:
The middleware chain places the authentication on the context before your handlers see the request. You never manage this manually.

Authentication Flow

Manager and Providers

authn.Manager fans out to a list of authn.Provider implementations. Each provider handles one kind of credential:
The manager iterates providers in order and calls the first one that returns Supports(token) == true — first-supporting-provider-wins dispatch.

User

authn.User is the UserDetails equivalent:
User.Check() returns the first account-status error (core.ErrAccountDisabled, core.ErrAccountLocked, etc.) or nil when the account is usable.

Sentinel Errors

All authentication failures wrap a sentinel from core: Match them with errors.Is: