Skip to content

Commit 2025f95

Browse files
baku2sanpvaneck
andauthored
fix: handle URL-safe base64 decoding for JWT (#38991)
* fix: handle URL-safe base64 decoding for JWT - Updated the JWT decoding logic to use URL-safe base64 decoding. - Added padding to the base64 encoded string to ensure proper decoding. - This fixes the issue where UTF-8 decoding errors occurred due to missing padding in the base64 string. Changes: - Replaced `base64.decodebytes` with `base64.urlsafe_b64decode`. - Added logic to calculate and append necessary padding to the base64 string. * More concise way as requested * Extend changes to aio decorators.py as requested * format by black * Update sdk/identity/azure-identity/azure/identity/_internal/decorators.py Co-authored-by: Paul Van Eck <[email protected]> * Update sdk/identity/azure-identity/azure/identity/aio/_internal/decorators.py Co-authored-by: Paul Van Eck <[email protected]> * Formatted code using Black as specified in ../../../eng/tox/tox.ini with the designated version --------- Co-authored-by: Paul Van Eck <[email protected]>
1 parent 095eef6 commit 2025f95

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

sdk/identity/azure-identity/azure/identity/_internal/decorators.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ def wrapper(*args, **kwargs):
2222
try:
2323
token = fn(*args, **kwargs)
2424
_LOGGER.log(
25-
logging.DEBUG if within_credential_chain.get() else logging.INFO, "%s succeeded", fn.__qualname__
25+
logging.DEBUG if within_credential_chain.get() else logging.INFO,
26+
"%s succeeded",
27+
fn.__qualname__,
2628
)
2729
if _LOGGER.isEnabledFor(logging.DEBUG):
2830
try:
29-
base64_meta_data = token.token.split(".")[1].encode("utf-8") + b"=="
30-
json_bytes = base64.decodebytes(base64_meta_data)
31+
base64_meta_data = token.token.split(".")[1]
32+
padding_needed = -len(base64_meta_data) % 4
33+
if padding_needed:
34+
base64_meta_data += "=" * padding_needed
35+
json_bytes = base64.urlsafe_b64decode(base64_meta_data)
3136
json_string = json_bytes.decode("utf-8")
3237
json_dict = json.loads(json_string)
3338
upn = json_dict.get("upn", "unavailableUpn")

sdk/identity/azure-identity/azure/identity/aio/_internal/decorators.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@ async def wrapper(*args, **kwargs):
2121
try:
2222
token = await fn(*args, **kwargs)
2323
_LOGGER.log(
24-
logging.DEBUG if within_credential_chain.get() else logging.INFO, "%s succeeded", fn.__qualname__
24+
logging.DEBUG if within_credential_chain.get() else logging.INFO,
25+
"%s succeeded",
26+
fn.__qualname__,
2527
)
2628
if _LOGGER.isEnabledFor(logging.DEBUG):
2729
try:
28-
base64_meta_data = token.token.split(".")[1].encode("utf-8") + b"=="
29-
json_bytes = base64.decodebytes(base64_meta_data)
30+
base64_meta_data = token.token.split(".")[1]
31+
padding_needed = -len(base64_meta_data) % 4
32+
if padding_needed:
33+
base64_meta_data += "=" * padding_needed
34+
json_bytes = base64.urlsafe_b64decode(base64_meta_data)
3035
json_string = json_bytes.decode("utf-8")
3136
json_dict = json.loads(json_string)
3237
upn = json_dict.get("upn", "unavailableUpn")

0 commit comments

Comments
 (0)