Help us shape the future of Go! Take the 2026 Go Developer Survey

keyauth

package
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Key authentication errors distinguish missing from rejected credentials.
	ErrKeyMissing = errors.New("keyauth: key missing")
	ErrKeyInvalid = errors.New("keyauth: key invalid")
)

Functions

func New

func New(configs ...Config) zinc.Middleware

New authenticates requests with an API key. Config.Validator is required; state is published only after successful validation.

Types

type Config

type Config struct {
	Extractor      Extractor
	Validator      Validator
	SuccessHandler zinc.HandlerFunc
	ErrorHandler   func(*zinc.Context, error) error
}

Config controls key extraction, validation, and failure handling.

type Credentials

type Credentials struct {
	Key    string
	Source Source
}

Credentials contains a key supplied for validation.

type Extractor

type Extractor func(*zinc.Context) (Credentials, error)

Extractor reads key credentials from a request.

func FromAuthorizationHeader

func FromAuthorizationHeader() Extractor

FromAuthorizationHeader extracts a Bearer key.

func FromCookie

func FromCookie(name string) Extractor

FromCookie extracts a key from a cookie.

func FromFirst

func FromFirst(extractors ...Extractor) Extractor

FromFirst falls back only for missing credentials.

func FromHeader

func FromHeader(header string) Extractor

FromHeader extracts an unprefixed key from header.

func FromHeaderPrefix

func FromHeaderPrefix(header, prefix string) Extractor

FromHeaderPrefix extracts a key after a case-insensitive prefix.

func FromQuery

func FromQuery(name string) Extractor

FromQuery extracts a key from a query parameter.

type Source

type Source string

Source identifies where a key was extracted.

const (
	SourceAuthorizationHeader Source = "authorization_header"
	SourceHeader              Source = "header"
	SourceQuery               Source = "query"
	SourceCookie              Source = "cookie"
)

type State

type State struct {
	Key    string
	Source Source
}

State is the authenticated key state stored on the context.

func Get

func Get(c *zinc.Context) (State, bool)

Get returns authenticated key state.

func MustGet

func MustGet(c *zinc.Context) State

MustGet returns key state or panics when absent.

type Validator

type Validator func(*zinc.Context, Credentials) (bool, error)

Validator verifies extracted key credentials.

func Static

func Static(key string) Validator

Static returns a constant-time validator for one key.

func StaticKeys

func StaticKeys(keys ...string) Validator

StaticKeys returns a constant-time validator for a fixed key set.

Jump to

Keyboard shortcuts

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