Documentation
¶
Overview ¶
Package authn turns a request into a principal: an ordered chain of authenticators, the product hooks that run once one succeeds, and the HTTP middleware that puts the result in the request context.
It never mints account ids (ResolveUser maps an identity onto the product's own records) and never decides access: a request no authenticator claims passes through with no principal, and each route, or auth/rbac, decides what that caller may do.
Index ¶
- Variables
- func AdmitActive(_ context.Context, p *principal.Principal) error
- func Middleware(c Chain, reject func(w http.ResponseWriter, r *http.Request, err error)) func(http.Handler) http.Handler
- func Reject(w http.ResponseWriter, _ *http.Request, err error)
- func Status(err error) int
- type Authenticator
- type AuthenticatorFunc
- type Chain
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidCredential is a credential in a known format that is wrong, expired or revoked. ErrInvalidCredential = errors.New("authn: invalid credential") // ErrSessionDead is a session that no longer exists; the product clears its cookie. ErrSessionDead = errors.New("authn: session ended") // ErrInactive is a valid credential for an account that may not act: disabled, deleted or refused. ErrInactive = errors.New("authn: account inactive") ErrUnavailable = errors.New("authn: backend unavailable") )
The rejections every product maps the same way; wrap one with fmt.Errorf("%w: ...") to add detail. Status maps each to its HTTP status.
Functions ¶
func AdmitActive ¶
AdmitActive admits a principal whose Status is StatusActive and refuses every other, an unresolved one included.
It fails closed for every Kind: a service account or an admin principal is refused too unless ResolveUser, or the authenticator, sets its Status to StatusActive. A product that admits such callers marks them active there; AdmitActive makes no exception by kind.
func Middleware ¶
func Middleware(c Chain, reject func(w http.ResponseWriter, r *http.Request, err error)) func(http.Handler) http.Handler
Middleware runs c on every request. A principal goes into the request context (principal.From reads it); a rejection is answered by reject, Reject when nil; a request no authenticator claims passes through with no principal.
Types ¶
type Authenticator ¶
type Authenticator interface {
// Authenticate returns (nil, false, nil) when the request carries no credential in its format, so the chain tries the next one. An error means the credential is in its format but is not accepted.
Authenticate(r *http.Request) (*principal.Principal, bool, error)
}
Authenticator recognises one credential format.
An implementation must decline a credential that is not in its format with (nil, false, nil), never with an error: the chain stops at the first authenticator that claims the request or errors, so an error on a foreign format hides every later authenticator.
type AuthenticatorFunc ¶
AuthenticatorFunc adapts a function to Authenticator.
func (AuthenticatorFunc) Authenticate ¶
Authenticate calls f.
type Chain ¶
type Chain struct {
Authenticators []Authenticator
// ResolveUser maps the authenticated identity onto the product's account, filling in what the credential did not carry (ID, Status, Roles). Optional.
ResolveUser func(ctx context.Context, p *principal.Principal) error
// Admit is the central status check, run after ResolveUser for every credential kind. Optional; AdmitActive is the usual one.
Admit func(ctx context.Context, p *principal.Principal) error
}
Chain runs authenticators in order. The first that claims the request decides it: its principal, or its error, is the chain's answer and no later authenticator runs.