JWKS and key rotation without breaking every token
A JSON Web Key Set is a JSON document with a keys array, each entry a public key in JWK form. Identity providers publish one at a well-known URL, often /.well-known/jwks.json or a path advertised in their OpenID configuration. A verifier downloads it, selects the key whose kid matches the token header, and verifies. This is what makes asymmetric JWT practical at scale: one private key at the issuer, many verifiers with a cache of public keys.
A typical entry has kty, kid, use set to sig, alg such as RS256, and the key parameters — n and e for RSA, crv, x and y for EC, crv and x for OKP. Some sets also include x5c, an X.509 certificate chain, and x5t, a thumbprint. A private key parameter such as d must never appear in a published set.
| Field | Meaning | Example | Needed for verification? |
|---|---|---|---|
| kty | Key type | RSA, EC, OKP | Yes |
| kid | Key identifier | 2026-11-signing-1 | For selection |
| use | Intended use | sig | Recommended check |
| alg | Algorithm this key is for | RS256 | Recommended check |
| n, e | RSA modulus and exponent | base64url | RSA keys |
| crv, x, y | EC curve and coordinates | P-256, base64url | EC keys |
| x5c | Certificate chain | array of base64 DER | Optional |
Caching that survives rotation
Fetching the JWKS on every request couples your latency and availability to the issuer's key endpoint and multiplies load during an incident. Caching forever means a rotation silently breaks every new token. The workable pattern is: fetch at startup, honour the response's cache lifetime with a sensible minimum and maximum, and refetch when a token presents an unknown kid. The refetch should be rate-limited and shared across concurrent requests, so a burst of tokens with the same unknown kid causes one fetch, not thousands.
After the refetch, retry the verification once. If the kid is still unknown, reject the token. Do not fall back to the first key or to a key without a kid. A common bug is to treat a missing kid as "try every key"; that works until a key set has more than one key, and it turns a rotation into a performance problem and an ambiguity.
Rotating without logging everyone out
A rotation has three phases. First, generate the new key pair and publish its public key in the JWKS alongside the old one, with a new kid. Second, begin signing new tokens with the new private key while the old public key remains published. Third, after the longest token lifetime plus the verifier cache lifetime has passed, remove the old public key. If the old key is removed too early, tokens already issued with it cannot be verified and users are logged out or API calls fail.
The overlap should be at least the maximum access-token lifetime, and longer if refresh tokens or long-lived ID tokens are signed with the same key. If the key was compromised rather than merely rotated, the overlap must be zero and the old key must be revoked immediately; the cost of invalidating live tokens is the correct trade-off in that case. Decide which situation you are in before choosing the overlap.
What can go wrong
A duplicated kid makes selection ambiguous. A key set that publishes a private parameter d leaks the signing key to the world. A verifier that accepts keys without checking use or alg may pick a key intended for encryption, or an RSA key for an ES256 token. A cache with no expiry means an incident response that rotates keys has no effect until every process restarts.
On the issuer side, publishing the new key before signing with it is essential. If the new key is used first and published second, there is a window in which verifiers see an unknown kid and reject valid tokens. During that window, clients may retry aggressively and amplify the problem. A short pre-publication period of a few minutes is cheap insurance.
Practical checklist
- Publish only public parameters; assert in a test that no key object contains d, p, q or k.
- Give every key a unique kid and include it in every token header.
- Serve the JWKS with a cache lifetime and support conditional requests.
- Cache in the verifier, honour the lifetime, and refetch only on an unknown kid.
- Rate-limit and single-flight the refetch path.
- Overlap old and new keys for at least the maximum token lifetime, or zero on compromise.
- Never follow jku, x5u or an embedded jwk from an untrusted token.
The sample tokens in this site's signer include a kid and export a public JWK, so you can exercise selection and verification locally. For a real deployment, the same operations run against the issuer's JWKS with the caching and overlap rules above.
Rotation is a scheduled, rehearsed operation, not an emergency surprise. Test it in staging by rotating a key and watching a token issued before the rotation still verify.
Sources
- RFC 7517 — JSON Web Key (JWK) and JWK Set
- RFC 8725 §3.5 — Key identification and rotation
- OpenID Connect Core §10.1.1 — Rotating signing keys
Questions
How often should a verifier fetch the JWKS?
On startup, then cache it with the Cache-Control lifetime the issuer provides, and refetch when a token names an unknown kid. Refetching on every request is a denial-of-service amplifier and adds latency; never refetching means a rotation breaks verification. Rate-limit the unknown-kid path to one fetch per minute or so.
What happens if two keys have the same kid?
The key set is ambiguous and the verifier may pick the wrong one. kid values must be unique within a JWKS. During rotation, give the new key a new kid and keep the old one until tokens signed with it have expired. Reusing a kid for a new key is a common cause of intermittent signature failures.
Should I trust the jku header?
No. A jku or x5u header lets the token point at a key URL, which means the attacker chooses the key. Use the issuer's configured JWKS URL, not a URL from the token. Ignore embedded jwk and jwk sets in headers unless you have a specific, validated use case.
Paste a public JWK from a JWKS into the verifier to check which kid signs a token.
Open the verifierWritten against the JOSE specifications (RFC 7515–7519), RFC 7636 and RFC 8037; every example can be reproduced in the verifier linked above.
Back to the guides