Skip to content

Commit

Permalink
Fix: Do URL safe base64 decoding if non-URL safe decoding fails
Browse files Browse the repository at this point in the history
Signed-off-by: George J Padayatti <[email protected]>
  • Loading branch information
georgepadayatti committed Oct 22, 2024
1 parent bca2d1f commit 74a688f
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions sdjwt/pex.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,22 @@ def validate_and_deserialise_presentation_submission(

def decode_header_and_claims_in_jwt(token: str) -> Tuple[dict, dict]:
headers_encoded, claims_encoded, _ = token.split(".")
claims_decoded = base64.b64decode(claims_encoded + "=" * (-len(claims_encoded) % 4))
headers_decoded = base64.b64decode(
headers_encoded + "=" * (-len(headers_encoded) % 4)
)
return (json.loads(headers_decoded), json.loads(claims_decoded))
try:
claims_decoded = base64.b64decode(
claims_encoded + "=" * (-len(claims_encoded) % 4)
)
headers_decoded = base64.b64decode(
headers_encoded + "=" * (-len(headers_encoded) % 4)
)
return (json.loads(headers_decoded), json.loads(claims_decoded))
except Exception:
claims_decoded = base64.urlsafe_b64decode(
claims_encoded + "=" * (-len(claims_encoded) % 4)
)
headers_decoded = base64.urlsafe_b64decode(
headers_encoded + "=" * (-len(claims_encoded) % 4)
)
return (json.loads(headers_decoded), json.loads(claims_decoded))


class VpTokenExpiredError(Exception):
Expand Down

0 comments on commit 74a688f

Please sign in to comment.