Skip to content

Commit 5e2a73b

Browse files
committed
Non-MsalRuntime accounts will fallback gracefully
1 parent 8fcfefa commit 5e2a73b

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

msal/broker.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,22 @@ def _convert_error(error, client_id):
7171

7272

7373
def _read_account_by_id(account_id, correlation_id):
74-
"""Return an instance of MSALRuntimeError or MSALRuntimeAccount, or None"""
74+
"""Return an instance of MSALRuntimeAccount, or log error and return None"""
7575
callback_data = _CallbackData()
7676
pymsalruntime.read_account_by_id(
7777
account_id,
7878
correlation_id,
7979
lambda result, callback_data=callback_data: callback_data.complete(result)
8080
)
8181
callback_data.signal.wait()
82-
return (callback_data.result.get_error() or callback_data.result.get_account()
83-
or None) # None happens when the account was not created by broker
82+
error = callback_data.result.get_error()
83+
if error:
84+
logger.debug("read_account_by_id() error: %s", _convert_error(error, None))
85+
return None
86+
account = callback_data.result.get_account()
87+
if account:
88+
return account
89+
return None # None happens when the account was not created by broker
8490

8591

8692
def _convert_result(result, client_id, expected_token_type=None): # Mimic an on-the-wire response from AAD
@@ -195,8 +201,6 @@ def _acquire_token_silently(
195201
# acquireTokenSilently is expected to fail. - Sam Wilson
196202
correlation_id = correlation_id or _get_new_correlation_id()
197203
account = _read_account_by_id(account_id, correlation_id)
198-
if isinstance(account, pymsalruntime.MSALRuntimeError):
199-
return _convert_error(account, client_id)
200204
if account is None:
201205
return
202206
params = pymsalruntime.MSALRuntimeAuthParameters(client_id, authority)
@@ -220,8 +224,6 @@ def _acquire_token_silently(
220224
def _signout_silently(client_id, account_id, correlation_id=None):
221225
correlation_id = correlation_id or _get_new_correlation_id()
222226
account = _read_account_by_id(account_id, correlation_id)
223-
if isinstance(account, pymsalruntime.MSALRuntimeError):
224-
return _convert_error(account, client_id)
225227
if account is None:
226228
return
227229
callback_data = _CallbackData()

tests/msaltest.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def acquire_token_silent(app):
5959
"""acquire_token_silent() - with an account already signed into MSAL Python."""
6060
account = _select_account(app)
6161
if account:
62-
pprint.pprint(app.acquire_token_silent(
62+
pprint.pprint(app.acquire_token_silent_with_error(
6363
_input_scopes(),
6464
account=account,
6565
force_refresh=_input_boolean("Bypass MSAL Python's token cache?"),
@@ -103,6 +103,15 @@ def acquire_token_by_username_password(app):
103103
pprint.pprint(app.acquire_token_by_username_password(
104104
_input("username: "), getpass.getpass("password: "), scopes=_input_scopes()))
105105

106+
def acquire_token_by_device_flow(app):
107+
"""acquire_token_by_device_flow() - Note that this one does not go through broker"""
108+
flow = app.initiate_device_flow(scopes=_input_scopes())
109+
print(flow["message"])
110+
sys.stdout.flush() # Some terminal needs this to ensure the message is shown
111+
input("After you completed the step above, press ENTER in this console to continue...")
112+
result = app.acquire_token_by_device_flow(flow) # By default it will block
113+
pprint.pprint(result)
114+
106115
_JWK1 = """{"kty":"RSA", "n":"2tNr73xwcj6lH7bqRZrFzgSLj7OeLfbn8216uOMDHuaZ6TEUBDN8Uz0ve8jAlKsP9CQFCSVoSNovdE-fs7c15MxEGHjDcNKLWonznximj8pDGZQjVdfK-7mG6P6z-lgVcLuYu5JcWU_PeEqIKg5llOaz-qeQ4LEDS4T1D2qWRGpAra4rJX1-kmrWmX_XIamq30C9EIO0gGuT4rc2hJBWQ-4-FnE1NXmy125wfT3NdotAJGq5lMIfhjfglDbJCwhc8Oe17ORjO3FsB5CLuBRpYmP7Nzn66lRY3Fe11Xz8AEBl3anKFSJcTvlMnFtu3EpD-eiaHfTgRBU7CztGQqVbiQ", "e":"AQAB"}"""
107116
SSH_CERT_DATA = {"token_type": "ssh-cert", "key_id": "key1", "req_cnf": _JWK1}
108117
SSH_CERT_SCOPE = ["https://pas.windows.net/CheckMyAccess/Linux/.default"]
@@ -176,6 +185,7 @@ def main():
176185
acquire_token_silent,
177186
acquire_token_interactive,
178187
acquire_token_by_username_password,
188+
acquire_token_by_device_flow,
179189
acquire_ssh_cert_silently,
180190
acquire_ssh_cert_interactive,
181191
remove_account,

0 commit comments

Comments
 (0)