jwtutils

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: Apache-2.0, MIT Imports: 10 Imported by: 0

Documentation

Overview

Package jwtutils signs and verifies JWTs with typed private claims.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingKey    = errors.New("jwtutils: signing key is required")
	ErrMissingKeySet = errors.New("jwtutils: verification key set is empty")
	// ErrReservedClaim reports a private claim that uses a registered name.
	ErrReservedClaim = errors.New("jwtutils: private claim collides with a registered claim")
	// ErrWeakHMACKey reports an HMAC key shorter than the algorithm minimum.
	ErrWeakHMACKey = errors.New("jwtutils: HMAC key too short")
)

Errors returned by the signer and verifier.

Functions

This section is empty.

Types

type CachedKeyProvider

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

CachedKeyProvider caches keys for a fixed time.

func NewCachedKeyProvider

func NewCachedKeyProvider(inner KeyProvider, ttl time.Duration) *CachedKeyProvider

NewCachedKeyProvider builds a key cache with the given lifetime.

func (*CachedKeyProvider) Invalidate

func (c *CachedKeyProvider) Invalidate()

Invalidate clears both cached entries.

func (*CachedKeyProvider) SignKey

func (c *CachedKeyProvider) SignKey(ctx context.Context) (jwk.Key, error)

SignKey returns the cached signing key, refreshing it when expired.

func (*CachedKeyProvider) VerifyKeySet

func (c *CachedKeyProvider) VerifyKeySet(ctx context.Context) (jwk.Set, error)

VerifyKeySet returns the cached public key set under the same TTL.

func (*CachedKeyProvider) WithClock

func (c *CachedKeyProvider) WithClock(now func() time.Time) *CachedKeyProvider

WithClock overrides the time source (tests).

type KeyProvider

type KeyProvider interface {
	// SignKey returns the current signing key.
	SignKey(ctx context.Context) (jwk.Key, error)
	// VerifyKeySet returns the published public keys.
	VerifyKeySet(ctx context.Context) (jwk.Set, error)
}

KeyProvider supplies the signing key and public verification keys.

type PrivateClaimDecoder

type PrivateClaimDecoder interface {
	DecodePrivateClaims(params map[string]any) error
}

PrivateClaimDecoder customizes reconstruction of typed private claims.

type Signer

type Signer[T any] struct {
	// contains filtered or unexported fields
}

Signer creates signed JWTs with typed private claims.

func NewSigner

func NewSigner[T any](key jwk.Key, algorithm jwa.SignatureAlgorithm) (*Signer[T], error)

NewSigner builds a signer with the given key and algorithm.

func (*Signer[T]) Sign

func (s *Signer[T]) Sign(claims T, std Standard) (string, error)

Sign creates a compact JWT from the registered and private claims.

func (*Signer[T]) WithAudience

func (s *Signer[T]) WithAudience(audience ...string) *Signer[T]

WithAudience sets the default audience claim.

func (*Signer[T]) WithIssuer

func (s *Signer[T]) WithIssuer(issuer string) *Signer[T]

WithIssuer sets the default issuer claim.

func (*Signer[T]) WithTTL

func (s *Signer[T]) WithTTL(ttl time.Duration) *Signer[T]

WithTTL sets the default token lifetime.

type Standard

type Standard struct {
	Issuer    string
	Subject   string
	Audience  []string
	ExpiresAt time.Time
	IssuedAt  time.Time
	NotBefore time.Time
	JWTID     string
}

Standard carries the registered claims managed by the Signer and surfaced by the Verifier.

type Verified

type Verified[T any] struct {
	Standard
	Private T
}

Verified contains the registered and private claims of a checked token.

type Verifier

type Verifier[T any] struct {
	// contains filtered or unexported fields
}

Verifier checks JWTs and decodes their typed private claims.

func NewVerifier

func NewVerifier[T any](key jwk.Key, algorithm jwa.SignatureAlgorithm) (*Verifier[T], error)

NewVerifier builds a verifier with a key and signature algorithm.

func (*Verifier[T]) Verify

func (v *Verifier[T]) Verify(encoded string) (Verified[T], error)

Verify checks the token and decodes its typed private claims.

func (*Verifier[T]) WithAudience

func (v *Verifier[T]) WithAudience(audience string) *Verifier[T]

WithAudience requires a matching audience claim.

func (*Verifier[T]) WithClockSkew

func (v *Verifier[T]) WithClockSkew(skew time.Duration) *Verifier[T]

WithClockSkew sets the allowed clock difference for time claims.

func (*Verifier[T]) WithIssuer

func (v *Verifier[T]) WithIssuer(issuer string) *Verifier[T]

WithIssuer requires a matching issuer claim.

func (*Verifier[T]) WithKeySet

func (v *Verifier[T]) WithKeySet(set jwk.Set) *Verifier[T]

WithKeySet configures verification with a JWKS key set.

func (*Verifier[T]) WithRequiredClaims

func (v *Verifier[T]) WithRequiredClaims(names ...string) *Verifier[T]

WithRequiredClaims requires the named registered claims.

Jump to

Keyboard shortcuts

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