JWKS: retrieve and rotate public keys safely
Learn how a JWK Set is structured, how a resource server obtains keys from a trusted issuer, and how to coordinate caching and key rotation.
JWKS: retrieve and rotate public keys safely
A JSON Web Key Set (JWKS) is a JSON document containing cryptographic keys. OAuth or OpenID Connect servers can publish public signing keys so APIs and clients can verify token signatures. A verifier selects a public key matching the token’s kid from a key set belonging to a trusted issuer, while also checking the source and intended use of that key.
Structure and purpose
RFC 7517 defines a JWK Set as a JSON object with a required keys array; each element is one JWK. Array order does not imply priority by default. Individual keys may contain fields such as kty, use, key_ops, alg, and kid.
{
"keys": [
{
"kty": "RSA",
"use": "sig",
"kid": "signing-key-2026-01",
"alg": "RS256",
"n": "<public modulus>",
"e": "AQAB"
}
]
}This is a structural example with placeholders, not a usable key. A public key set should contain only public signing keys. JWK can represent private or symmetric keys too, so a document at a public endpoint is not automatically safe to publish.
Get keys from a trusted issuer
For an OAuth JWT access token, do not derive a key URL from the token contents. Start with metadata for an issuer the application already trusts. Confirm that metadata’s issuer matches the expected issuer, then retrieve the key list from its jwks_uri. Use kid as a selection hint when present. If it is absent, follow the key-selection rules of the trusted token profile; kid is optional and never establishes trust by itself.
Configure the trusted issuer and its metadata. Do not follow an arbitrary `jku` URL in a token header.
Retrieve metadata over HTTPS and check the issuer, `jwks_uri`, and permitted algorithm policy.
Confirm the response is valid JSON with a `keys` array; check that supported keys have the required structure and appropriate use.
Select a matching key when `kid` exists, or follow the trusted profile’s rules when it does not. Validate token type, issuer, signature, audience, expiry, and required permissions. Reject the request if no permitted key is available or signature verification fails.
Cache the set for a limited period based on provider documentation and risk. On an unfamiliar `kid`, an implementation may refresh once, but must limit retries so attacker-generated tokens cannot trigger unbounded network requests.
Coordinate rotation and caches
If verifiers still have an old JWKS when an issuer begins signing with a new key, they may reject legitimate tokens because the new kid is unknown. Conversely, stopping use of an old private key before verifiers have obtained the new public key can break validation of tokens signed during the transition. An issuer can publish the new public key first, allow verifiers time to refresh, and then begin signing with the new private key. Keep the old public key available for a period that accounts for token lifetime, cache duration, and revocation policy. Standards do not prescribe one universal wait time; check actual cache controls, token lifetimes, and deployment behavior.
If validation fails after a key change, compare the token’s kid and issuer, JWKS refresh time, metadata jwks_uri, cache policy, and the issuer’s signing-key rollout. Do not recover by skipping signature checks or accepting every key.
Public and secret key material
For asymmetric signatures, public keys can be distributed to verifiers; the issuer must protect the private signing key. A symmetric key uses the same secret material to sign and verify. Publishing it in a public JWKS would let anyone create tokens. Before publishing, check for private fields and ensure secrets do not appear in access logs or error messages.
References
[RFC 7517 §5 — JWK Set Format](https://www.rfc-editor.org/rfc/rfc7517.html#section-5)
[RFC 7517 §§9.1–9.2 — Key Provenance and Protection](https://www.rfc-editor.org/rfc/rfc7517.html#section-9)
[RFC 8414 §2 — `jwks_uri` Metadata](https://www.rfc-editor.org/rfc/rfc8414.html#section-2)
[RFC 9068 §4 — JWT Access Token Validation](https://www.rfc-editor.org/rfc/rfc9068.html#section-4)