Skip to content

Commit 37b031e

Browse files
Copilotxiangyan99
andcommitted
Refine claims challenge handling to ignore whitespace-only claims and add comprehensive tests
Co-authored-by: xiangyan99 <[email protected]>
1 parent 1541bfd commit 37b031e

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

sdk/identity/azure-identity/azure/identity/_credentials/azure_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def get_token(
113113
:raises ~azure.core.exceptions.ClientAuthenticationError: the credential invoked the Azure CLI but didn't
114114
receive an access token.
115115
"""
116-
if claims:
116+
if claims and claims.strip():
117117
raise CredentialUnavailableError(f"Fail to get token, please run az login --claims-challenge {claims}")
118118

119119
options: TokenRequestOptions = {}
@@ -144,7 +144,7 @@ def get_token_info(self, *scopes: str, options: Optional[TokenRequestOptions] =
144144
:raises ~azure.core.exceptions.ClientAuthenticationError: the credential invoked the Azure CLI but didn't
145145
receive an access token.
146146
"""
147-
if options and options.get("claims"):
147+
if options and options.get("claims") and options.get("claims").strip():
148148
claims = options["claims"]
149149
raise CredentialUnavailableError(f"Fail to get token, please run az login --claims-challenge {claims}")
150150

sdk/identity/azure-identity/azure/identity/aio/_credentials/azure_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ async def get_token(
104104
:raises ~azure.core.exceptions.ClientAuthenticationError: the credential invoked the Azure CLI but didn't
105105
receive an access token.
106106
"""
107-
if claims:
107+
if claims and claims.strip():
108108
raise CredentialUnavailableError(f"Fail to get token, please run az login --claims-challenge {claims}")
109109

110110
# only ProactorEventLoop supports subprocesses on Windows (and it isn't the default loop on Python < 3.8)
@@ -139,7 +139,7 @@ async def get_token_info(self, *scopes: str, options: Optional[TokenRequestOptio
139139
:raises ~azure.core.exceptions.ClientAuthenticationError: the credential invoked the Azure CLI but didn't
140140
receive an access token.
141141
"""
142-
if options and options.get("claims"):
142+
if options and options.get("claims") and options.get("claims").strip():
143143
claims = options["claims"]
144144
raise CredentialUnavailableError(f"Fail to get token, please run az login --claims-challenge {claims}")
145145

sdk/identity/azure-identity/tests/test_cli_credential.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,10 @@ def test_empty_claims_does_not_raise_error(get_token_method):
432432
# Test with None explicitly
433433
token = AzureCliCredential().get_token("scope", claims=None)
434434
assert token.token == "token"
435+
436+
# Test with whitespace-only string
437+
token = AzureCliCredential().get_token("scope", claims=" ")
438+
assert token.token == "token"
435439
else: # get_token_info
436440
# Test with None options
437441
token = AzureCliCredential().get_token_info("scope")
@@ -448,3 +452,7 @@ def test_empty_claims_does_not_raise_error(get_token_method):
448452
# Test with empty string claims in options
449453
token = AzureCliCredential().get_token_info("scope", options={"claims": ""})
450454
assert token.token == "token"
455+
456+
# Test with whitespace-only claims in options
457+
token = AzureCliCredential().get_token_info("scope", options={"claims": " "})
458+
assert token.token == "token"

sdk/identity/azure-identity/tests/test_cli_credential_async.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,10 @@ async def test_empty_claims_does_not_raise_error(get_token_method):
434434
# Test with None explicitly
435435
token = await AzureCliCredential().get_token("scope", claims=None)
436436
assert token.token == "access-token"
437+
438+
# Test with whitespace-only string
439+
token = await AzureCliCredential().get_token("scope", claims=" ")
440+
assert token.token == "access-token"
437441
else: # get_token_info
438442
# Test with None options
439443
token = await AzureCliCredential().get_token_info("scope")
@@ -450,3 +454,7 @@ async def test_empty_claims_does_not_raise_error(get_token_method):
450454
# Test with empty string claims in options
451455
token = await AzureCliCredential().get_token_info("scope", options={"claims": ""})
452456
assert token.token == "access-token"
457+
458+
# Test with whitespace-only claims in options
459+
token = await AzureCliCredential().get_token_info("scope", options={"claims": " "})
460+
assert token.token == "access-token"

0 commit comments

Comments
 (0)