authn package provides the Manager that fans out to a list of
Provider implementations, plus every credential-specific provider: password,
bearer token, API key, HMAC-signed requests, OAuth2 login, TOTP, magic link,
passkey, and multi-factor orchestration.
Import
Manager
Manager.Authenticate(ctx, token) iterates providers in order, calls the
first one where Supports(token) returns true, and returns the authenticated
token or an error wrapping core.ErrProviderNotFound.
Manager options
User
User is the UserDetails equivalent — the result of UserService.LoadUser.
user.CheckPreAuth()— disabled, then expired-account, then locked. Run before a password/credential is examined.user.CheckPostAuth()— expired-credentials. Run only after a correct credential.user.Check()— the combination of both, for callers that just want a single status gate (used byAPIKeyProvider,HMACProvider, andMagicLinkProvider).
PasswordProvider calls CheckPreAuth/CheckPostAuth directly rather than
Check(), so status is enforced on the correct side of the password
comparison (pre-auth checks gate access regardless of the password; the
credentials-expired check only fires once the password has matched).
UserService
authn.ErrUserNotFound when the username is not in your store.
PasswordProvider performs a dummy hash comparison on ErrUserNotFound (using
the same KDF as a real match) to prevent username-enumeration timing attacks.
InMemoryUserService
Useful for tests and playgrounds. It takes no constructor arguments; users are added afterwards withAdd, which takes a User value (not a pointer):
Providers
PasswordProvider
Username + password authentication with KDF verification.- Pre-auth checks:
Disabled,AccountExpired,Locked - Post-auth check:
CredentialsExpired - Timing equalization: performs a dummy password comparison (with the configured encoder) when the user is not found, so response time does not reveal whether a username exists
BearerTokenProvider
Validates a raw bearer token by delegating to aTokenAuthenticator:
*resource.BearerTokenAuthenticator (JWT) and *resource.IntrospectionAuthenticator
(opaque tokens) both satisfy TokenAuthenticator — see
resource-server. authn.NewBearerAuthenticationToken(token)
builds the request token manually if you want to route a raw bearer token
through Manager.Authenticate yourself instead of using a resource-server
middleware.
APIKeyProvider
Authenticates a raw API key against anAPIKeyService:
web/middleware.APIKeyAuth to read the key off a request:
authn.NewAPIKeyToken(key) builds the request token manually if you are not
using the middleware.
HMACProvider
Validates HMAC-signed requests (timestamp + method + path + body hash) against a key store:SigningKeyStore resolves a shared secret by key ID; NewInMemorySigningKeyStore()
Add(keyID, secret)covers tests and small deployments. To authenticate real requests, mountweb/middleware.HMACSignatureAuth(manager), which parses theAuthorization: HMAC keyId=...,ts=...,nonce=...,sig=...header, hashes the body, and builds the token viaauthn.CanonicalHMACRequest+authn.NewHMACSignatureTokenfor you.
OAuth2LoginProvider
Adapts a completed OAuth2/OIDC exchange toauthn.Provider so it can run
through the same Manager as other credential types:
OAuth2Completer. authn.NewOAuth2LoginToken(registrationID)
builds the request token. Note that the built-in oauth2 package’s own login
callback (see oauth2) creates its session directly and does
not route through this provider — use OAuth2LoginProvider only if you
want OAuth2 completion to flow through a shared authn.Manager alongside
other providers.
TOTPProvider
Validates time-based one-time passwords (RFC 6238) as a second factor:authn.NewOTPToken(username, code) against the Manager
(or the TOTPProvider directly).
MagicLinkProvider
Validates single-use, expiring tokens for passwordless email/SMS login:MemoryMagicLinkStore implements Create/Validate/Consume with a 32-byte
random base64url token and no background goroutine — call DeleteExpired()
yourself for GC. authn.NewMagicLinkToken(token) builds the request token; see
login for the HTTP request/verify handlers built on top of
this provider.
PasskeyProvider
Validates WebAuthn assertions (sign-in only — registration is a separate ceremony, not an authentication act) by delegating to aPasskeyAuthenticator
you implement, typically wrapping github.com/go-webauthn/webauthn:
PasskeyCredential is the persisted representation of a registered credential
your PasskeyAuthenticator implementation reads and writes:
PasskeyAuthenticator
implementation to use for storage — this package defines no concrete
implementation, keeping it stdlib-only:
authn.NewPasskeyToken(credID, authData, clientDataJSON, sig, userHandle)
builds the assertion request token; web/auth provides the HTTP handlers (see
login).
MFAManager
Orchestrates a two-step (primary + second-factor) login. It is a standalone orchestrator, not aProvider — it sits above the Manager, taking an
already-authenticated primary result and gating it on one or more
MFAFactors.
MFAFactor is the interface you implement per second-factor type; it can
delegate straight to TOTPProvider:
MFAPolicy.RequiredFactors decides which factor IDs a given principal must
complete; FixedMFAPolicy (via NewFixedMFAPolicy(ids...)) applies the same
list to everyone.
Flow:
CompleteMFA returns a *PartialMFAToken (not yet Authenticated()) when
more factors remain, or the final authenticated token once all required
factors are satisfied. MemoryMFAStore is the in-process MFAStore
implementation with TTL expiry. See login for the HTTP
handlers built on top of MFAManager.
Error sentinels
Other errors (wrong password, account locked) are
core.* sentinels.