Documentation
¶
Overview ¶
Package jwt issues and verifies JWT access tokens (RFC 7519, RFC 9068) using only the standard library.
Only two algorithms exist: HS256 and EdDSA (Ed25519). Every key is pinned to one algorithm through keys.Key.Alg, and the verifier looks the key up by "kid" and requires the token's "alg" to match it. "none", RSA and ECDSA are never accepted, which rules out algorithm-confusion attacks by construction.
Index ¶
Constants ¶
const ( // DefaultTTL is the access-token lifetime when Config.TTL is zero. DefaultTTL = 10 * time.Minute // DefaultLeeway is the clock-skew allowance when Config.Leeway is zero. DefaultLeeway = 30 * time.Second // MaxLeeway caps Config.Leeway. MaxLeeway = 2 * time.Minute // MaxTokenSize is the largest token the verifier will parse. MaxTokenSize = 8 << 10 // TokenType is the "typ" header for access tokens (RFC 9068). TokenType = "at+jwt" )
Variables ¶
This section is empty.
Functions ¶
func JWKSHandler ¶ added in v2.2.0
JWKSHandler serves PublicJWKS(kp) as application/jwk-set+json, for other services to verify access tokens without being able to mint them. The set is built on every request, so rotation shows up at once; maxAge sets Cache-Control (zero means 5 minutes). Keep a rotated-out key until every verifier's cache has seen the new one: prune after at least maxAge + the access-token TTL.
Types ¶
type Config ¶
type Config struct {
Issuer string // "iss"; required
Audience string // "aud"; required
TTL time.Duration // issuer only; DefaultTTL if zero
Leeway time.Duration // verifier only; DefaultLeeway if zero, at most MaxLeeway
// Now returns the current time. Nil means time.Now. Useful in tests.
Now func() time.Time
}
Config holds the settings shared by Issuer and Verifier.
type Issuer ¶
type Issuer struct {
// contains filtered or unexported fields
}
Issuer issues JWT access tokens signed with the provider's active key.
The active key is read on every call, so rotating the provider takes effect for the next token issued.
type JWK ¶ added in v2.2.0
type JWK struct {
Kty string `json:"kty"` // "OKP"
Crv string `json:"crv"` // "Ed25519"
X string `json:"x"` // base64url public key
Kid string `json:"kid"` // key ID, as in the token header
Alg string `json:"alg,omitempty"` // "EdDSA"
Use string `json:"use,omitempty"` // "sig"
}
JWK is a public JSON Web Key (RFC 7517) for an Ed25519 key (RFC 8037).
type JWKS ¶ added in v2.2.0
type JWKS struct {
Keys []JWK `json:"keys"`
}
JWKS is a JSON Web Key Set.
func PublicJWKS ¶ added in v2.2.0
PublicJWKS returns the public keys of kp's EdDSA verification keys, active key first. HS256 secrets, private keys and keys past their NotAfter are never included.
type RemoteKeys ¶ added in v2.2.0
type RemoteKeys struct {
// contains filtered or unexported fields
}
RemoteKeys is a verification-only keys.Provider backed by a remote JWKS, typically another service's JWKSHandler. Pass it to NewVerifier.
It accepts only Ed25519 keys (kty OKP, alg EdDSA or absent, use sig or absent); every key stays pinned to EdDSA. ActiveKey returns the zero Key, so RemoteKeys cannot be used to issue tokens. A token with an unknown key ID triggers a fetch, at most once per MinRefreshInterval; the cached set is also refreshed in the background once it is older than RefreshInterval. A failed fetch keeps the previous set, for at most MaxStale when that is set.
func NewRemoteKeys ¶ added in v2.2.0
func NewRemoteKeys(ctx context.Context, jwksURL string, cfg RemoteKeysConfig) (*RemoteKeys, error)
NewRemoteKeys fetches the key set at jwksURL once, and fails if that fetch fails, so a misconfigured URL is noticed at start-up.
func (*RemoteKeys) ActiveKey ¶ added in v2.2.0
func (r *RemoteKeys) ActiveKey() keys.Key
ActiveKey implements keys.Provider. It returns the zero Key: remote keys verify, they never sign.
func (*RemoteKeys) FindKey ¶ added in v2.2.0
func (r *RemoteKeys) FindKey(id string) (keys.Key, bool)
FindKey implements keys.Finder: an unknown key ID causes a fetch (rate limited), since the issuer may have rotated.
func (*RemoteKeys) Refresh ¶ added in v2.2.0
func (r *RemoteKeys) Refresh(ctx context.Context) error
Refresh fetches the key set now. On failure the previous set is kept.
func (*RemoteKeys) VerificationKeys ¶ added in v2.2.0
func (r *RemoteKeys) VerificationKeys() []keys.Key
VerificationKeys implements keys.Provider. Past MaxStale it returns no keys.
type RemoteKeysConfig ¶ added in v2.2.0
type RemoteKeysConfig struct {
// HTTPClient fetches the set. Default: a client with a 10-second timeout.
HTTPClient *http.Client
// RefreshInterval is how old the cached set may get before it is
// fetched again in the background. Default 15 minutes.
RefreshInterval time.Duration
// MinRefreshInterval limits fetches caused by unknown key IDs, so
// tokens with made-up IDs cannot make the verifier hammer the issuer.
// Default 1 minute.
MinRefreshInterval time.Duration
// AllowHTTP permits a plain-HTTP URL. Only for tests and local
// development: keys fetched over HTTP can be replaced in transit.
AllowHTTP bool
// MaxStale, when set, bounds how long the last fetched set is trusted
// while fetches keep failing. Past it, no key verifies (fail closed)
// until a fetch succeeds, so a key the issuer withdrew cannot be kept
// alive by blocking the fetch. It must exceed RefreshInterval. Zero
// keeps the last set indefinitely, as before.
MaxStale time.Duration
}
RemoteKeysConfig configures RemoteKeys.
type Verifier ¶
type Verifier struct {
// contains filtered or unexported fields
}
Verifier verifies JWT access tokens.
Checks, in order: size and shape, header ("typ" must be at+jwt, "kid" must name a known key, "alg" must equal that key's pinned algorithm), signature, then claims ("iss", "aud", "exp", "nbf", "iat" with leeway, non-empty "sub").
func NewVerifier ¶
NewVerifier creates a JWT verifier.