Auth & Identity

JWKS and Key Rotation

A JSON Web Key Set (JWKS) is the public directory of keys an issuer uses to sign tokens. Publishing keys this way lets issuers rotate signing keys without breaking the services that verify their tokens.

What you'll learn

  • Explain what a JWKS endpoint provides
  • Describe how a verifier selects the right key
  • Understand why key rotation is necessary
  • Identify how to cache keys without breaking rotation

6 min

The public key directory

A JWKS is a JSON document that lists an issuer's public signing keys, each tagged with a key identifier and the algorithm it is used with. Verifiers fetch this set from a well-known endpoint and use it to check the signatures on the tokens they receive. Because the keys involved are public keys, the document can be served openly to anyone, with no authentication required.

This published directory is precisely what lets independent services validate Merion-issued tokens locally, with no shared secret passing between them. The issuer alone holds the corresponding private keys and uses them to sign tokens; everyone else uses the matching public keys merely to verify. That asymmetry is what makes the whole arrangement safe to expose, and it is the foundation that makes painless key rotation possible.

Matching the key

Each JWT header carries a key identifier that names which key was used to sign that particular token. To verify a token, a service reads that identifier from the header and selects the matching key from the JWKS document it has fetched. This small piece of indirection is the mechanism that makes rotation work at all: the token itself effectively points at the key needed to check it.

If a service cannot find a key matching the identifier in its current copy of the JWKS, the correct behaviour is to refresh that copy once before giving up, in case a brand-new key has just been published that the service has not seen yet. Only after a refresh still fails to produce a match should the token be rejected. Designing for this case is what keeps verification working smoothly across a rotation.

Why rotate keys

Key rotation exists to limit the damage if a signing key is ever compromised, and more generally as a matter of good operational hygiene, much like changing passwords. Issuers periodically introduce a new signing key, begin signing freshly issued tokens with it, and later retire the old key once any tokens it signed have safely expired.

During the overlap period, both the old and new keys appear together in the published JWKS, so tokens signed by either remain verifiable for as long as they are valid. This staged approach is the entire reason rotation can happen without downtime for well-behaved clients: nothing is ever pulled out from under a token that is still in use. A client that refreshes its key set on demand will simply pick up the new key and carry on uninterrupted.

Caching responsibly

Fetching the JWKS on every incoming request would be needlessly wasteful, generating constant traffic to the issuer for data that rarely changes, so verifiers cache the key set instead. The art is in caching it with a sensible lifetime while also refreshing on demand whenever an unknown key identifier appears in a token. That combination gives you the performance of a cache without the risk of missing a freshly rotated key.

A cache that never refreshes will eventually reject perfectly valid tokens after a rotation; a cache that refreshes too eagerly defeats its own purpose. Most OIDC libraries strike this balance for you automatically, so you rarely implement it by hand. To see how the fetched keys are then actually used, revisit Validating a JWT or browse the Merion API documentation.

Key takeaways

  • A JWKS publishes an issuer's public signing keys for verifiers
  • Tokens reference the signing key by identifier in their header
  • Rotation introduces new keys and retires old ones with overlap
  • Cache the key set but refresh when an unknown key appears

FAQ

Is it safe to expose the JWKS publicly?

Yes. It contains only public keys, which are meant to be shared so anyone can verify signatures. The private signing keys never leave the issuer.

What happens to tokens during rotation?

Existing tokens stay valid because the retiring key remains in the JWKS until those tokens expire. New tokens use the new key. There is an intentional overlap.

How long should I cache the JWKS?

Long enough to avoid constant refetching, but always refresh on demand if you encounter an unrecognised key identifier so you pick up new keys promptly.

Integrate with Merion

Ready to build?

Read the API reference, grab the OpenAPI spec, and ship a resilient integration.