PyJWT Key Fetcher is an async Python library designed to fetch JSON Web Key Sets (JWKS) for JWT token verification. It automatically retrieves issuer configurations (e.g., from OpenID Connect discovery endpoints) to locate JWKS URIs and fetch the correct public keys. This library acts as an improved async replacement for `PyJWKClient` from PyJWT. The current version is 0.8.0, and it maintains a relatively active release cadence with several updates per year.
pip install pyjwt-key-fetcherVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to use `AsyncKeyFetcher` to retrieve a signing key from a JWT's issuer, and then use that key with `PyJWT` to decode and verify the token. It includes `valid_issuers` for security and explicitly passes `audience` and `issuer` to `jwt.decode` for full validation.
Update method/class names (e.g., `fetcher.get_configuration()` instead of `fetcher.get_openid_configuration()`, `Provider` instead of `OpenIDProvider`). Refer to the v0.3.0 changelog for all renamed symbols.
Replace `OpenIDConfigurationTypeDef` with `ConfigurationTypeDef` in type hints or direct references.
Adjust caching parameters (`cache_maxsize`, `cache_ttl`) during `AsyncKeyFetcher` initialization to match your security requirements and acceptable latency for key revocation propagation. E.g., `AsyncKeyFetcher(cache_ttl=600)` for 10-minute cache.
Always pass `audience` and `issuer` to `jwt.decode()` after fetching the key, ensuring they match the expected values for your application. E.g., `jwt.decode(..., audience=my_app_aud, issuer=expected_issuer, **key_entry)`.
The method `get_openid_configuration` was renamed to `get_configuration` in v0.3.0. Use `await fetcher.get_configuration(token)` instead.
Ensure all calls to `AsyncKeyFetcher` methods (like `get_key`, `get_configuration`) are prefixed with `await`. Also, `AsyncKeyFetcher` itself is not awaitable; you instantiate it directly and then call its async methods.
Ensure the `audience` parameter passed to `jwt.decode()` exactly matches the `aud` claim in the JWT. For production, never set `verify_aud=False`.
Ensure the `issuer` parameter passed to `jwt.decode()` exactly matches the `iss` claim in the JWT. For production, never set `verify_iss=False`.