jwt

package
v0.32.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: Apache-2.0, BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package jwt implements CEL extension functions for JSON Web Token (JWT) parsing, claims inspection, and validation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Library

func Library(options ...Option) cel.EnvOption

Library returns a cel.EnvOption to configure extended functions for JWT data handling and claims inspection.

Types

type Option

type Option func(*jwtLib) *jwtLib

Option declares a functional operator for configuring JWT extension library behavior.

func Clock

func Clock(nowFunc func() time.Time) Option

Clock sets a custom clock function for time validation (defaults to time.Now).

func ClockLeeway

func ClockLeeway(leeway time.Duration) Option

ClockLeeway sets the tolerance window when checking token time claims (iat, nbf, exp).

func ValidateTimes

func ValidateTimes(leeway ...time.Duration) Option

ValidateTimes enables automatic time validation (iat, nbf, exp) during token parsing with an optional clock leeway.

func Version

func Version(version uint32) Option

Version sets the library version for JWT extensions.

type Token

type Token struct {
	// Standard claims
	Issuer          string    `json:"iss" cel:"issuer"`
	Subject         string    `json:"sub" cel:"subject"`
	Audience        []string  `json:"aud" cel:"aud"`
	AuthorizedParty string    `json:"azp,omitempty" cel:"azp"`
	ExpiresAt       time.Time `json:"exp" cel:"exp"`
	NotBefore       time.Time `json:"nbf" cel:"nbf"`
	IssuedAt        time.Time `json:"iat" cel:"iat"`
	ID              string    `json:"jti,omitempty" cel:"id"`

	// Header derived fields
	Algorithm string `json:"alg" cel:"alg"`
	KeyID     string `json:"kid" cel:"keyId"`

	// Raw JSON payload associated with the token including custom claims.
	// Must be treated as read-only once initialized.
	Payload map[string]any `json:"-" cel:"-"`
}

Token represents a parsed JWT token using Go native struct types. A Token instance and its associated Payload map MUST be treated as immutable once parsed or created.

func NewToken

func NewToken(header, payload map[string]any) (*Token, error)

NewToken generates a `jwt.Token` instance from the JSON-decoded header and payload of a JWT.

Signature validation of the token must be performed before passing the token to CEL. It is recommended that `IsValidAt` and `PresentedBy` are checked after creation of the token to ensure the token matches core content assumptions.

func ParseToken

func ParseToken(tokenStr string) (*Token, error)

ParseToken parses a JWT token string into a structured Token. Verification of the token must be performed before passing the token to CEL.

func (*Token) Claim

func (t *Token) Claim(adapter types.Adapter, claimName string) ref.Val

Claim queries a claim value by key name using the provided types.Adapter, returning an optional dyn value.

func (*Token) IsValidAt

func (t *Token) IsValidAt(refTime time.Time, leeway time.Duration) bool

IsValidAt checks whether the token time claims (iat, nbf, exp) are valid at the given reference time with clock leeway tolerance.

func (*Token) PresentedBy

func (t *Token) PresentedBy(issuer, presenter string) bool

PresentedBy determines whether the token from the given issuer was presented by the expected authorized party (`azp`) or audience (`aud`). If the token contains an `azp` claim, it is checked against the `presenter`. Otherwise, the `aud` claim is checked.

Jump to

Keyboard shortcuts

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