22 lines
620 B
Python
22 lines
620 B
Python
import base64
|
|
import json
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def decode_jwt(jwt: str):
|
|
jwt_parts = jwt.split(".")
|
|
try:
|
|
payload_bytes = base64.urlsafe_b64decode(_correct_padding(jwt_parts[1]))
|
|
payload = json.loads(payload_bytes.decode("UTF-8"))
|
|
except Exception as e:
|
|
logger.warning(f"OAuthToken error: Could not decode jwt: {e}")
|
|
return None
|
|
return payload
|
|
|
|
|
|
# https://stackoverflow.com/questions/2941995/python-ignore-incorrect-padding-error-when-base64-decoding
|
|
def _correct_padding(data: str) -> str:
|
|
return f"{data}{'=' * (4 - len(data) % 4)}"
|