opyebJWTlab

JWKS, kid and key rotation: fetching and selecting public keys safely

Operations · 6 min read

In one line: The shape of a JWKS document, choosing a key by kid, running an overlapping rotation, and the caching pitfalls to avoid.

With an asymmetric algorithm the verifier needs a public key. JWKS (JSON Web Key Set) is the standard way to publish several keys at once, and kid is the value that tells them apart.

What a JWKS looks like

{
  "keys": [
    {"kty": "RSA", "kid": "2026-11", "use": "sig", "alg": "RS256",
     "n": "sXch...", "e": "AQAB"},
    {"kty": "RSA", "kid": "2026-12", "use": "sig", "alg": "RS256",
     "n": "uPq0...", "e": "AQAB"}
  ]
}

kty is the key type (RSA, EC, OKP), use is the purpose (sig for signing), and the key material is n and e for RSA, crv, x, y for EC, and crv, x for Ed25519. Every value is Base64URL.

Selecting by kid

The token header's kid selects the matching key in the set. If the token has no kid, or no key matches, do not guess: reject. A set with a single key can be used without a kid, but that stops working the moment rotation starts, so include kid from the beginning.

Rotation

StepAction
1. PublishAdd the new public key to the set; do not sign with it yet
2. Switch signingStart signing with the new kid and keep the old key listed
3. OverlapServe both keys for at least the longest token lifetime
4. RemoveDrop the old key once every token it signed has expired

Skipping step three invalidates every token issued before the switch. With 15-minute access tokens, publish both keys for at least 15 minutes, ideally 30 or more.

Caching and failure

Fetching the JWKS on every verification adds latency and another failure point. Cache it in memory and refetch only when an unknown kid appears. Even then, rate-limit refetches, or an attacker can flood you with random kids. The JWKS URL must be HTTPS and, where possible, come from a fixed trust list.

JWTlab is zero-API and never requests a JWKS on your behalf. Paste the JSON you obtained from a trusted issuer and it checks kid selection, key type and the signature itself.

Decoding and signature checks run right here in your browser. The token you paste is not stored.

Open the JWTlab decoder

Frequently asked questions

What if kid changes on every token?

kid identifies a key, so it should change only when keys change. A per-token kid means the issuer creates a new key each time, which invalidates JWKS caches constantly. Fix the key and define a rotation period.

Can a JWKS contain a private key?

A legitimate JWKS has public values only. If parameters such as d, p or q appear, treat the key as compromised and rotate. JWTlab refuses to verify with a private-key PEM.

Sources

Read next