Skip to content

Commit 5937f45

Browse files
committed
Add handling for unexpected successful response type
1 parent c0468e1 commit 5937f45

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

packages/auth0_server_python/src/auth0_server_python/auth_server/server_client.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,13 @@ async def backchannel_authentication_grant(self, auth_req_id: str) -> Dict[str,
10721072
interval
10731073
)
10741074

1075-
token_response = response.json()
1075+
try:
1076+
token_response = response.json()
1077+
except json.JSONDecodeError:
1078+
raise ApiError(
1079+
"invalid_response",
1080+
"Failed to parse token response as JSON"
1081+
)
10761082

10771083
# Add required fields if they are missing
10781084
if "expires_in" in token_response and "expires_at" not in token_response:

packages/auth0_server_python/tests/test_server_client.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import pytest
23
import time
34

@@ -1037,6 +1038,29 @@ async def test_backchannel_authentication_grant_error_response(mocker):
10371038
assert 2 == exc.value.interval
10381039
assert "invalid_grant" in str(exc.value.code)
10391040

1041+
@pytest.mark.asyncio
1042+
async def test_backchannel_authentication_grant_json_decode_error(mocker):
1043+
client = ServerClient(
1044+
domain="auth0.local",
1045+
client_id="client_id",
1046+
client_secret="client_secret",
1047+
secret="some-secret"
1048+
)
1049+
client._oauth.metadata = {"token_endpoint": "https://auth0.local/token"}
1050+
1051+
# Mock httpx.AsyncClient.post to return a response whose .json() raises JSONDecodeError
1052+
mock_post = mocker.patch("httpx.AsyncClient.post", new_callable=AsyncMock)
1053+
mock_response = AsyncMock()
1054+
mock_response.status_code = 200
1055+
mock_response.json = MagicMock(side_effect=json.JSONDecodeError("Expecting value", "not json", 0))
1056+
mock_post.return_value = mock_response
1057+
1058+
with pytest.raises(ApiError) as exc:
1059+
await client.backchannel_authentication_grant("auth_req_123")
1060+
1061+
assert exc.value.code == "invalid_response"
1062+
assert "Failed to parse token response as JSON" in str(exc.value)
1063+
10401064
@pytest.mark.asyncio
10411065
async def test_get_token_for_connection_success(mocker):
10421066
client = ServerClient(

0 commit comments

Comments
 (0)