jwt

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2026 License: MIT Imports: 14 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

This section is empty.

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 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