authn

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 4 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

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

Constants

This section is empty.

Variables

View Source
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 is a backend the check needs that did not answer; the credential may be fine.
	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

func AdmitActive(_ context.Context, p *principal.Principal) error

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.

func Reject

func Reject(w http.ResponseWriter, _ *http.Request, err error)

Reject is the default rejection response: the status Status maps err to, with its status text and nothing about the error.

func Status

func Status(err error) int

Status is the HTTP status a rejection maps to. An error that is none of the package's is a server failure.

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

type AuthenticatorFunc func(r *http.Request) (*principal.Principal, bool, error)

AuthenticatorFunc adapts a function to Authenticator.

func (AuthenticatorFunc) Authenticate

func (f AuthenticatorFunc) Authenticate(r *http.Request) (*principal.Principal, bool, error)

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.

func (Chain) Authenticate

func (c Chain) Authenticate(r *http.Request) (*principal.Principal, error)

Authenticate returns the request's principal, nil when no authenticator claims it, or the error that rejected it.

Jump to

Keyboard shortcuts

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