jwt

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package jwt provides RS256 JWT signing, verification, and JWKS publishing with key rotation support.

RS256 only. The active key in the supplied KeyRing signs new tokens. Verification looks up the key by the mandatory "kid" header. The JWKS endpoint publishes all public keys so third-party services can verify tokens without sharing a secret.

Rotation procedure:

  1. Add new key with Active=false to the key set.
  2. Deploy — all instances now recognize the new key for verification.
  3. Flip the new key to Active=true (and old key to Active=false).
  4. Deploy — new tokens signed with new key, old tokens still verify.
  5. Wait access_token lifetime (15 min) for all old tokens to expire.
  6. Remove old key from set.
  7. Deploy.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateAccessToken

func CreateAccessToken(claims Claims, kr *KeyRing, expiry time.Duration) (string, error)

CreateAccessToken signs a new RS256 access token using the active key in the ring. The token always carries a "kid" header so verifiers can pick the correct public key.

Types

type Claims

type Claims struct {
	Sub       string `json:"sub"`
	Email     string `json:"email"`
	Name      string `json:"name"`
	Role      string `json:"role"`
	Tenant    string `json:"tenant"`
	AvatarURL string `json:"avatar_url"`
	IssuedAt  int64  `json:"iat"`
	ExpiresAt int64  `json:"exp"`
}

Claims holds the fields embedded in an access token.

func VerifyAccessToken

func VerifyAccessToken(tokenStr string, kr *KeyRing, expectedTenant string) (*Claims, error)

VerifyAccessToken verifies an RS256 access token and returns its claims. The token must carry a "kid" header that matches a key in the ring. Tokens without "kid" or with an unknown "kid" are rejected.

If expectedTenant is non-empty, the token's "tenant" claim must match it exactly; otherwise the token is rejected. Passing an empty expectedTenant disables the cross-tenant check (backward-compatible mode).

Tokens with a missing or zero "exp" claim are explicitly rejected: the underlying lestrrat-go jwt library treats an absent exp as "no expiration", which would otherwise produce unbounded-lifetime tokens.

type KeyRing

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

KeyRing manages multiple signing keys for rotation. Exactly one key is "active" and used to sign new tokens. All keys remain available for verification of previously-issued tokens.

func NewKeyRing

func NewKeyRing(keys []SigningKey) (*KeyRing, error)

NewKeyRing creates a KeyRing from the given keys. Exactly one key must be marked Active. If no key is marked Active, the last key in the slice becomes active. If multiple keys are marked Active, an error is returned. An empty slice also returns an error.

func (*KeyRing) Active

func (kr *KeyRing) Active() SigningKey

Active returns the key used to sign new tokens.

func (*KeyRing) AllKIDs

func (kr *KeyRing) AllKIDs() []string

AllKIDs returns the key IDs of every key in the ring, in insertion order.

func (*KeyRing) Get

func (kr *KeyRing) Get(kid string) (SigningKey, bool)

Get returns the key with the given kid and true, or the zero value and false if not found.

func (*KeyRing) JWKS

func (kr *KeyRing) JWKS() ([]byte, error)

JWKS returns the JSON-encoded JWKS document (RFC 7517) containing every RSA public key in the ring. Suitable for serving at /.well-known/jwks.json.

type SigningKey

type SigningKey struct {
	KID        string
	PrivateKey *rsa.PrivateKey // for signing
	PublicKey  *rsa.PublicKey  // for verification + JWKS
	Active     bool
}

SigningKey holds an RSA key pair for JWT signing.

func GenerateKey

func GenerateKey(kid string) (SigningKey, error)

GenerateKey creates a fresh RSA 2048-bit key pair and returns it as a SigningKey. The key is in-memory only.

Jump to

Keyboard shortcuts

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