keys

package
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: MIT Imports: 4 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 keys holds signing and encryption keys for access tokens.

Every Key is pinned to one algorithm. A Provider exposes the active key (read on every issue, so rotation is immediate) and the keys still accepted for verification. MemoryProvider is an in-process implementation; back Provider with a KMS or Vault for multiple instances.

Index

Constants

View Source
const MinHMACKeySize = 32

MinHMACKeySize is the minimum HMAC secret length (256 bits).

Variables

This section is empty.

Functions

This section is empty.

Types

type Alg

type Alg string

Alg names the algorithm a key is pinned to. A key is only ever used with its own algorithm; the algorithm named inside a token is never trusted.

const (
	// HS256 is HMAC-SHA256 (JWT). Secret must be at least MinHMACKeySize bytes.
	HS256 Alg = "HS256"

	// EdDSA is Ed25519 (JWT). PublicKey is required; PrivateKey is required
	// only for issuing.
	EdDSA Alg = "EdDSA"
)

type Finder added in v2.2.0

type Finder interface {
	FindKey(id string) (Key, bool)
}

Finder is implemented by providers that look keys up themselves, for example to fetch a key they have not seen yet (jwt.RemoteKeys). Find uses it when present.

type Key

type Key struct {
	ID  string // logical key ID (kid); must be unique per provider
	Alg Alg    // pinned algorithm; token formats may define their own values

	Secret     []byte             // symmetric key material (HS256, PASETO local)
	PrivateKey ed25519.PrivateKey // EdDSA signing key
	PublicKey  ed25519.PublicKey  // EdDSA verification key
}

Key represents a cryptographic key usable for issuing or verifying tokens.

func Find

func Find(p Provider, id string) (Key, bool)

Find returns the verification key with the given ID.

func (Key) ValidateForJWT

func (k Key) ValidateForJWT(sign bool) error

ValidateForJWT reports whether k is usable for JWT signing (sign=true) or verification (sign=false).

type MemoryProvider

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

MemoryProvider is an in-memory rotating key provider.

NOT for distributed systems (yet). Perfect for monolith / MVP.

func NewMemoryProvider

func NewMemoryProvider(initial Key) *MemoryProvider

NewMemoryProvider creates a key provider with an initial active key.

func (*MemoryProvider) ActiveKey

func (p *MemoryProvider) ActiveKey() Key

ActiveKey returns the current active key.

func (*MemoryProvider) Prune

func (p *MemoryProvider) Prune(maxOld int)

Prune removes old keys beyond retention count.

Call this AFTER rotation window expires.

func (*MemoryProvider) Rotate

func (p *MemoryProvider) Rotate(newKey Key) error

Rotate promotes a new key to active.

Old active key is retained for verification. The new key ID must be non-empty and must not match any key already held.

func (*MemoryProvider) VerificationKeys

func (p *MemoryProvider) VerificationKeys() []Key

VerificationKeys returns all keys valid for verification.

Order matters:

  • active key first (fast path)
  • then older keys

type Provider

type Provider interface {
	ActiveKey() Key
	VerificationKeys() []Key
}

Provider exposes active and historical keys.

Contract:

  • ActiveKey() is used ONLY for issuing tokens, and is read on every issue so that rotation takes effect immediately
  • VerificationKeys() is used for verification
  • VerificationKeys MUST include ActiveKey
  • implementations must be safe for concurrent use

Jump to

Keyboard shortcuts

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