jwt

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 8 Imported by: ? modules, 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 authenticates signed JSON Web Tokens (RFC 7519) as authentication.Credential values of kind authentication.CredentialKindJWT.

A Verifier is strict by construction: the issuer, at least one audience, the accepted signature algorithms, a key source and a maximum token lifetime are required; "none" is never accepted; every token must carry a "kid" header, "iss", "sub", "aud", "iat" and "exp"; and "exp - iat" must not exceed Options.MaxLifetime. Options.ClockSkew is applied symmetrically to "exp", "nbf" and "iat". Every failure is reported as authentication.ErrUnauthenticated without token material.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClaimsMapper

type ClaimsMapper func(context.Context, jwt.MapClaims) (authentication.Principal, error)

ClaimsMapper maps verified JWT claims to a protocol-neutral principal. It is called only after the signature and all registered claims have been verified. The returned principal is validated with authentication.ValidatePrincipal (honouring Options.ClockSkew).

type KeySource

type KeySource interface {
	Key(context.Context, string) (any, error)
}

KeySource returns the verification key for a validated JWT key ID. The returned key must match the token's algorithm, for example []byte for HMAC, *rsa.PublicKey for RS256 or ed25519.PublicKey for EdDSA. Errors are never shown to callers of Verifier.Authenticate.

type Options

type Options struct {
	// Issuer is the exact required "iss" claim. Required.
	Issuer string
	// Audiences lists accepted "aud" values; a token must name at least one.
	// Required, without duplicates.
	Audiences []string
	// Algorithms lists accepted JWS "alg" values, for example "RS256" or
	// "EdDSA". Required, without duplicates. "none" is always rejected.
	Algorithms []string
	// Keys resolves the verification key for a token's "kid". Required.
	Keys KeySource
	// ClaimsMapper maps verified claims to a principal. The default maps
	// "iss" and "sub" to ID "<iss>|<sub>", "iat" and "exp" to the lifetime,
	// and sets Methods to ["jwt"].
	ClaimsMapper ClaimsMapper
	// Clock returns the verification time; its result is converted to UTC.
	// Defaults to time.Now.
	Clock func() time.Time
	// ClockSkew is the tolerated clock difference between issuer and
	// verifier. Zero means no tolerance.
	ClockSkew time.Duration
	// MaxTokenBytes bounds the accepted token size. Defaults to 32 KiB.
	MaxTokenBytes int
	// MaxLifetime bounds "exp - iat". Required and positive.
	MaxLifetime time.Duration
}

Options configures a signed JWT verifier.

type Verifier

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

Verifier authenticates signed JWT credentials. It is safe for concurrent use; its KeySource and ClaimsMapper must be too.

func NewVerifier

func NewVerifier(options Options) (*Verifier, error)

NewVerifier returns a strict signed JWT verifier. It returns authentication.ErrInvalidCredential when options are incomplete or invalid.

func (*Verifier) Authenticate

func (v *Verifier) Authenticate(ctx context.Context, credential authentication.Credential) (authentication.Principal, error)

Authenticate verifies a JWT credential and returns the mapped principal. Credentials of another kind, oversized or invalid tokens, and every verification failure return authentication.ErrUnauthenticated, so a Verifier belongs last in an authentication.Chain.

Jump to

Keyboard shortcuts

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