Documentation
¶
Overview ¶
Package apikey provides static and callback API-key authenticators.
Index ¶
Examples ¶
Constants ¶
const MaxEntries = 256
MaxEntries bounds active key candidates and per-request comparison work.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Authenticator ¶
type Authenticator struct {
// contains filtered or unexported fields
}
Authenticator validates API-key credentials through a callback or interface.
func New ¶
func New(validator Validator, options ...Option) (*Authenticator, error)
New creates a callback API-key authenticator.
func (*Authenticator) Authenticate ¶
func (a *Authenticator) Authenticate(ctx context.Context, credential authentication.Credential) (authentication.Result, error)
Authenticate validates one bounded API-key credential.
type Entry ¶
type Entry struct {
ID string
Key string
Principal authentication.PrincipalSpec
}
Entry configures one active key with a deterministic non-secret identifier.
type Option ¶
type Option func(*config)
Option configures an Authenticator.
func WithMaxKeyBytes ¶
WithMaxKeyBytes sets the inclusive key size bound.
func WithMaxKeyIDBytes ¶
WithMaxKeyIDBytes sets the inclusive key-ID size bound.
type Static ¶
type Static struct {
// contains filtered or unexported fields
}
Static validates API keys against an atomically replaceable bounded key set.
func (*Static) Authenticate ¶
func (s *Static) Authenticate(ctx context.Context, credential authentication.Credential) (authentication.Result, error)
Authenticate validates one API key against a single immutable key-set snapshot.
func (*Static) Replace ¶
Replace atomically replaces all active keys after validating the complete candidate set. A failed replacement leaves the previous set active.
Example ¶
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: "previous", Key: "old-secret",
Principal: authentication.PrincipalSpec{Subject: "service"},
}})
_ = authenticator.Replace([]apikey.Entry{
{ID: "current", Key: "new-secret", Principal: authentication.PrincipalSpec{Subject: "service"}},
{ID: "previous", Key: "old-secret", Principal: authentication.PrincipalSpec{Subject: "service"}},
})
result, err := authenticator.Authenticate(
context.Background(),
authentication.NewAPIKeyCredential("current", "new-secret"),
)
principal, authenticated := result.Principal()
fmt.Println(err, authenticated, principal.Subject())
}
Output: <nil> true service
type Validator ¶
type Validator interface {
ValidateAPIKey(context.Context, string, string) (authentication.Principal, error)
}
Validator validates an API key and returns an immutable principal.
type ValidatorFunc ¶
ValidatorFunc adapts a function to Validator.
func (ValidatorFunc) ValidateAPIKey ¶
func (f ValidatorFunc) ValidateAPIKey(ctx context.Context, keyID, key string) (authentication.Principal, error)
ValidateAPIKey calls f.