Skip to content

Commit 480457d

Browse files
authored
InteractiveBrowserCredential prompts for account selection (#8470)
1 parent eeb89c0 commit 480457d

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ def _get_token_by_auth_code(self, scopes, **kwargs):
9494
scopes = list(scopes) # type: ignore
9595
request_state = str(uuid.uuid4())
9696
app = self._get_app()
97-
auth_url = app.get_authorization_request_url(scopes, redirect_uri=redirect_uri, state=request_state, **kwargs)
97+
auth_url = app.get_authorization_request_url(
98+
scopes, redirect_uri=redirect_uri, state=request_state, prompt="select_account", **kwargs
99+
)
98100

99101
# open browser to that url
100102
if not webbrowser.open(auth_url):

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from azure.identity import InteractiveBrowserCredential
1212
from azure.identity._internal import AuthCodeRedirectServer
1313
import pytest
14-
from six.moves import urllib
14+
from six.moves import urllib, urllib_parse
1515

1616
from helpers import build_aad_response, mock_response, Request, validating_transport
1717

@@ -23,7 +23,7 @@
2323

2424
@patch("azure.identity._credentials.browser.webbrowser.open")
2525
def test_interactive_credential(mock_open):
26-
mock_open.return_value = True # the real webbrowser.open returns a bool
26+
mock_open.side_effect = _validate_auth_request_url
2727
oauth_state = "state"
2828
client_id = "client-id"
2929
expected_refresh_token = "refresh-token"
@@ -171,3 +171,13 @@ def test_no_browser():
171171
)
172172
with pytest.raises(ClientAuthenticationError, match=r".*browser.*"):
173173
credential.get_token("scope")
174+
175+
176+
def _validate_auth_request_url(url):
177+
parsed_url = urllib_parse.urlparse(url)
178+
params = urllib_parse.parse_qs(parsed_url.query)
179+
assert params.get("prompt") == ["select_account"], "Auth code request doesn't specify 'prompt=select_account'."
180+
181+
# when used as a Mock's side_effect, this method's return value is the Mock's return value
182+
# (the real webbrowser.open returns a bool)
183+
return True

0 commit comments

Comments
 (0)