Skip to content

Commit 4ef6625

Browse files
Fixed an issue when missing padding in base64 decode (#17188)
- Apparently JWT doesn't use base64 padding and the Python base64 decoder requires it. So, we add the max padding and the decoder trims it properly.
1 parent 82fd6bc commit 4ef6625

File tree

2 files changed

+17
-2
lines changed
  • sdk/mixedreality/azure-mixedreality-authentication

2 files changed

+17
-2
lines changed

sdk/mixedreality/azure-mixedreality-authentication/azure/mixedreality/authentication/_utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ def retrieve_jwt_expiration_timestamp(jwt_value):
4242
raise ValueError("Invalid JWT structure. Expected a JWS Compact Serialization formatted value.")
4343

4444
try:
45-
padded_base64_payload = base64.b64decode(parts[1]).decode('utf-8')
45+
# JWT prefers no padding (see https://tools.ietf.org/id/draft-jones-json-web-token-02.html#base64urlnotes).
46+
# We pad the value with the max padding of === to keep our logic simple and allow the base64 decoder to handle
47+
# the value properly. b64decode will properly trim the padding appropriately, but apparently doesn't want to
48+
# handle the addition of padding.
49+
padded_base64_payload = base64.b64decode(parts[1] + "===").decode('utf-8')
4650
payload = json.loads(padded_base64_payload)
4751
except ValueError:
4852
raise ValueError("Unable to decode the JWT.")

sdk/mixedreality/azure-mixedreality-authentication/tests/test_utils.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_generate_cv_base_are_random(self):
2424
assert cv2 is not None
2525
assert cv1 != cv2
2626

27-
def test_retrieve_jwt_expiration_timestamp(self):
27+
def test_retrieve_jwt_expiration_timestamp_with_padding(self):
2828
# Note: The trailing "." on the end indicates an empty signature indicating that this JWT is not signed.
2929
jwt_value = "eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJlbWFpbCI6IkJvYkBjb250b3NvLmNvbSIsImdpdmVuX25hbWUiOiJCb2IiLCJpc3MiOiJodHRwOi8vRGVmYXVsdC5Jc3N1ZXIuY29tIiwiYXVkIjoiaHR0cDovL0RlZmF1bHQuQXVkaWVuY2UuY29tIiwiaWF0IjoiMTYxMDgxMjI1MCIsIm5iZiI6IjE2MTA4MTI1NTAiLCJleHAiOiIxNjEwODk4NjUwIn0=."
3030
expected_expiration_timestamp = 1610898650 # 1/17/2021 3:50:50 PM UTC
@@ -34,6 +34,17 @@ def test_retrieve_jwt_expiration_timestamp(self):
3434
assert actual is not None
3535
assert actual == expected_expiration_timestamp
3636

37+
def test_retrieve_jwt_expiration_timestamp_no_padding(self):
38+
# Note: The trailing "." on the end indicates an empty signature indicating that this JWT is not signed.
39+
# The trailing "=" has been removed to test without base64 padding, which is apparently expected for JWT.
40+
jwt_value = "eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJlbWFpbCI6IkJvYkBjb250b3NvLmNvbSIsImdpdmVuX25hbWUiOiJCb2IiLCJpc3MiOiJodHRwOi8vRGVmYXVsdC5Jc3N1ZXIuY29tIiwiYXVkIjoiaHR0cDovL0RlZmF1bHQuQXVkaWVuY2UuY29tIiwiaWF0IjoiMTYxMDgxMjI1MCIsIm5iZiI6IjE2MTA4MTI1NTAiLCJleHAiOiIxNjEwODk4NjUwIn0."
41+
expected_expiration_timestamp = 1610898650 # 1/17/2021 3:50:50 PM UTC
42+
43+
actual = retrieve_jwt_expiration_timestamp(jwt_value)
44+
45+
assert actual is not None
46+
assert actual == expected_expiration_timestamp
47+
3748
def test_retrieve_jwt_expiration_timestamp_invalid_parameter(self):
3849
with pytest.raises(ValueError):
3950
retrieve_jwt_expiration_timestamp(None)

0 commit comments

Comments
 (0)