apikey

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package apikey provides static and callback API-key authenticators.

Index

Examples

Constants

View Source
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

func WithMaxKeyBytes(maximum int) Option

WithMaxKeyBytes sets the inclusive key size bound.

func WithMaxKeyIDBytes

func WithMaxKeyIDBytes(maximum int) Option

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 NewStatic

func NewStatic(entries []Entry) (*Static, error)

NewStatic validates and copies the initial active 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

func (s *Static) Replace(entries []Entry) error

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

type ValidatorFunc func(context.Context, string, string) (authentication.Principal, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL