-
-
Notifications
You must be signed in to change notification settings - Fork 703
Description
Summary.
I've hit a problem in the wild with JWTs being signed without a kid
and the corresponding JWK also not containing a kid
. This appears to be a valid use case:
The "kid" (key ID) parameter is used to match a specific key. This
is used, for instance, to choose among a set of keys within a JWK Set
during key rollover.
...
Use of this member is OPTIONAL.
IE it's only functionally necessary to include one when there is some ambiguity around which key. Eg: during key rollover. At other times, when there is only one key, neither the JWT nor the JWK need to include one.
It appears to be valid to match a token with no kid
against any signing key on a JWK. There is no specification for what to do in the case of ambiguity that I can see, but it seems reasonable to fail validation in that situation.
What you expected.
If there is not kid
in the token header and there is exactly one signing key on the JWK then the following code should still work:
jwks_client = jwt.jwks_client.PyJWKClient(config.jwk_url)
signing_key = jwks_client.get_signing_key_from_jwt(token)
decoded = jwt.decode_complete(token, signing_key)
This should:
- Fetch the JWK
- Filter a list of signing keys (including keys with no
kid
) - If the token contains a
kid
it should match on thekid
- If the token does not contain a
kid
it verify the list of signing keys contains just one key and verify against that.
System Information
$ python -m jwt.help
{
"cryptography": {
"version": "45.0.4"
},
"implementation": {
"name": "CPython",
"version": "3.12.0"
},
"platform": {
"release": "24.5.0",
"system": "Darwin"
},
"pyjwt": {
"version": "2.10.1"
}
}
This appears to be a related to #857