jwks

package
v1.3.12 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package jwks provides a production-ready JWKS (JSON Web Key Set) key function for validating JWTs.

The keyfunc wraps the MicahParks/keyfunc library with built-in periodic key refresh, rate-limiting of unknown-KID refresh attempts, and structured logging. It is safe for concurrent use and suitable for use as a jwt.Keyfunc in JWT middleware.

Basic usage:

kf, err := jwks.New(ctx, []string{"https://auth.example.com/.well-known/jwks.json"})
if err != nil {
    return err
}

jwtConfig := middleware.JWTConfig{
    Keyfunc: kf.Keyfunc,
}
e.Use(middleware.JWT(jwtConfig))

The keyfunc automatically refreshes keys in the background. If a JWT references an unknown key ID, it triggers an immediate refresh but rate-limits rapid refresh attempts to prevent thundering herd.

Index

Constants

View Source
const (
	DefaultRateLimitBurst  = 5
	DefaultRefreshTimeout  = 10 * time.Second
	DefaultRefreshInterval = 60 * time.Minute
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

type KeyFunc

type KeyFunc struct {
	// contains filtered or unexported fields
}

func New

func New(ctx context.Context, urls []string, opts ...Option) (*KeyFunc, error)

func (*KeyFunc) Keyfunc added in v1.3.5

func (k *KeyFunc) Keyfunc(token *jwt.Token) (any, error)

func (*KeyFunc) KeyfuncCtx added in v1.3.5

func (k *KeyFunc) KeyfuncCtx(ctx context.Context) jwt.Keyfunc

func (*KeyFunc) Storage added in v1.3.5

func (k *KeyFunc) Storage() jwkset.Storage

func (*KeyFunc) VerificationKeySet added in v1.3.5

func (k *KeyFunc) VerificationKeySet(ctx context.Context) (jwt.VerificationKeySet, error)

type Option

type Option func(*Config)

func WithHTTPClient

func WithHTTPClient(client *http.Client) Option

func WithRateLimitBurst

func WithRateLimitBurst(burst int) Option

func WithRateLimitWaitMax

func WithRateLimitWaitMax(maxWait time.Duration) Option

func WithRefreshInterval

func WithRefreshInterval(interval time.Duration) Option

func WithRefreshTimeout

func WithRefreshTimeout(timeout time.Duration) Option

func WithValidationSkipAll

func WithValidationSkipAll(skip bool) Option

Jump to

Keyboard shortcuts

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