jwt

package
v2.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 19 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package jwt issues and verifies JWT access tokens (RFC 7519, RFC 9068) using only the standard library.

Only two algorithms exist: HS256 and EdDSA (Ed25519). Every key is pinned to one algorithm through keys.Key.Alg, and the verifier looks the key up by "kid" and requires the token's "alg" to match it. "none", RSA and ECDSA are never accepted, which rules out algorithm-confusion attacks by construction.

Index

Constants

View Source
const (
	// DefaultTTL is the access-token lifetime when Config.TTL is zero.
	DefaultTTL = 10 * time.Minute

	// DefaultLeeway is the clock-skew allowance when Config.Leeway is zero.
	DefaultLeeway = 30 * time.Second

	// MaxLeeway caps Config.Leeway.
	MaxLeeway = 2 * time.Minute

	// MaxTokenSize is the largest token the verifier will parse.
	MaxTokenSize = 8 << 10

	// TokenType is the "typ" header for access tokens (RFC 9068).
	TokenType = "at+jwt"
)

Variables

This section is empty.

Functions

func JWKSHandler added in v2.2.0

func JWKSHandler(kp keys.Provider, maxAge time.Duration) http.Handler

JWKSHandler serves PublicJWKS(kp) as application/jwk-set+json, for other services to verify access tokens without being able to mint them. The set is built on every request, so rotation shows up at once; maxAge sets Cache-Control (zero means 5 minutes). Keep a rotated-out key until every verifier's cache has seen the new one: prune after at least maxAge + the access-token TTL.

Types

type Config

type Config struct {
	Issuer   string        // "iss"; required
	Audience string        // "aud"; required
	TTL      time.Duration // issuer only; DefaultTTL if zero
	Leeway   time.Duration // verifier only; DefaultLeeway if zero, at most MaxLeeway

	// Now returns the current time. Nil means time.Now. Useful in tests.
	Now func() time.Time
}

Config holds the settings shared by Issuer and Verifier.

type Issuer

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

Issuer issues JWT access tokens signed with the provider's active key.

The active key is read on every call, so rotating the provider takes effect for the next token issued.

func NewIssuer

func NewIssuer(kp keys.Provider, cfg Config) (*Issuer, error)

NewIssuer creates a JWT issuer. The provider's current active key must be valid for signing.

func (*Issuer) Issue

func (i *Issuer) Issue(
	ctx context.Context,
	claims token.Claims,
) (string, error)

Issue implements token.Issuer.

type JWK added in v2.2.0

type JWK struct {
	Kty string `json:"kty"`           // "OKP"
	Crv string `json:"crv"`           // "Ed25519"
	X   string `json:"x"`             // base64url public key
	Kid string `json:"kid"`           // key ID, as in the token header
	Alg string `json:"alg,omitempty"` // "EdDSA"
	Use string `json:"use,omitempty"` // "sig"
}

JWK is a public JSON Web Key (RFC 7517) for an Ed25519 key (RFC 8037).

type JWKS added in v2.2.0

type JWKS struct {
	Keys []JWK `json:"keys"`
}

JWKS is a JSON Web Key Set.

func PublicJWKS added in v2.2.0

func PublicJWKS(kp keys.Provider) JWKS

PublicJWKS returns the public keys of kp's EdDSA verification keys, active key first. HS256 secrets and private keys are never included.

type RemoteKeys added in v2.2.0

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

RemoteKeys is a verification-only keys.Provider backed by a remote JWKS, typically another service's JWKSHandler. Pass it to NewVerifier.

It accepts only Ed25519 keys (kty OKP, alg EdDSA or absent, use sig or absent); every key stays pinned to EdDSA. ActiveKey returns the zero Key, so RemoteKeys cannot be used to issue tokens. A token with an unknown key ID triggers a fetch, at most once per MinRefreshInterval; the cached set is also refreshed in the background once it is older than RefreshInterval. A failed fetch keeps the previous set.

func NewRemoteKeys added in v2.2.0

func NewRemoteKeys(ctx context.Context, jwksURL string, cfg RemoteKeysConfig) (*RemoteKeys, error)

NewRemoteKeys fetches the key set at jwksURL once, and fails if that fetch fails, so a misconfigured URL is noticed at start-up.

func (*RemoteKeys) ActiveKey added in v2.2.0

func (r *RemoteKeys) ActiveKey() keys.Key

ActiveKey implements keys.Provider. It returns the zero Key: remote keys verify, they never sign.

func (*RemoteKeys) FindKey added in v2.2.0

func (r *RemoteKeys) FindKey(id string) (keys.Key, bool)

FindKey implements keys.Finder: an unknown key ID causes a fetch (rate limited), since the issuer may have rotated.

func (*RemoteKeys) Refresh added in v2.2.0

func (r *RemoteKeys) Refresh(ctx context.Context) error

Refresh fetches the key set now. On failure the previous set is kept.

func (*RemoteKeys) VerificationKeys added in v2.2.0

func (r *RemoteKeys) VerificationKeys() []keys.Key

VerificationKeys implements keys.Provider.

type RemoteKeysConfig added in v2.2.0

type RemoteKeysConfig struct {
	// HTTPClient fetches the set. Default: a client with a 10-second timeout.
	HTTPClient *http.Client

	// RefreshInterval is how old the cached set may get before it is
	// fetched again in the background. Default 15 minutes.
	RefreshInterval time.Duration

	// MinRefreshInterval limits fetches caused by unknown key IDs, so
	// tokens with made-up IDs cannot make the verifier hammer the issuer.
	// Default 1 minute.
	MinRefreshInterval time.Duration

	// AllowHTTP permits a plain-HTTP URL. Only for tests and local
	// development: keys fetched over HTTP can be replaced in transit.
	AllowHTTP bool
}

RemoteKeysConfig configures RemoteKeys.

type Verifier

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

Verifier verifies JWT access tokens.

Checks, in order: size and shape, header ("typ" must be at+jwt, "kid" must name a known key, "alg" must equal that key's pinned algorithm), signature, then claims ("iss", "aud", "exp", "nbf", "iat" with leeway, non-empty "sub").

func NewVerifier

func NewVerifier(kp keys.Provider, cfg Config) (*Verifier, error)

NewVerifier creates a JWT verifier.

func (*Verifier) Verify

func (v *Verifier) Verify(
	ctx context.Context,
	accessToken string,
) (*token.Claims, error)

Verify implements token.Verifier.

Jump to

Keyboard shortcuts

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