Migrate from a pinned public key to JWKS
A service that verifies tokens with a copied public key breaks at the next signing-key rotation. Move it to the issuer's key set.
Why
The service rotates its signing key when the active key is older than AUTH_SERVICE_KEY_ROTATION_DAYS (default 90), and an operator can rotate at any time. On rotation the previous key is marked retiring for an overlap at least as long as the longest-lived token, and both keys are published. Every token carries a kid header naming its key. A verifier holding one pinned key rejects every token signed after the rotation. The key adopted from the legacy private and public key settings on first start is only the first key in that sequence; it is not permanent.
Where the keys are
The discovery document names jwks_uri, which is /.well-known/jwks.json on the issuer; /oauth/jwks serves the same set. It lists the active key and any retiring key as RS256 keys with their kid, and answers carry Cache-Control public, max-age=300. Replicas converge on a new key within about 30 seconds of a rotation.
Steps
Do these in order and prove each.
- Find the pinned keys: a PEM public key, a public-key setting, or a verifier built with a key instead of an issuer URL.
- Switch the verifier to the issuer. The SDK verifiers fetch and cache the key set, pick the key by kid, take the algorithm from the key and refresh once on an unknown kid. Pass both the issuer and the audience; both are required and checked exactly. In the monorepo's shared Python middleware set JWKS_URL, JWT_ISSUER and JWT_AUDIENCE: a configured JWKS URL then wins over a static key.
- Keep the static key out of the path. In that middleware a static key is used only with no JWKS URL, or when JWT_ALLOW_STATIC_PUBLIC_KEY is on. That switch is break-glass: it is logged and prefers the stale key over the key set. Do not leave it on.
- Make failures visible. A token whose kid stays unknown after one refresh must be rejected, never accepted on a fallback to some other key. Treat a token with no kid as a failure unless you must keep verifying very old tokens, and then only for the issuer that emitted them. The SDK verifiers and the shared Python middleware behave this way. The middleware's JWT_ALLOW_KIDLESS_TOKENS switch accepts a token with no kid again; it is break-glass: see the last section.
- Prove rotation in a non-production environment: rotate with an administrator credential, check that a token signed before and one after both verify, that the key set lists both kids during the overlap, and that nothing restarts. The rotation runbook has not been rehearsed end to end.
- Remove the static key from configuration, images and secrets storage once that passes.
- Set the pins on the consumer, and in the shared Python middleware turn JWT_REQUIRE_PINS on so a missing pin fails closed.
Verification
A token signed by the previous key still verifies during the overlap. With an SDK verifier, a token with an unknown kid is rejected and costs at most one key set fetch per refresh interval; the shared Python middleware does the same, per process, with a 10-second interval. A token whose header alg differs from the key's is rejected. Nothing in your deployment mentions a PEM public key.
Limits of the shared Python middleware
The monorepo's shared Python middleware fetches the key set at most once every 10 seconds, whether the last fetch succeeded or failed, and by default rejects a token with no kid without fetching anything. It still differs from the SDK verifiers. The interval is fixed and kept per process, so several processes each make their own fetches, and a newly published key, or the issuer coming back after a failed fetch, can take up to 10 seconds to be picked up. With JWT_ALLOW_KIDLESS_TOKENS on, it tries a token with no kid against every published key and accepts it if one verifies; leave it off unless an issuer you trust still omits kid.
The Nyay OS MCP token check and consent ticket check use the same key set logic. The MCP check keeps using the last set it fetched while the issuer cannot be reached. Prefer the SDK verifier for new work.
Rollback
Put the static key back only as time-boxed break-glass and plan the next rotation around it. If your verifier cannot reach the key set, fix the path to the issuer; do not pin a key as a workaround, because the pin goes stale silently.