Skip to content

Instantly share code, notes, and snippets.

@carvalhochris
Created August 24, 2025 21:10
Show Gist options
  • Select an option

  • Save carvalhochris/0a52d50775fff8f20103f2625a4a8419 to your computer and use it in GitHub Desktop.

Select an option

Save carvalhochris/0a52d50775fff8f20103f2625a4a8419 to your computer and use it in GitHub Desktop.
Python Decode Auth0 JWT with JWK Endpoint
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