Skip to content

Commit f0a7ed4

Browse files
authored
add using system account support (Azure#34754)
* add using system account support * update * update * update
1 parent feeefb0 commit f0a7ed4

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

.vscode/cspell.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,12 @@
576576
"wammsa"
577577
]
578578
},
579+
{
580+
"filename": "sdk/identity/azure-identity-broker/tests/*.py",
581+
"words": [
582+
"signin"
583+
]
584+
},
579585
{
580586
"filename": "sdk/tables/azure-data-tables/tests/**/*.py",
581587
"words": [

sdk/identity/azure-identity-broker/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Features Added
66

7+
- `InteractiveBrowserBrokerCredential` now supports a `use_operating_system_account` property to enable the use of the currently logged in operating system account for authentication rather than prompting for a credential.
8+
79
### Breaking Changes
810

911
### Bugs Fixed

sdk/identity/azure-identity-broker/azure/identity/broker/_browser.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class InteractiveBrowserBrokerCredential(_InteractiveBrowserCredential):
3737
:keyword int timeout: seconds to wait for the user to complete authentication. Defaults to 300 (5 minutes).
3838
:keyword int parent_window_handle: If your app is a GUI app running on a modern Windows system, you are required to
3939
also provide its window handle so that the sign in UI window will properly pop up on top of your window.
40+
:keyword bool use_operating_system_account: Whether to authenticate with the currently signed in user instead of
41+
prompting the user with a login dialog. Defaults to False.
4042
:keyword bool enable_msa_passthrough: Determines whether Microsoft Account (MSA) passthrough is enabled. Note, this
4143
is only needed for select legacy first-party applications. Defaults to False.
4244
:keyword bool disable_instance_discovery: Determines whether or not instance discovery is performed when attempting
@@ -52,6 +54,7 @@ class InteractiveBrowserBrokerCredential(_InteractiveBrowserCredential):
5254
def __init__(self, **kwargs: Any) -> None:
5355
self._parent_window_handle = kwargs.pop("parent_window_handle", None)
5456
self._enable_msa_passthrough = kwargs.pop("enable_msa_passthrough", False)
57+
self._use_operating_system_account = kwargs.pop("use_operating_system_account", False)
5558
super().__init__(**kwargs)
5659

5760
@wrap_exceptions
@@ -61,6 +64,22 @@ def _request_token(self, *scopes: str, **kwargs: Any) -> Dict:
6164
app = self._get_app(**kwargs)
6265
port = self._parsed_url.port if self._parsed_url else None
6366

67+
if self._use_operating_system_account:
68+
try:
69+
result = app.acquire_token_interactive(
70+
scopes=scopes,
71+
login_hint=self._login_hint,
72+
claims_challenge=claims,
73+
timeout=self._timeout,
74+
prompt=msal.Prompt.NONE,
75+
port=port,
76+
parent_window_handle=self._parent_window_handle,
77+
enable_msa_passthrough=self._enable_msa_passthrough,
78+
)
79+
if "access_token" in result:
80+
return result
81+
except socket.error:
82+
pass
6483
try:
6584
result = app.acquire_token_interactive(
6685
scopes=scopes,

sdk/identity/azure-identity-broker/tests/test_broker.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,24 @@
44
# ------------------------------------
55
import pytest
66
import sys
7+
from unittest.mock import patch, Mock
78
from azure.identity.broker import InteractiveBrowserBrokerCredential
89

910

10-
@pytest.mark.skip("Not compatible with identity 1.15.0b1")
1111
@pytest.mark.skipif(not sys.platform.startswith("win"), reason="tests Windows-specific behavior")
1212
def test_interactive_browser_broker_cred():
1313
cred = InteractiveBrowserBrokerCredential()
1414
assert cred._get_app()._enable_broker
15+
16+
17+
@pytest.mark.skipif(not sys.platform.startswith("win"), reason="tests Windows-specific behavior")
18+
def test_interactive_browser_broker_cred_signed_in_account():
19+
with patch("msal.broker._signin_silently", Mock(return_value="token")) as mock_signin_silently:
20+
try:
21+
cred = InteractiveBrowserBrokerCredential(
22+
parent_window_handle="window_handle", use_operating_system_account=True
23+
)
24+
cred.get_token("scope")
25+
except Exception: # msal raises TypeError which is expected. We are not testing msal here.
26+
pass
27+
assert mock_signin_silently.called

0 commit comments

Comments
 (0)