Skip to content

Commit 27bbe60

Browse files
committed
Test acquire_token_silent()
1 parent 7319c2f commit 27bbe60

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

msal/wam.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
https://github.com/AzureAD/microsoft-authentication-library-for-cpp/pull/2406/files
55
"""
66
from threading import Event
7+
import json
78
import logging
89

910
import pymsalruntime # See https://github.com/AzureAD/microsoft-authentication-library-for-cpp/pull/2419/files#diff-d5ea5122ff04e14411a4f695895c923daba73c117d6c8ceb19c4fa3520c3c08a
@@ -31,8 +32,9 @@ def _read_account_by_id(account_id):
3132
callback_data.signal.wait()
3233
return callback_data.auth_result
3334

34-
def _signin_silently(authority, client_id):
35+
def _signin_silently(authority, client_id, scope):
3536
params = pymsalruntime.MSALRuntimeAuthParameters(client_id, authority)
37+
params.set_requested_scopes(scope or "https://graph.microsoft.com/.default")
3638
callback_data = _CallbackData()
3739
pymsalruntime.signin_silently(
3840
params,
@@ -49,15 +51,25 @@ def _signin_interactively():
4951
callback_data.signal.wait()
5052
return callback_data.auth_result
5153

52-
def _acquire_token_silently(authority, client_id, account):
54+
def _acquire_token_silently(authority, client_id, account, scope):
5355
params = pymsalruntime.MSALRuntimeAuthParameters(client_id, authority)
56+
params.set_requested_scopes(scope)
5457
callback_data = _CallbackData()
5558
pymsalruntime.signin_silently(
5659
params,
5760
"correlation", # TODO
5861
lambda result, callback_data=callback_data: callback_data.complete(result))
5962
callback_data.signal.wait()
60-
return callback_data.auth_result
63+
result = callback_data.auth_result
64+
return {k: v for k, v in {
65+
"error": result.get_error(),
66+
"access_token": result.get_access_token(),
67+
#"expires_in": result.get_access_token_expiry_time(), # TODO
68+
#"scope": result.get_granted_scopes(), # TODO
69+
"id_token_claims": json.loads(result.get_id_token())
70+
if result.get_id_token() else None,
71+
"account": result.get_account(),
72+
}.items() if v}
6173

6274
def _acquire_token_interactive(
6375
authority,
@@ -95,18 +107,19 @@ def acquire_token_interactive(
95107
scopes, # type: list[str]
96108
**kwargs):
97109
"""MSAL Python's acquire_token_interactive() will call this"""
110+
scope = " ".join(scopes)
98111
result = _signin_silently(authority, client_id)
99-
logger.debug("%s, %s, %s", result, dir(result), result.get_error())
112+
logger.debug("%s, %s, %s, %s, %s", client_id, scope, result, dir(result), result.get_error())
100113
if not result.get_account():
101114
result = _signin_interactively(authority, client_id)
102115
if not result.get_account():
103116
return {"error": result.get_error()} # TODO
104117

105118
result = _acquire_token_silently(
106-
authority, client_id, account, scopes, **kwargs)
119+
authority, client_id, account, scope, **kwargs)
107120
if not result.get_access_token():
108121
result = _acquire_token_interactive(
109-
authority, client_id, account, scopes, **kwargs)
122+
authority, client_id, account, scope, **kwargs)
110123
if not result.get_access_token():
111124
return {"error": result.get_error()} # TODO
112125
# TODO: Also store the tokens and account into MSAL's token cache
@@ -123,9 +136,13 @@ def acquire_token_silent(
123136
authority, # type: str
124137
client_id, # type: str
125138
scopes, # type: list[str]
126-
account,
139+
account=None, # TBD
127140
):
128-
wam_account = _read_account_by_id(account["some_sort_of_id"]) # TODO
141+
scope = " ".join(scopes)
142+
if account:
143+
wam_account = _read_account_by_id(account["some_sort_of_id"]) # TODO
144+
else:
145+
wam_account = _signin_silently(authority, client_id, scope).get_account()
129146
if wam_account:
130-
return _acquire_token_silently(authority, client_id, scopes, wam_account)
147+
return _acquire_token_silently(authority, client_id, wam_account, scope)
131148

tests/test_wam.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,26 @@
77
logging.basicConfig(level=logging.DEBUG)
88

99
class TestWam(unittest.TestCase):
10+
client_id = "04b07795-8ddb-461a-bbee-02f9e1bf7b46" # A well-known app
1011

12+
@unittest.skip("Not yet implemented")
1113
def test_acquire_token_interactive(self):
1214
acquire_token_interactive(
1315
"https://login.microsoftonline.com/common",
14-
"my_client_id",
15-
["foo", "bar"],
16+
#"my_client_id",
17+
"26a7ee05-5602-4d76-a7ba-eae8b7b67941",
18+
#["foo", "bar"],
19+
["https://graph.microsoft.com/.default"],
1620
)
1721

18-
@unittest.skip("Not yet implemented")
1922
def test_acquire_token_silent(self):
20-
acquire_token_silent(
23+
result = acquire_token_silent(
2124
"https://login.microsoftonline.com/common",
22-
"my_client_id",
23-
["foo", "bar"],
24-
{"some_sort_of_id": "placeholder"},
25+
#"my_client_id",
26+
#self.client_id,
27+
"26a7ee05-5602-4d76-a7ba-eae8b7b67941",
28+
["https://graph.microsoft.com/.default"],
29+
#{"some_sort_of_id": "placeholder"}, # TODO
2530
)
31+
self.assertIsNotNone(result.get("access_token"))
32+

0 commit comments

Comments
 (0)