Skip to content

JWT kid matcher: find the signing key

Match a token’s kid and alg against a JWKS to find the key that should verify it, see why other keys do not fit, and check the signature.

Runs in your browser. Nothing you paste is sent to us.

Token and key set

A sample ES256 token for the made-up realm acme is filled in, with the realm’s sample key set below.

kid
sample-acme-es256
alg
ES256

For a Keycloak realm, the JSON from /realms/<realm>/protocol/openid-connect/certs.

Match

One key fits
Key 2 (kid sample-acme-es256).
#kidTypeFits?
1sample-acme-rs256RSA RS256 signokid does not match; key type RSA, ES256 needs EC; key is for RS256
2sample-acme-es256EC P-256 ES256 sigyes
3sample-acme-encRSA RSA-OAEP encnokid does not match; key type RSA, ES256 needs EC; key is for RSA-OAEP; use is "enc", not "sig"

How to use it

  1. Paste the token.
  2. Paste the key set, such as a Keycloak realm’s certs endpoint.
  3. Read which key fits and why the others do not.
  4. Check that the signature verifies with the matched key.

What to know

A verifier picks the key by the kid in the token’s header, then checks that the key type, curve, alg and use fit the token.

When Keycloak rotates keys, new tokens carry the new kid while older ones still name the previous key. Keep the old key as passive until those tokens have expired.

A kid that is not in the set usually means the token comes from another realm or server, or the verifier’s cached key set is out of date.

What a key id is for

An issuer rarely has just one key. It has a signing key and an encryption key, and during a rotation it has an old signing key and a new one. To tell a verifier which key signed a token, it puts a key id, kid, in the token’s header. The same kid appears on one key in the issuer’s JWKS.

Matching the two is the first step of verification. When it goes wrong, the error messages from libraries are often vague: “no matching key”, “invalid signature”, “unable to find a signing key”. This tool shows the match, or the lack of one, key by key.

How a verifier picks the key

  1. Read kid and alg from the token’s header.
  2. Find the key in the set with the same kid.
  3. Check that the key type fits the algorithm: RSA for RS and PS, EC for ES with the right curve, OKP for EdDSA.
  4. Check that the key’s alg, if set, equals the token’s alg.
  5. Check that the key’s use, if set, is sig, and that key_ops, if set, allows verify.
  6. Verify the signature with that key.

If the token has no kid, RFC 7515 leaves it to the verifier. A common approach is to try every key that fits the algorithm.

Token algKey needed
RS256, RS384, RS512, PS256, PS384, PS512kty: RSA
ES256kty: EC, crv: P-256
ES384kty: EC, crv: P-384
ES512kty: EC, crv: P-521
EdDSAkty: OKP, crv: Ed25519
HS256, HS384, HS512A shared secret (kty: oct), never in a public set

What this tool does

  • Reads the token’s kid and alg.
  • Lists every key in the set with its id, type, curve, algorithm and use.
  • Marks each key as fitting or not, with the reasons: wrong kid, wrong type, wrong curve, a different alg, use: enc, or key_ops without verify.
  • Verifies the signature with the fitting key and reports the result.

The sample shows an ES256 token next to a key set with an RSA signing key, an EC signing key and an RSA encryption key, so you see one fit and two mismatches.

How to match a token

  1. Paste the token.
  2. Paste the key set. For Keycloak, copy the JSON from https://<host>/realms/<realm>/protocol/openid-connect/certs.
  3. Read the match result and the signature result.
  4. If nothing fits, read the reasons in the table to see why.

Key ids in Keycloak

Each key in a Keycloak realm has a kid, shown in the admin console under Realm settings, Keys, in the Kid column. The certs endpoint lists the same ids. Tokens carry the kid of the active key that signed them, which by default is the RSA key used for RS256.

Several signing keys

A realm can hold several keys for the same algorithm. The active key with the highest priority signs new tokens. Passive keys do not sign but are still listed so tokens they signed earlier can be verified. Disabled keys are not used at all.

Rotating keys safely

  1. Add a new key provider with a higher priority than the current one.
  2. New tokens now carry the new kid; the old key still verifies older tokens.
  3. Make the old key passive, so it stops signing but stays listed.
  4. When every token signed with the old key has expired, disable or delete it.

Use cases

“No matching key” after a rotation

Your API cached the old key set. Paste a new token and the current set: if the key fits here, the API needs to refresh its cache when it sees an unknown kid.

Tokens from the wrong place

A kid that appears in no key means the token came from another realm, another environment, or another issuer altogether. Check iss in the JWT decoder.

Library picks the encryption key

Some libraries pick the first RSA key in the set. In a Keycloak set that can be the enc key. The table shows which keys are for encryption so you can configure the library to select by kid.

Mismatch reasons explained

ReasonWhat it means
kid does not matchThis key is not the one the token names
key type RSA, ES256 needs ECThe token was signed with another kind of key
curve P-384, ES256 needs P-256Right kind of key, wrong size
key is for RSA-OAEPThe key’s alg is for something else
use is "enc", not "sig"An encryption key; never verify signatures with it

Caching key sets the right way

Fetching the key set on every request is slow and puts load on the issuer. Caching it forever breaks after a rotation. The middle path, which most good libraries take, looks like this.

  1. Fetch the key set from jwks_uri when the service starts.
  2. For each token, look up its kid in the cached set.
  3. If the kid is missing, fetch the set again, but not more than once every few seconds or minutes.
  4. If it is still missing, reject the token.
  5. Refresh the whole set on a regular interval too, so removed keys stop being trusted.

The rate limit in step 3 matters: without it, an attacker can send tokens with random key ids and make your service hammer the issuer.

What a library error usually means

Error wordingUsual meaning
No matching key, kid not foundThe kid is not in the cached set: stale cache or foreign token
Invalid signatureKey found, but the signature does not verify with it
Unsupported algorithmThe token’s alg is not in the library’s allowed list
Key type mismatchThe selected key is RSA but the token is ES256, or the reverse

Pasting the token and the current key set here separates the first case from the others in a few seconds.

If your verifier logs the kid of each rejected token, comparing those values with the current key set shows at once whether the rejections come from a rotation, a foreign issuer, or random probing.

Reading the sample

The sample token is signed with ES256 and names the key sample-acme-es256. The sample key set, made up for the realm acme, holds three keys. The RSA signing key fails on kid and on key type, the RSA encryption key fails on kid, type, alg and use, and the EC key fits on every count. The signature then verifies with that key.

Security notes

Treat kid as an opaque lookup value. Never build a file path, URL or database query from it without strict checks; attackers can put anything in a header. The security checker flags kid values with unusual characters.

Take key sets only from the issuer you trust, over HTTPS, and never from a URL inside the token.

The sample tokens and key set on this page are made up for the realm acme. Their private keys are not published anywhere.

Best practices

  • Select keys by kid, then confirm type, curve, alg and use.
  • Refresh the cached key set when an unknown kid arrives, with a rate limit.
  • Keep old public keys listed until every token they signed has expired.
  • Give every key in a set a unique kid.
Questions

Frequently asked questions

What if the token has no kid?

Then every key of the right type is a candidate. The tool tries each one and reports the one that verifies.

Why does a key with the right kid not fit?

Its type, curve or `alg` differs from the token’s `alg`, or its use is `enc`. An encryption key must not be used to verify signatures.

Where does Keycloak show key ids?

In the admin console under Realm settings, Keys, and in the realm’s certs endpoint. Both list the same `kid` values.

Why did my API start failing after a key rotation?

It probably cached the old key set and does not refresh it for unknown `kid` values. Refresh the cache when a token names a key you do not have.

Can two keys have the same kid?

They should not. RFC 7517 says key ids should be unique within a set, otherwise a verifier cannot tell which key to use.

Is the kid secret?

No. It is a public label that appears in every token and in the public key set.

How long should I keep an old key after rotating?

At least until every token it signed has expired. For access tokens that is minutes; tokens with longer lives need the key for longer.

Can an attacker choose the kid?

Yes, anyone can write any `kid` into a header. That is harmless as long as the verifier only looks keys up in a trusted set and never uses the value in a file path, URL or query.

Does Keycloak put a kid in every token?

Yes. Access and ID tokens carry the `kid` of the realm key that signed them, matching a key in the realm’s certs endpoint.

Rather not run Keycloak yourself?

We run it for you on a cluster of your own, with backups, upgrades and written support. Starter and Premium begin with a 7-day trial.