Async library to fetch JWKs for JWT tokens
Slowing down. Last release over 2 years ago, little activity since. Fine for existing code; think twice for a new project.
pip install pyjwt-key-fetcherpyjwt-key-fetcher
Async library to fetch JWKs for JWT tokens.
This library is intended to be used together with
PyJWT to automatically verify keys signed by
for example OpenID Connect providers. It retrieves the iss (issuer) and the kid (key
ID) from the JWT, fetches the configuration, typically from
.well-known/openid-configuration (can be overridden) from the issuer to find out the
jwks_uri (or jwks_url) and fetches that to find the right key.
This should give similar ability to verify keys as for example https://jwt.io/, where you can just paste in a token, and it will automatically reach out and retrieve the key for you.
The AsyncKeyFetcher provided by this library acts as an improved async replacement for
PyJWKClient.
Installation
The package is available on PyPI:
pip install pyjwt-key-fetcher
Usage
Example
import asyncio
import jwt
from pyjwt_key_fetcher import AsyncKeyFetcher
async def main():
fetcher = AsyncKeyFetcher()
# Token and options copied from
# https://pyjwt.readthedocs.io/en/2.6.0/usage.html#retrieve-rsa-signing-keys-from-a-jwks-endpoint
token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5FRTFRVVJCT1RNNE16STVSa0ZETlRZeE9UVTFNRGcyT0Rnd1EwVXpNVGsxUWpZeVJrUkZRdyJ9.eyJpc3MiOiJodHRwczovL2Rldi04N2V2eDlydS5hdXRoMC5jb20vIiwic3ViIjoiYVc0Q2NhNzl4UmVMV1V6MGFFMkg2a0QwTzNjWEJWdENAY2xpZW50cyIsImF1ZCI6Imh0dHBzOi8vZXhwZW5zZXMtYXBpIiwiaWF0IjoxNTcyMDA2OTU0LCJleHAiOjE1NzIwMDY5NjQsImF6cCI6ImFXNENjYTc5eFJlTFdVejBhRTJINmtEME8zY1hCVnRDIiwiZ3R5IjoiY2xpZW50LWNyZWRlbnRpYWxzIn0.PUxE7xn52aTCohGiWoSdMBZGiYAHwE5FYie0Y1qUT68IHSTXwXVd6hn02HTah6epvHHVKA2FqcFZ4GGv5VTHEvYpeggiiZMgbxFrmTEY0csL6VNkX1eaJGcuehwQCRBKRLL3zKmA5IKGy5GeUnIbpPHLHDxr-GXvgFzsdsyWlVQvPX2xjeaQ217r2PtxDeqjlf66UYl6oY6AqNS8DH3iryCvIfCcybRZkc_hdy-6ZMoKT6Piijvk_aXdm7-QQqKJFHLuEqrVSOuBqqiNfVrG27QzAPuPOxvfXTVLXL2jek5meH6n-VWgrBdoMFH93QEszEDowDAEhQPHVs0xj7SIzA"
key_entry = await fetcher.get_key(token)
token = jwt.decode(
jwt=token,
options={"verify_exp": False},
audience="https://expenses-api",
**key_entry
)
print(token)
if __name__ == "__main__":
asyncio.run(main())