jwks

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package jwks provides a JWKS (JSON Web Key Set) key source for validating JWTs issued by external identity providers such as Auth0, AWS Cognito, Google, or any OIDC-compliant provider.

The Source fetches public keys from a JWKS endpoint, caches them by key ID (kid), and automatically refreshes when an unknown kid is encountered — handling key rotation transparently.

Auth0 quick start

import (
    jwtpkg "github.com/KriaaCompany/api-security-sdk/auth/jwt"
    "github.com/KriaaCompany/api-security-sdk/auth/jwks"
)

src := jwks.Auth0("myapp.auth0.com")
svc := jwtpkg.New(jwtpkg.WithJWKS(src.KeyFunc))

claims, err := svc.Verify(tokenFromAuth0)

Generic OIDC provider

src := jwks.NewSource("https://accounts.google.com/.well-known/openid-configuration/jwks")
svc := jwtpkg.New(jwtpkg.WithJWKS(src.KeyFunc))

Index

Constants

This section is empty.

Variables

View Source
var ErrKIDNotFound = errors.New("jwks: no key found for kid")

ErrKIDNotFound is returned when the token's kid does not match any key in the JWKS after a fresh fetch.

View Source
var ErrUnsupportedKeyType = errors.New("jwks: unsupported key type (only RSA and EC supported)")

ErrUnsupportedKeyType is returned for JWK entries with an unsupported kty.

Functions

This section is empty.

Types

type Option

type Option func(*Source)

Option configures a Source.

func WithCacheTTL

func WithCacheTTL(d time.Duration) Option

WithCacheTTL sets how long fetched keys are cached before the next background refresh. Default: 15 minutes.

func WithHTTPClient

func WithHTTPClient(c *http.Client) Option

WithHTTPClient replaces the default HTTP client used to fetch the JWKS. Use this to set custom timeouts, TLS config, or a proxy.

func WithMinRefreshDelay

func WithMinRefreshDelay(d time.Duration) Option

WithMinRefreshDelay sets the minimum time between forced key-rotation refreshes (triggered by an unknown kid). Default: 30 seconds.

type Source

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

Source fetches and caches public keys from a JWKS endpoint.

func Auth0

func Auth0(domain string, opts ...Option) *Source

Auth0 creates a Source for an Auth0 tenant.

src := jwks.Auth0("myapp.auth0.com")

The domain may optionally include the "https://" scheme prefix; it is normalised automatically.

func NewSource

func NewSource(jwksURL string, opts ...Option) *Source

NewSource creates a Source that fetches keys from the given JWKS URL. Keys are fetched lazily on the first call to KeyFunc.

func (*Source) KeyFunc

func (s *Source) KeyFunc(token *gojwt.Token) (any, error)

KeyFunc is a github.com/golang-jwt/jwt/v5-compatible Keyfunc. Pass it directly to jwtpkg.WithJWKS:

svc := jwtpkg.New(jwtpkg.WithJWKS(src.KeyFunc))

func (*Source) Preload

func (s *Source) Preload() error

Preload fetches the JWKS immediately. Call this at startup to fail fast if the endpoint is unreachable, rather than failing on the first request.

Jump to

Keyboard shortcuts

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