LanguageEnglish
Build →

Guides · QuorumAuth

Fixing "kid not found" when verifying a JWT against a JWKS

Your service rejects tokens that look fine, and the log says the key ID is not in the key set. It is almost always one of five causes, and one of them means the rejection is correct.

Updated

The symptom

Tokens that worked an hour ago now fail, or tokens from one environment fail in another. Libraries phrase it differently: “kid not found”, “no matching key”, “unable to find a signing key”. They all mean the same thing: the token’s header names a key ID (kid) that is not in the JSON Web Key Set your verifier holds.

The fix depends on why. Retrying blindly, or switching off the check, turns a key-distribution bug into a security hole.

Five causes, from most to least common

  1. Stale cache after a rotation. The issuer published a new key and signs with it. Your verifier fetched the set once at startup and never again.
  2. Wrong key set. The token comes from another issuer than the one whose set you load: staging against production, one tenant or organization against another, or a hard-coded URL that no longer matches the issuer.
  3. A withdrawn key. After an emergency rotation, the issuer stops publishing the old key. Tokens it signed should fail. This one is not a bug.
  4. A forged or malformed token. Anyone can write any kid into a header. If such tokens make your verifier refetch every time, they double as a way to flood the issuer.
  5. A failed fetch reported as a missing key. A timeout or TLS error while loading the set leaves the verifier with no keys, and some code paths report that as “kid not found”.

Finding which cause you have

  1. Decode header and iss → Expected issuer?
  2. Expected issuer? → Wrong key setno
  3. Expected issuer? → kid in live set?yes
  4. kid in live set? → Stale cache: refetchyes
  5. kid in live set? → Withdrawn or forged: refuseno

A decision flow. Decode the token header. If the issuer is not the one you expect, you are reading the wrong key set. If the kid is in the issuer's current key set, your cache is stale. If the kid was in an earlier key set and is gone, the key was withdrawn and the refusal is correct. Otherwise the token names a key the issuer never published.

Two values from the token header and one from the issuer's key set settle most cases.

Check it in two minutes

Decode the header without verifying it, then compare with what the issuer publishes. Replace the placeholders with your token and issuer.

# 1. What the token claims: kid and alg in the header, iss in the payload
TOKEN='<your token>'
for part in 1 2; do
  s=$(printf %s "$TOKEN" | cut -d. -f"$part" | tr '_-' '/+')
  while [ $(( ${#s} % 4 )) -ne 0 ]; do s="$s="; done
  printf %s "$s" | base64 -d; echo
done

# 2. What the issuer publishes right now
ISSUER='<issuer URL from your configuration>'
JWKS_URI=$(curl -fsS "$ISSUER/.well-known/openid-configuration" | jq -r .jwks_uri)
curl -fsS "$JWKS_URI" | jq -r '.keys[].kid'
  • iss differs from your configured issuer: cause 2.
  • The kid is in the live set: cause 1, or cause 5 if your logs show a failed fetch.
  • The kid is not in the live set: cause 3 or 4. Refuse the token.

Fix the verifier, not the incident

  • Refetch once on an unknown kid, rate limited. A rotation then heals itself on the first new token, and a forged one cannot make you fetch on demand.
  • Take the key set location from configuration or discovery. Read the issuer from your configuration and the jwks_uri from its discovery document. Never from the token.
  • Check iss before you look up the key. A token from an issuer you do not trust should fail as such, not as a missing key.
  • Report fetch failures as unavailable. “I could not load the keys” and “this token is invalid” need different alerts and different retries.
  • On the issuer side, overlap keys. Publish the new key before signing with it, and keep the old one until its last token expires, unless you are withdrawing it on purpose.

A verifier that survives rotation

  1. 1Verifier → VerifierKnown kid: verify from cacheNo request
  2. 2Verifier → Issuer key setUnknown kid: fetch once
  3. 3Issuer key set → VerifierCurrent keys, new kid included
  4. 4Verifier → VerifierForged token: refuseNo further fetch

A sequence between a verifier and an issuer's key set. The verifier verifies a token whose kid it holds from cache, with no request. The issuer rotates. A token arrives naming the new kid; the verifier fetches the key set once and verifies. A forged token arrives; it is refused on the key it names, without fetching again.

A known key costs nothing; an unknown key costs one fetch; a forged one costs no fetch at all.

How QuorumAuth avoids it

QuorumAuth signs the tokens and runs the verifier side in its SDK, so the behaviour above is built in. In its tested behaviour:

  • One issuer per organization. Each organization is its own OpenID Provider, with an issuer that never changes when the organization is renamed and a jwks_uri holding exactly the keys that organization publishes. An organization that does not exist has no issuer and no key set.
  • Known keys cost nothing. A key ID the SDK already holds is answered without contacting the authority, and later verifications for the same organization come from cache.
  • Unknown keys cost one fetch. After a rotation, a key the SDK has never seen costs exactly one immediate discovery round-trip.
  • Forged tokens cost no fetch. A tampered token is refused on the key it names, and the SDK fetches no further keys.
  • Withdrawal is explicit. An emergency rotation names the key it withdraws, and the published key set no longer contains it. A refused rotation withdraws nothing.
  • Failures say what failed. Discovery refuses a key set carrying private key material, refuses a peer that is not the expected authority, and when discovery is unreachable it fails closed and names the connection failure.

Keys are only half of token trust. To stop a token whose key is still valid, see how to revoke a JWT before it expires.

Where to go next

For the key set format and rotation routine, read what a JWKS is. For services that sign their own requests, read about JWT client assertions. For the whole machine-to-machine picture, see the machine-to-machine authentication platform.

Why does "kid not found" start right after a key rotation?

Your verifier cached the old key set and does not refetch when a token names a key it has never seen. Refetch once on an unknown kid, with a rate limit.

Should I refetch the JWKS every time verification fails?

No. A forged token can name any kid it likes, so unlimited refetching lets anyone make you hammer the issuer. Refetch once per unknown kid, rate limited, and refuse after that.

Can I read the JWKS URL from the token?

No. Take it from your configuration or the issuer's discovery document. A key location chosen by the token lets the token choose who it trusts.

Is "kid not found" ever the right answer?

Yes. After an emergency rotation, tokens signed with the withdrawn key should fail. So should tokens from an issuer you do not trust.

Talk to sales about QuorumAuth.

QuorumAuth is sold through our sales team. The Build page shows the drop-in sign-in on your origin and how to reach sales about your stack.

Go to Build and contact sales →