Skip to content

Commit a230e92

Browse files
authored
InteractiveBrowserCredential is unavailable when it can't bind a port (#11665)
1 parent 661b54c commit a230e92

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

sdk/identity/azure-identity/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Release History
22

33
## 1.4.0b4 (Unreleased)
4+
- `InteractiveBrowserCredential` raises `CredentialUnavailableError` when it
5+
can't start an HTTP server on `localhost`.
6+
([#11665](https://github.com/Azure/azure-sdk-for-python/pull/11665))
47
- When constructing `DefaultAzureCredential`, you can now configure a tenant ID
58
for `InteractiveBrowserCredential`. When none is specified, the credential
69
authenticates users in their home tenants. To specify a different tenant, use

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def _request_token(self, *scopes, **kwargs):
5858
# type: (*str, **Any) -> dict
5959

6060
# start an HTTP server on localhost to receive the redirect
61+
redirect_uri = None
6162
for port in range(8400, 9000):
6263
try:
6364
server = self._server_class(port, timeout=self._timeout)

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from azure.core.exceptions import ClientAuthenticationError
1212
from azure.core.pipeline.policies import SansIOHTTPPolicy
13-
from azure.identity import AuthenticationRequiredError, InteractiveBrowserCredential
13+
from azure.identity import AuthenticationRequiredError, CredentialUnavailableError, InteractiveBrowserCredential
1414
from azure.identity._internal import AuthCodeRedirectServer
1515
from azure.identity._internal.user_agent import USER_AGENT
1616
from msal import TokenCache
@@ -302,6 +302,14 @@ def test_no_browser():
302302
credential.get_token("scope")
303303

304304

305+
def test_cannot_bind_port():
306+
"""get_token should raise CredentialUnavailableError when the redirect listener can't bind a port"""
307+
308+
credential = InteractiveBrowserCredential(server_class=Mock(side_effect=socket.error))
309+
with pytest.raises(CredentialUnavailableError):
310+
credential.get_token("scope")
311+
312+
305313
def _validate_auth_request_url(url):
306314
parsed_url = urllib_parse.urlparse(url)
307315
params = urllib_parse.parse_qs(parsed_url.query)

0 commit comments

Comments
 (0)