Documentation
¶
Overview ¶
Package authentication defines the protocol-neutral authentication contracts shared by every authenticator in this module.
A transport adapter (for example package middleware) turns request data into a Credential; an Authenticator verifies it and returns a Principal. Federated logins (OIDC, OAuth2, SAML) implement FederatedProvider with a Begin/Complete pair instead. Chain composes authenticators, and ContextWithPrincipal / PrincipalFromContext carry the result through a request.
All values are copied on the way in and out (see the Clone methods), so callers never share mutable state with an authenticator. Errors are the sentinels declared in this package, returned without wrapping detail that could contain credential material; test them with errors.Is.
The package does not import package authorization: authentication and authorization are independent and can be used separately.
Index ¶
Constants ¶
const ( // CredentialKindJWT is a JWS compact serialization. CredentialKindJWT = "jwt" // CredentialKindBearer is an opaque bearer token that is not a JWT. CredentialKindBearer = "bearer" )
Well-known Credential kinds produced by transport adapters.
Variables ¶
var ( // ErrInvalidPrincipal reports a principal that fails ValidatePrincipal's // identity or bound checks. ErrInvalidPrincipal = errors.New("authentication: invalid principal") // ErrInvalidCredential reports malformed credentials or invalid // configuration passed to a constructor. ErrInvalidCredential = errors.New("authentication: invalid credential") // ErrUnauthenticated reports a credential that was rejected. It is the // generic failure: authenticators use it instead of revealing why. ErrUnauthenticated = errors.New("authentication: unauthenticated") // ErrExpired reports a credential, link, state or session whose lifetime // has ended. ErrExpired = errors.New("authentication: expired") // ErrReplay reports one-time state (callback state, magic link, refresh // token) that was unknown or already used, or a duplicate store key. ErrReplay = errors.New("authentication: replay detected") // ErrInvalidCallback reports a malformed federated callback or an // unregistered redirect URI. ErrInvalidCallback = errors.New("authentication: invalid callback") // ErrProviderNotFound reports that no provider matches a name or domain. ErrProviderNotFound = errors.New("authentication: provider not found") // ErrUnsupportedCredential reports that an Authenticator does not handle // the presented credential kind or format. Chain uses it to try the next // Authenticator; it is still an authentication failure for callers. ErrUnsupportedCredential = errors.New("authentication: unsupported credential") )
Sentinel errors returned by this module's authenticators. They never carry credential material.
Functions ¶
func ContextWithPrincipal ¶
ContextWithPrincipal returns a child context carrying an independently owned copy of principal.
func ValidatePrincipal ¶
ValidatePrincipal checks identity, lifetime, and bounded claim data at now. ID, Issuer and Subject must be non-empty UTF-8 without NUL; both times must be non-zero UTC with AuthenticatedAt <= now < ExpiresAt; traits and methods are bounded in count and size and methods are unique. It returns ErrExpired when only the lifetime has ended and ErrInvalidPrincipal otherwise.
Types ¶
type Authenticator ¶
type Authenticator interface {
Authenticate(context.Context, Credential) (Principal, error)
}
Authenticator validates one credential and returns its principal.
func Chain ¶
func Chain(authenticators ...Authenticator) Authenticator
Chain returns an Authenticator that tries authenticators in order. The first result that is not ErrUnsupportedCredential wins, whether it succeeds or fails, so a recognized but invalid credential is never retried elsewhere. When no authenticator recognizes the credential, Chain returns ErrUnsupportedCredential. Each authenticator receives its own copy of the credential. A nil authenticator fails closed with ErrUnauthenticated.
Authenticators that reject foreign credential kinds with an error other than ErrUnsupportedCredential (for example jwt.Verifier) should be placed last.
type BeginRequest ¶
BeginRequest describes the caller-controlled inputs to a federated flow.
func (BeginRequest) Clone ¶
func (r BeginRequest) Clone() BeginRequest
Clone returns an independently owned request.
type Credential ¶
Credential is credential material presented to an Authenticator. Callers should discard it as soon as authentication completes.
func (Credential) Clone ¶
func (c Credential) Clone() Credential
Clone returns independently owned credential data.
type FederatedProvider ¶
type FederatedProvider interface {
Name() string
Begin(context.Context, BeginRequest) (Redirect, error)
Complete(context.Context, Callback) (Principal, error)
}
FederatedProvider starts and completes a federated login flow.
type Principal ¶
type Principal struct {
ID string
Issuer string
Subject string
AuthenticatedAt time.Time
ExpiresAt time.Time
Traits map[string][]string
Methods []string
}
Principal is a protocol-neutral authenticated identity. ID is unique across issuers (authenticators in this module use "<issuer>|<subject>"). AuthenticatedAt and ExpiresAt are UTC and bound the principal's validity; Traits carries bounded claim data and Methods the authentication methods used, such as "jwt", "oidc", "saml" or "apitoken".
func PrincipalFromContext ¶
PrincipalFromContext returns an independently owned copy of the principal stored by ContextWithPrincipal.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package apitoken issues and authenticates long-lived personal and service API tokens of the form <prefix>_<id>_<secret>.
|
Package apitoken issues and authenticates long-lived personal and service API tokens of the form <prefix>_<id>_<secret>. |
|
Package device implements an OAuth 2.0 Device Authorization Grant (RFC 8628) client for command-line tools on machines without a browser.
|
Package device implements an OAuth 2.0 Device Authorization Grant (RFC 8628) client for command-line tools on machines without a browser. |
|
Package jwt authenticates signed JSON Web Tokens (RFC 7519) as authentication.Credential values of kind authentication.CredentialKindJWT.
|
Package jwt authenticates signed JSON Web Tokens (RFC 7519) as authentication.Credential values of kind authentication.CredentialKindJWT. |
|
Package magiclink issues and consumes single-use, short-lived login links.
|
Package magiclink issues and consumes single-use, short-lived login links. |
|
Package metadata caches identity provider metadata documents (issuer and endpoint URLs, supported algorithms and scopes) in a cache Cache.
|
Package metadata caches identity provider metadata documents (issuer and endpoint URLs, supported algorithms and scopes) in a cache Cache. |
|
Package middleware adapts authentication.Authenticator to transports.
|
Package middleware adapts authentication.Authenticator to transports. |
|
Package oauth2 implements the plain OAuth 2.0 authorization code flow with PKCE (S256) as an authentication.FederatedProvider, for providers that do not issue OpenID Connect ID tokens.
|
Package oauth2 implements the plain OAuth 2.0 authorization code flow with PKCE (S256) as an authentication.FederatedProvider, for providers that do not issue OpenID Connect ID tokens. |
|
Package oidc implements the OpenID Connect authorization code flow with PKCE (S256) and nonce as an authentication.FederatedProvider.
|
Package oidc implements the OpenID Connect authorization code flow with PKCE (S256) and nonce as an authentication.FederatedProvider. |
|
Package saml authenticates SAML 2.0 Web Browser SSO responses (HTTP-POST binding) for service-provider initiated logins, as an authentication.FederatedProvider.
|
Package saml authenticates SAML 2.0 Web Browser SSO responses (HTTP-POST binding) for service-provider initiated logins, as an authentication.FederatedProvider. |
|
Package session manages durable, server-side login sessions with rotating refresh tokens.
|
Package session manages durable, server-side login sessions with rotating refresh tokens. |
|
Package sso selects the federated login provider for a user's input: an exact provider name or an e-mail address whose domain is registered.
|
Package sso selects the federated login provider for a user's input: an exact provider name or an e-mail address whose domain is registered. |