Created
August 24, 2025 21:10
-
-
Save carvalhochris/0a52d50775fff8f20103f2625a4a8419 to your computer and use it in GitHub Desktop.
Python Decode Auth0 JWT with JWK Endpoint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.conf import settings | |
| from django.http import HttpRequest | |
| import requests | |
| from joserfc.jwk import KeySet | |
| from joserfc import jwt | |
| from joserfc.jwt import Token | |
| from requests import Response | |
| from project.types import EmailOrNot, OptionalEmail, OptionalToken | |
| def token_in_email_out(request: HttpRequest) -> EmailOrNot: | |
| token: OptionalToken = request.headers.get("Authorization") | |
| if not token: | |
| return None | |
| res: Response = requests.get( | |
| f"https://{settings.AUTH0_DOMAIN}/.well-known/jwks.json" | |
| ) | |
| key_set: KeySet = KeySet.import_key_set(res.json()) | |
| decoded: Token = jwt.decode(token, key_set) | |
| email: OptionalEmail = decoded.claims.get("email") | |
| if email: | |
| return email | |
| return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment