Documentation
¶
Overview ¶
Package authentication defines framework-independent authentication contracts.
Index ¶
- Constants
- Variables
- func ContextWithPrincipal(ctx context.Context, principal Principal) context.Context
- type APIKeyCredential
- type Authenticator
- type BasicCredential
- type BearerCredential
- type Binding
- type Challenge
- type Clockdeprecated
- type Composite
- type Credential
- type CredentialKind
- type Event
- type Failure
- type FailureKind
- type FailureOption
- type Instrumented
- type Instrumenter
- type Outcome
- type Principal
- func (p Principal) Audiences() []string
- func (p Principal) AuthenticatedAt() time.Time
- func (p Principal) Claims() map[string]any
- func (p Principal) IsAnonymous() bool
- func (p Principal) Issuer() string
- func (p Principal) Method() string
- func (p Principal) Scopes() []string
- func (p Principal) Subject() string
- func (p Principal) TenantHints() []string
- type PrincipalSpec
- type Result
- type ResultState
Examples ¶
Constants ¶
const ( // MaxChallengeParameters bounds authentication challenge metadata. MaxChallengeParameters = 16 // MaxChallengeSchemeBytes bounds an authentication scheme token. MaxChallengeSchemeBytes = 64 // MaxChallengeNameBytes bounds an authentication parameter name. MaxChallengeNameBytes = 64 // MaxChallengeValueBytes bounds an authentication parameter value. MaxChallengeValueBytes = 1024 )
const ( // MaxClaims is the maximum number of entries in a principal claim map. MaxClaims = 128 // MaxClaimDepth is the maximum nesting depth accepted in principal claims. MaxClaimDepth = 8 // MaxClaimCollection is the maximum number of elements in a nested claim // map, slice, or array. MaxClaimCollection = 256 )
Variables ¶
var ( // ErrCredentialsAbsent means no credential was supplied by an enabled source. ErrCredentialsAbsent = errors.New("authentication: credentials absent") // ErrCredentialsInvalid means credential syntax or protocol data was invalid. ErrCredentialsInvalid = errors.New("authentication: credentials invalid") // ErrCredentialsRejected means a validly formed credential was not accepted. ErrCredentialsRejected = errors.New("authentication: credentials rejected") // to a transient dependency or infrastructure failure. ErrAuthenticationUnavailable = errors.New("authentication: validation unavailable") // ErrAmbiguousCredentials means more than one credential was supplied where // exactly one was required. ErrAmbiguousCredentials = errors.New("authentication: ambiguous credentials") // ErrInvalidChallenge identifies invalid challenge protocol data. ErrInvalidChallenge = errors.New("authentication: invalid challenge") // ErrInvalidConfiguration identifies unsafe or incomplete authenticator // configuration. ErrInvalidConfiguration = errors.New("authentication: invalid configuration") )
var ErrInvalidPrincipal = errors.New("authentication: invalid principal")
ErrInvalidPrincipal identifies a principal that violates an identity invariant or contains claims that cannot be copied safely.
Functions ¶
Types ¶
type APIKeyCredential ¶
type APIKeyCredential struct {
// contains filtered or unexported fields
}
APIKeyCredential is an API key with an optional non-secret key identifier. Its formatted representation is always redacted.
func NewAPIKeyCredential ¶
func NewAPIKeyCredential(keyID, key string) APIKeyCredential
NewAPIKeyCredential creates an API-key credential.
func (APIKeyCredential) GoString ¶
func (APIKeyCredential) GoString() string
func (APIKeyCredential) Key ¶
func (c APIKeyCredential) Key() string
func (APIKeyCredential) KeyID ¶
func (c APIKeyCredential) KeyID() string
func (APIKeyCredential) Kind ¶
func (APIKeyCredential) Kind() CredentialKind
func (APIKeyCredential) String ¶
func (APIKeyCredential) String() string
type Authenticator ¶
type Authenticator interface {
Authenticate(context.Context, Credential) (Result, error)
}
Authenticator validates one typed credential.
Example (BackgroundConsumer) ¶
package main
import (
"context"
"fmt"
authentication "github.com/faustbrian/go-authentication"
"github.com/faustbrian/go-authentication/apikey"
)
func main() {
authenticator, _ := apikey.NewStatic([]apikey.Entry{{
ID: "worker", Key: "secret",
Principal: authentication.PrincipalSpec{Subject: "invoice-worker"},
}})
result, err := authenticator.Authenticate(
context.Background(),
authentication.NewAPIKeyCredential("worker", "secret"),
)
principal, authenticated := result.Principal()
fmt.Println(err, authenticated, principal.Subject())
}
Output: <nil> true invoice-worker
type BasicCredential ¶
type BasicCredential struct {
// contains filtered or unexported fields
}
BasicCredential is a username and password extracted from Basic authentication. Its formatted representation is always redacted.
func NewBasicCredential ¶
func NewBasicCredential(username, password string) BasicCredential
NewBasicCredential creates a Basic credential.
func (BasicCredential) GoString ¶
func (BasicCredential) GoString() string
func (BasicCredential) Kind ¶
func (BasicCredential) Kind() CredentialKind
func (BasicCredential) Password ¶
func (c BasicCredential) Password() string
func (BasicCredential) String ¶
func (BasicCredential) String() string
func (BasicCredential) Username ¶
func (c BasicCredential) Username() string
type BearerCredential ¶
type BearerCredential struct {
// contains filtered or unexported fields
}
BearerCredential is an opaque bearer token. Its formatted representation is always redacted.
func NewBearerCredential ¶
func NewBearerCredential(token string) BearerCredential
NewBearerCredential creates a bearer credential.
func (BearerCredential) GoString ¶
func (BearerCredential) GoString() string
func (BearerCredential) Kind ¶
func (BearerCredential) Kind() CredentialKind
func (BearerCredential) String ¶
func (BearerCredential) String() string
func (BearerCredential) Token ¶
func (c BearerCredential) Token() string
type Binding ¶
type Binding struct {
Kind CredentialKind
Authenticator Authenticator
}
Binding associates a credential kind with one authenticator. Bindings of the same kind are evaluated in declaration order.
type Challenge ¶
type Challenge struct {
// contains filtered or unexported fields
}
Challenge is an immutable authentication challenge. Transport adapters are responsible for serializing it according to their protocol.
func NewChallenge ¶
NewChallenge validates and copies challenge protocol data.
func (Challenge) Parameters ¶
Parameters returns a defensive copy of the authentication parameters.
type Composite ¶
type Composite struct {
// contains filtered or unexported fields
}
Composite routes typed credentials to ordered authenticators. Only rejected credentials fall through; every other failure is terminal.
func NewComposite ¶
NewComposite validates and copies ordered authenticator bindings.
func (*Composite) Authenticate ¶
Authenticate evaluates authenticators bound to the credential kind in deterministic declaration order.
type Credential ¶
type Credential interface {
Kind() CredentialKind
fmt.Stringer
}
Credential is a typed authentication credential. Implementations redact their secret-bearing representation.
type CredentialKind ¶
type CredentialKind string
CredentialKind identifies a credential's protocol family.
const ( CredentialBasic CredentialKind = "basic" CredentialBearer CredentialKind = "bearer" CredentialAPIKey CredentialKind = "api_key" )
type Event ¶
type Event struct {
Outcome Outcome
Failure FailureKind
Duration time.Duration
}
Event contains bounded, secret-free authentication telemetry.
type Failure ¶
type Failure struct {
// contains filtered or unexported fields
}
Failure is a secret-safe, typed authentication failure.
func NewFailure ¶
func NewFailure(kind FailureKind, options ...FailureOption) *Failure
NewFailure creates a classified, secret-safe failure.
func (*Failure) Challenges ¶
Challenges returns a defensive copy of the associated challenges.
func (*Failure) Kind ¶
func (f *Failure) Kind() FailureKind
Kind returns the stable failure classification.
type FailureKind ¶
type FailureKind string
FailureKind classifies authentication failures independently of transports.
const ( FailureAbsent FailureKind = "absent" FailureInvalid FailureKind = "invalid" FailureRejected FailureKind = "rejected" FailureAmbiguous FailureKind = "ambiguous" )
type FailureOption ¶
type FailureOption func(*Failure)
FailureOption configures a Failure.
func WithChallenges ¶
func WithChallenges(challenges ...Challenge) FailureOption
WithChallenges associates transport-independent challenges with a failure.
func WithFailureCause ¶
func WithFailureCause(cause error) FailureOption
WithFailureCause preserves cause for errors.Is and errors.As without including the cause text in Failure.Error.
type Instrumented ¶
type Instrumented struct {
// contains filtered or unexported fields
}
Instrumented decorates an authenticator with failure-isolated telemetry.
func NewInstrumented ¶
func NewInstrumented(authenticator Authenticator, instrumenter Instrumenter, clock Clock) (*Instrumented, error)
NewInstrumented creates an authentication instrumentation decorator.
func (*Instrumented) Authenticate ¶
func (i *Instrumented) Authenticate(ctx context.Context, credential Credential) (Result, error)
Authenticate reports bounded outcome metadata without changing the wrapped authenticator's result or error.
type Instrumenter ¶
type Instrumenter interface {
Start(context.Context, CredentialKind) (context.Context, func(Event))
}
Instrumenter starts instrumentation for one authentication attempt. Implementations must not derive attributes from credential contents.
type Outcome ¶
type Outcome string
Outcome is the bounded authentication outcome reported to instrumentation.
type Principal ¶
type Principal struct {
// contains filtered or unexported fields
}
Principal is an immutable authenticated identity or the explicit anonymous identity. Its zero value is anonymous.
func AnonymousPrincipal ¶
func AnonymousPrincipal() Principal
AnonymousPrincipal returns the explicit anonymous identity.
func NewPrincipal ¶
func NewPrincipal(spec PrincipalSpec) (Principal, error)
NewPrincipal validates and copies authenticated identity data.
func PrincipalFromContext ¶
PrincipalFromContext retrieves a principal stored by ContextWithPrincipal.
func (Principal) AuthenticatedAt ¶
AuthenticatedAt returns the time at which the identity was authenticated.
func (Principal) IsAnonymous ¶
IsAnonymous reports whether p represents absence of an authenticated identity.
func (Principal) Scopes ¶
Scopes returns a copy of the scopes asserted by the credential. Scopes are authentication data and are not an authorization decision.
func (Principal) TenantHints ¶
TenantHints returns a copy of non-authoritative tenant hints.
type PrincipalSpec ¶
type PrincipalSpec struct {
Subject string
Method string
Issuer string
Audiences []string
TenantHints []string
Scopes []string
Claims map[string]any
AuthenticatedAt time.Time
}
PrincipalSpec contains the identity data used to construct a Principal. Callers may reuse or mutate its slices and maps after NewPrincipal returns.
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
Result is the outcome of successful authentication policy evaluation.
func AnonymousResult ¶
func AnonymousResult() Result
AnonymousResult creates an explicit anonymous result for optional routes.
func NewAuthenticatedResult ¶
NewAuthenticatedResult creates a result for a concrete authenticated identity.
type ResultState ¶
type ResultState string
ResultState identifies whether authentication established an identity or an explicitly permitted anonymous state.
const ( ResultAuthenticated ResultState = "authenticated" ResultAnonymous ResultState = "anonymous" )
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package apikey provides static and callback API-key authenticators.
|
Package apikey provides static and callback API-key authenticators. |
|
Package authhttp provides strict HTTP credential extraction, challenges, and authentication-only middleware for net/http.
|
Package authhttp provides strict HTTP credential extraction, challenges, and authentication-only middleware for net/http. |
|
Package authlog adapts authentication instrumentation to log/slog.
|
Package authlog adapts authentication instrumentation to log/slog. |
|
authotel
module
|
|
|
Package authtest provides deterministic authentication fixtures and assertions.
|
Package authtest provides deterministic authentication fixtures and assertions. |
|
Package basic provides Basic credential authenticators.
|
Package basic provides Basic credential authenticators. |
|
Package bearer provides validation adapters for opaque bearer tokens.
|
Package bearer provides validation adapters for opaque bearer tokens. |
|
jwt
module
|
|
|
oidc
module
|