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 ¶
const MinHMACKeySize = 32
MinHMACKeySize is the minimum HMAC secret length (256 bits).
Variables ¶
var ErrUnknownKey = errors.New("keys: unknown key ID")
ErrUnknownKey is returned by Revoke for a key ID the provider does not hold.
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.
type Finder ¶ added in v2.2.0
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
// NotAfter, when set, retires the key at that time: from then on it
// neither issues nor verifies tokens, and PublicJWKS leaves it out,
// even if the provider still holds it. Zero means no limit.
NotAfter time.Time
}
Key represents a cryptographic key usable for issuing or verifying tokens.
func (Key) ValidateForJWT ¶
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) Revoke ¶ added in v2.5.0
func (p *MemoryProvider) Revoke(id string) error
Revoke removes one old key at once, so tokens it signed stop verifying on the next call. The active key cannot be revoked: Rotate to a new key first, then revoke the old one.
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 ¶
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