Skip to content

Commit 78a4f47

Browse files
committed
[Identity] Handle MSAL string response scenario
Signed-off-by: Paul Van Eck <[email protected]>
1 parent 54ee3ae commit 78a4f47

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

Diff for: sdk/identity/azure-identity/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
### Bugs Fixed
1212

13+
- Fixed an issue with error handling in MSAL-based credentials when the response content is a string rather than a dictionary. ([#40281](https://github.com/Azure/azure-sdk-for-python/pull/40281))
14+
1315
### Other Changes
1416

1517
## 1.21.0 (2025-03-11)

Diff for: sdk/identity/azure-identity/azure/identity/_internal/msal_client.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ def _store_auth_error(self, response: PipelineResponse) -> None:
131131
# response to an auth request, so no credential will want to include it with an exception
132132
content = response.context.get(ContentDecodePolicy.CONTEXT_NAME)
133133
if content and "error" in content:
134-
self._local.error = (content["error"], response.http_response)
134+
error_code = content["error"] if isinstance(content, dict) else "oauth_error"
135+
self._local.error = (error_code, response.http_response)
135136

136137
def __getstate__(self) -> Dict[str, Any]: # pylint:disable=client-method-name-no-double-underscore
137138
state = self.__dict__.copy()

Diff for: sdk/identity/azure-identity/tests/test_msal_client.py

+25
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,28 @@ def test_get_error_response():
5050
assert response is second_response
5151

5252
assert not client.get_error_response(first_result)
53+
54+
55+
def test_string_response_content():
56+
"""Test client handling string response content with error message"""
57+
# String response content with error message
58+
string_error = "Some string error message containing the word error"
59+
response = mock.Mock(status_code=401, headers={})
60+
response.text = lambda encoding=None: string_error
61+
response.headers["content-type"] = "text/plain"
62+
response.content_type = "text/plain"
63+
64+
# Create a transport that returns the string response
65+
transport = validating_transport(
66+
requests=[Request(url="https://localhost")],
67+
responses=[response],
68+
)
69+
70+
client = MsalClient(transport=transport)
71+
72+
# Make a request to store the auth error.
73+
client.get("https://localhost")
74+
error_code, saved_response = getattr(client._local, "error", (None, None))
75+
76+
assert error_code == "oauth_error"
77+
assert saved_response is response

0 commit comments

Comments
 (0)