25 lines
676 B
Python
25 lines
676 B
Python
import base64
|
|
import json
|
|
|
|
import structlog
|
|
|
|
logger = structlog.get_logger(__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}", exc_info=True, label="sso"
|
|
)
|
|
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)}"
|