Documentation
¶
Overview ¶
Package apikey provides stable API key authentication middleware.
The middleware extracts credentials from Authorization: ApiKey <secret> or X-API-Key, delegates verification to an application-owned Verifier, stores the authenticated principal in request context, and can enforce required scopes. Storage, hashing, rotation, and last-used tracking intentionally belong to the Verifier implementation instead of the core middleware.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RequireScopeMiddleware ¶
RequireScopeMiddleware requires an authenticated API key principal with scope.
Types ¶
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
Middleware authenticates requests with API keys.
func NewMiddleware ¶
func NewMiddleware(cfg Config) (*Middleware, error)
NewMiddleware constructs API key middleware.
func (*Middleware) Handler ¶
func (m *Middleware) Handler(next http.Handler) http.Handler
Handler requires a valid API key before calling next.
func (*Middleware) OptionalHandler ¶
func (m *Middleware) OptionalHandler(next http.Handler) http.Handler
OptionalHandler authenticates a valid API key when present and otherwise lets anonymous requests continue. Malformed or invalid credentials still fail.
type PresentedKey ¶
PresentedKey describes a credential extracted from a request.
type Principal ¶
type Principal struct {
ID string
Name string
TenantID string
Scopes []string
Metadata map[string]any
}
Principal describes the authenticated API key owner.
func PrincipalFromContext ¶
PrincipalFromContext returns the authenticated API key principal.
type Verifier ¶
type Verifier interface {
VerifyAPIKey(ctx context.Context, key PresentedKey) (Principal, error)
}
Verifier validates a presented API key and returns its principal.
type VerifierFunc ¶
type VerifierFunc func(ctx context.Context, key PresentedKey) (Principal, error)
VerifierFunc adapts a function to the Verifier interface.
func (VerifierFunc) VerifyAPIKey ¶
func (f VerifierFunc) VerifyAPIKey(ctx context.Context, key PresentedKey) (Principal, error)
VerifyAPIKey validates a presented API key.