Beta

Create a tenant

A new tenant starts with its own users, OAuth settings, audit history and logs. You are its first Tenant Admin.

Tokens fail with an unknown key after a rollover

A planned signing-key rollover should pass without anyone noticing, because verifiers fetch the new key before tokens use it. This lesson follows one photo API instance that refuses every new token after a rollover, finds the stale key source behind it, repairs only that, and checks that the old key's overlap and the refusals both still hold.

One instance refuses every new token

All names, domains and identifiers in these examples are fictional. At 06:07 UTC on a Monday, the photo API's monitoring pages you: "api-3: 401 rate 41% over 5 minutes (fleet median 0.3%)." The photo API runs as four instances behind api.photos.example, and only one of them is unhappy. The printing company has noticed too. Some of its album requests fail and a retry usually succeeds, because the retry tends to land on a different instance.

The timing is not subtle. At 06:00, the photo service's identity provider, auth.photos.example, switched to a new signing key, as planned for weeks. Since then, every access token it issues is signed with the new key. Tokens issued before 06:00 still carry signatures from the old one.

It helps to be precise about who sends what. The identity provider issues a token to the printing site. The printing site presents that token to the photo API. The photo API verifies the signature with a public key it obtained from the identity provider's published key set, the JWKS that JWKs, key sets, and key IDs described. The identity provider never talks to the API about any particular token. Its whole part in verification is publishing keys.

Here is one of the refusals from api-3:

GET /albums HTTP/1.1
Host: api.photos.example
Authorization: Bearer <redacted>

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="photos", error="invalid_token", error_description="No key matches the token's key ID"
X-Request-ID: 6e0b93d4-2c71-4f58-9a3e-b1d8c5f72a06
X-Served-By: api-3

Compare the token with the verifier's key set

Start with the token. The printing site's engineer decodes a refused one locally and sends you the header and the issuer, which is all you need:

Header:
{ "alg": "ES256", "kid": "photos-2026-10", "typ": "at+jwt" }

Claims (excerpt):
{ "iss": "https://auth.photos.example", "aud": "https://api.photos.example" }

The kid, or key ID, tells a verifier which of the issuer's keys to use. It does not tell the verifier which issuers to trust or where to get keys. Those come from the API's configuration. The issuer here is the one the photo API is configured to trust, so this is not a token from the wrong environment.

Next, look at what the issuer publishes. Its discovery document names the key set's location as jwks_uri, as Reading provider metadata showed, and fetching it gives the current public keys:

GET /.well-known/jwks.json HTTP/1.1
Host: auth.photos.example

HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: public, max-age=3600

{
  "keys": [
    { "kid": "photos-2026-09", "kty": "EC", "crv": "P-256", "alg": "ES256", "use": "sig",
      "x": "f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU", "y": "x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0" },
    { "kid": "photos-2026-10", "kty": "EC", "crv": "P-256", "alg": "ES256", "use": "sig",
      "x": "WbbaSStuffGv3-nBRxzb6Rp6kEn2JaWhJKd9Q4ZVb5U", "y": "rHtM4w9Bc0gXl2JcLs0Q3pZyH7mKdV8eTn1RfSa6uYo" }
  ]
}

Each entry is a public key: kty is its type, alg the signing algorithm it is used with, and use: "sig" says it is for verifying signatures. Public keys are not secrets, so the key set is safe to share in a ticket. Both keys are there, and Cache-Control tells verifiers they may reuse this response for up to an hour before fetching it again.

Where the token's kid turns up decides where the fault is:

Where an unknown key ID can come from
What you findWhat it points to
The token names a different issuerAn environment mismatch, not a rollover. A valid token is refused in one deployment follows that path.
The issuer is right, but its key set lacks the kidThe issuer is signing with a key it has not published. No verifier can fix that; the issuer's rollout must.
The issuer publishes the kid, but one verifier lacks itThat verifier's key source or cache is stale.

This is the third row. Every instance runs the same code against the same issuer, so the difference has to be in how each one loaded its keys, and the instances' own logs record exactly that:

api-1  2026-10-12T05:31:08Z  jwks.refresh  source=https://auth.photos.example/.well-known/jwks.json
                             keys=photos-2026-09,photos-2026-10  next_refresh_s=3600
api-3  2026-10-02T14:31:40Z  jwks.load     source=file:/etc/photos-api/jwks.json
                             keys=photos-2026-09  refresh=disabled

api-3 has not read the issuer's key set in ten days. The change history explains it: during a discovery outage on 2 October, an engineer pointed api-3 at a saved copy of the key set so it could restart, and the change was never reverted. That file is a snapshot from before the new key was published. Every other instance has been refreshing hourly and picked up photos-2026-10 a week ago.

The same new token at a refreshing instance and at api-3, pinned to a key set snapshot.

Refetch once, from the trusted source

A verifier that loads keys from the issuer is designed to survive a rollover even with a cache. OpenID Connect Core, section 10.1.1, describes the behavior, as Signing keys and rotation explained: a verifier that sees an unfamiliar kid goes back to the issuer's jwks_uri and retrieves the keys again. That only works if the verifier has a jwks_uri to go back to. api-3 had a file and refreshing turned off, so its refusal was final.

Two limits keep that refetch safe. First, it is bounded. Anyone can send a token with a made-up kid, and a verifier that fetched the key set for every one of them would hammer the issuer on an attacker's behalf. One refetch per short interval is enough. Second, the keys come only from the configured issuer's jwks_uri. A JWT header can carry its own key-related parameters: jku names a URL for a key set, x5u a URL for a certificate, and jwk embeds a public key directly. A verifier must never use those to find its key, because then the token would choose the key that checks its own signature, and anyone could sign one. In outline, the lookup below tries the keys it holds, refetches from the configured jwks_uri at most once a minute when the kid is unfamiliar, and refuses the token if the key is still missing:

async function keyFor(header: JwtHeader): Promise<PublicKey> {
  let key = keySet.find(header.kid, header.alg);
  // An unfamiliar kid allows one refetch a minute, from the configured issuer only.
  // header.jku, header.x5u and header.jwk are never consulted.
  if (!key && keySet.fetchedAt < Date.now() - 60_000) {
    await keySet.refresh(config.jwksUri);
    key = keySet.find(header.kid, header.alg);
  }
  if (!key) throw new InvalidToken("No key matches the token's key ID");
  return key;
}

The narrow repair follows: point api-3 back at the managed source, the issuer's jwks_uri with normal refreshing, exactly like the other three instances. After the change, api-3 logs jwks.refresh with both key IDs. Nothing else changes. Signature checking stays on, the allowed algorithm stays ES256, and the issuer, audience and expiry checks run as before. Turning off signature checks on api-3, or adding whatever key arrives with a token, would make the 401s stop by making api-3 accept tokens nobody signed.

Publish, sign, retire

A planned rollover is three separate steps, and keeping them apart is what makes it painless. The identity provider published photos-2026-10 on 5 October, a week before using it. That is far longer than the one-hour max-age, so every verifier refreshing normally had the new key well before any token needed it. It started signing with the new key at 06:00 today. It will retire the old key, removing it from the key set, only once nothing it signed can still be valid.

"Nothing it signed" needs an inventory, not an assumption. The printing site's access tokens last ten minutes, so it is tempting to retire the old key ten minutes after 06:00. But the old key signed everything the issuer issued until then. ID tokens for the printing site last an hour. And the inventory turns up the printing company's overnight batch client, whose access tokens last twelve hours. A token it received at 05:58 is valid until 17:58, and removing the old key before then would break it at every instance at once. The issuer team sets retirement for 13 October, after the longest-lived artifact has expired, with a margin.

A compromised key is different. If the old private key had leaked, the issuer would remove it from the key set immediately and accept that every token it signed stops working. That disruption is the point. A planned rollover keeps the overlap. A response to compromise deliberately ends it.

Verify overlap, new tokens and refusal

Verify against api-3 directly, using X-Served-By to confirm which instance answered. A token issued after 06:00, signed with photos-2026-10, should now get 200. So should a token signed with the old key that is still within its lifetime. The batch client's token from before 06:00 is the obvious one, and it proves the overlap still holds on the repaired instance.

Then check that api-3 still refuses what it should. A token signed by a key the issuer never published, which the identity team generates with a throwaway test key, must get 401. api-3's log should show exactly one jwks.refresh triggered by it, and a second such token a few seconds later must be refused with no further fetch. A genuine token from another issuer that happens to use the same kid string must also be refused, because api-3 only trusts the configured issuer's keys. Do not produce these by editing a real token's header. Changing the header invalidates the signature, so the refusal would prove nothing about key lookup.

Worked example: the handoff note

The note ends with a key ledger, because the next rollover depends on knowing when each key was published, used and retired:

Symptom:       api-3 refused new tokens with 401 invalid_token from 06:00 UTC on 2026-10-12.
               Other instances unaffected.
First failure: Key lookup at api-3, kid photos-2026-10 not found
               (request 6e0b93d4-2c71-4f58-9a3e-b1d8c5f72a06).
Compared:      Token iss https://auth.photos.example, kid photos-2026-10.
               Issuer JWKS publishes photos-2026-09 and photos-2026-10 (since 2026-10-05).
               api-3 loaded photos-2026-09 only, from file, refresh disabled (since 2026-10-02).
Cause:         Workaround from the 2026-10-02 discovery outage pinned api-3 to a key set snapshot
               and was never reverted.
Repair:        api-3 key source restored to the issuer's jwks_uri with hourly refresh.
Verification:  At api-3: new-key token 200; old-key batch token (issued 05:58) 200;
               unpublished-key token 401 after one refetch, second one 401 with no refetch;
               other-issuer token with the same kid string 401.
Not changed:   Issuer keys and rollover schedule, validation rules, other instances.
Key ledger:    photos-2026-09  published 2026-09-07  signing until 2026-10-12 06:00  retire 2026-10-13
               photos-2026-10  published 2026-10-05  signing from  2026-10-12 06:00
Follow-up:     Alert when any instance's key set is older than a day.

Check your understanding

Try these questions before moving on. If an answer isn't right, use the feedback and try again.

The issuer's JWKS publishes both the old and the new kid, but one API instance refuses every token signed with the new key. What is the narrow repair? 1 of 2

Choose one answer.

The printing site's access tokens last ten minutes. Why might the old key need to stay published long after ten minutes past the switch? 2 of 2

Choose one answer.

Try it in the Lab

We value your privacy

We use cookies and similar technologies to enhance your browsing experience, and analytics to understand our traffic. By clicking "Allow all", you consent to optional analytics. Cookie Policy

Learn identity