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
- Read
kidandalgfrom the token’s header. - Find the key in the set with the same
kid. - Check that the key type fits the algorithm: RSA for RS and PS, EC for ES with the right curve, OKP for EdDSA.
- Check that the key’s
alg, if set, equals the token’salg. - Check that the key’s
use, if set, issig, and thatkey_ops, if set, allowsverify. - 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 alg | Key needed |
|---|---|
| RS256, RS384, RS512, PS256, PS384, PS512 | kty: RSA |
| ES256 | kty: EC, crv: P-256 |
| ES384 | kty: EC, crv: P-384 |
| ES512 | kty: EC, crv: P-521 |
| EdDSA | kty: OKP, crv: Ed25519 |
| HS256, HS384, HS512 | A shared secret (kty: oct), never in a public set |
What this tool does
- Reads the token’s
kidandalg. - 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 differentalg,use: enc, orkey_opswithoutverify. - 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
- Paste the token.
- Paste the key set. For Keycloak, copy the JSON from
https://<host>/realms/<realm>/protocol/openid-connect/certs. - Read the match result and the signature result.
- 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
- Add a new key provider with a higher priority than the current one.
- New tokens now carry the new
kid; the old key still verifies older tokens. - Make the old key passive, so it stops signing but stays listed.
- 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
| Reason | What it means |
|---|---|
| kid does not match | This key is not the one the token names |
| key type RSA, ES256 needs EC | The token was signed with another kind of key |
| curve P-384, ES256 needs P-256 | Right kind of key, wrong size |
| key is for RSA-OAEP | The 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.
- Fetch the key set from
jwks_uriwhen the service starts. - For each token, look up its
kidin the cached set. - If the
kidis missing, fetch the set again, but not more than once every few seconds or minutes. - If it is still missing, reject the token.
- 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 wording | Usual meaning |
|---|---|
| No matching key, kid not found | The kid is not in the cached set: stale cache or foreign token |
| Invalid signature | Key found, but the signature does not verify with it |
| Unsupported algorithm | The token’s alg is not in the library’s allowed list |
| Key type mismatch | The 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,alganduse. - Refresh the cached key set when an unknown
kidarrives, 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.