jwt

package
v0.36.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 9 Imported by: 3 modules, 2 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 provides helpers for signing and verifying RS256 JSON Web Tokens on top of github.com/golang-jwt/jwt/v5, including support for key rotation via deprecated verification keys.

A Key pairs an RSA public key with an optional private key and an identifier that is written to and read from the token's "kid" header. JWT signs tokens with its current key and verifies them using DefaultKeyFunc unless a custom key function is supplied:

key, deprecated, err := jwt.NewKeysFromFilenames("public.pem", "private.pem", nil)
j := jwt.New(key, jwt.WithDeprecatedKeys(deprecated...))
token, err := j.GetSignedToken(jwt.NewRegisteredClaimsWithLifetime(time.Hour))

Index

Constants

This section is empty.

Variables

View Source
var MaxTimeDifferenceBetweenNodes = 30 * time.Second

MaxTimeDifferenceBetweenNodes represents an offset that should be taken into account when creating e.g. jwt tokens with the `notBefore` flag. It is the default offset used by NewRegisteredClaims.

Functions

func DefaultKeyFunc

func DefaultKeyFunc(key Key, deprecatedKeys map[string]Key) jwt.Keyfunc

DefaultKeyFunc returns a jwt.Keyfunc that accepts only RS256 tokens and selects the public key by the token's "kid" header, looking it up in deprecatedKeys first and then comparing it to key.ID. It returns an error for other signing methods and for missing, non-string or unknown key IDs.

func NewKeysFromFilenames

func NewKeysFromFilenames(publicKeyPemFilename, privateKeyPemFilename string, deprecatedPublicKeyPemFilenames []string) (Key, []Key, error)

NewKeysFromFilenames loads the current key via NewKeyFromFilenames and the deprecated keys via NewDeprecatedKeysFromFilenames.

func NewRegisteredClaims added in v0.23.0

func NewRegisteredClaims(opts ...RegisteredClaimsOption) jwt.RegisteredClaims

NewRegisteredClaims returns jwt.RegisteredClaims with IssuedAt and NotBefore set to the current time minus the offset. The offset accounts for clock differences between nodes in a distributed system; offsets under one millisecond are ignored. If no WithOffset option is provided, MaxTimeDifferenceBetweenNodes is used.

func NewRegisteredClaimsWithLifetime added in v0.23.0

func NewRegisteredClaimsWithLifetime(lifetime time.Duration, opts ...RegisteredClaimsOption) jwt.RegisteredClaims

NewRegisteredClaimsWithLifetime returns claims as created by NewRegisteredClaims with ExpiresAt set to IssuedAt plus lifetime. Since IssuedAt is shifted back by the offset, the effective remaining lifetime is lifetime minus the offset.

func NewStandardClaims deprecated

func NewStandardClaims() jwt.RegisteredClaims

NewStandardClaims returns registered claims using MaxTimeDifferenceBetweenNodes as offset.

Deprecated: Use NewRegisteredClaims instead.

func NewStandardClaimsWithLifetime deprecated

func NewStandardClaimsWithLifetime(lifetime time.Duration) jwt.RegisteredClaims

NewStandardClaimsWithLifetime returns registered claims expiring after lifetime, using MaxTimeDifferenceBetweenNodes as offset.

Deprecated: Use NewRegisteredClaimsWithLifetime instead.

Types

type JWT

type JWT struct {
	// Key is the current key used for signing and verification.
	Key Key
	// KeyFunc resolves the verification key for a parsed token.
	KeyFunc jwt.Keyfunc
	// DeprecatedKeys holds verification-only keys indexed by key ID,
	// e.g. kept after a key rotation.
	DeprecatedKeys map[string]Key
}

JWT signs and parses RS256 tokens. Create it with New.

func New

func New(key Key, opts ...Option) *JWT

New returns a JWT that signs with key. If no key function is set via WithKeyFun, DefaultKeyFunc is used with key and any keys added via WithDeprecatedKeys. Nil options are ignored.

func (*JWT) GetSignedToken

func (j *JWT) GetSignedToken(claims jwt.Claims) (string, error)

GetSignedToken returns claims as a token signed with RS256 using the private part of JWT.Key, with the "kid" header set to its ID. It returns an error if signing fails, e.g. when no private key is set.

func (*JWT) ParseWithClaims

func (j *JWT) ParseWithClaims(token string, claims jwt.Claims) (*jwt.Token, error)

ParseWithClaims parses and validates token into claims, resolving the verification key with JWT.KeyFunc.

type Key

type Key struct {
	// ID (required) is the key identifier used as the token's "kid" header,
	// e.g. a hash of the public key.
	ID string
	// Public (required) is the RSA public key used for verification.
	Public *rsa.PublicKey
	// Private (optional) is the RSA private key used for signing.
	Private *rsa.PrivateKey
}

Key is an RSA key pair identified by ID.

func NewDeprecatedKeysFromFilenames

func NewDeprecatedKeysFromFilenames(publicKeyPemFilenames []string) ([]Key, error)

NewDeprecatedKeysFromFilenames loads public-only keys from the given PEM files, for use with WithDeprecatedKeys. It returns the first load error.

func NewKey

func NewKey(id string, public *rsa.PublicKey, private *rsa.PrivateKey) Key

NewKey returns a Key with the given ID and keys.

func NewKeyFromFilenames

func NewKeyFromFilenames(publicKeyPemFilename, privateKeyPemFilename string) (Key, error)

NewKeyFromFilenames loads a Key from PEM files. The private key is optional and skipped if privateKeyPemFilename is empty. Literal \n escape sequences in the file contents are converted to newlines before parsing. The key ID is the hex-encoded SHA-256 of the trimmed public key file contents. It returns an error if a file cannot be read or parsed.

type Option

type Option func(*JWT)

Option configures a JWT.

func WithDeprecatedKeys

func WithDeprecatedKeys(v ...Key) Option

WithDeprecatedKeys adds keys that are still accepted for verification, indexed by their Key.ID.

func WithKeyFun

func WithKeyFun(v jwt.Keyfunc) Option

WithKeyFun sets the key function used to verify tokens. Defaults to DefaultKeyFunc over the current and deprecated keys.

type RegisteredClaimsOption added in v0.23.0

type RegisteredClaimsOption func(*registeredClaimsOptions)

RegisteredClaimsOption configures how NewRegisteredClaims creates claims.

func WithOffset added in v0.23.0

func WithOffset(offset time.Duration) RegisteredClaimsOption

WithOffset sets the offset subtracted from the current time to account for clock differences between nodes. Defaults to MaxTimeDifferenceBetweenNodes.

Jump to

Keyboard shortcuts

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