Skip to content

Commit 15b8efa

Browse files
committed
POC of acquire_token_interactive()
1 parent 27bbe60 commit 15b8efa

File tree

2 files changed

+42
-44
lines changed

2 files changed

+42
-44
lines changed

msal/wam.py

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import logging
99

1010
import pymsalruntime # See https://github.com/AzureAD/microsoft-authentication-library-for-cpp/pull/2419/files#diff-d5ea5122ff04e14411a4f695895c923daba73c117d6c8ceb19c4fa3520c3c08a
11+
import win32gui # Came from package pywin32
1112

1213

1314
logger = logging.getLogger(__name__)
@@ -32,6 +33,19 @@ def _read_account_by_id(account_id):
3233
callback_data.signal.wait()
3334
return callback_data.auth_result
3435

36+
37+
def _convert_result(result):
38+
return {k: v for k, v in {
39+
"error": result.get_error(),
40+
"access_token": result.get_access_token(),
41+
#"expires_in": result.get_access_token_expiry_time(), # TODO
42+
#"scope": result.get_granted_scopes(), # TODO
43+
"id_token_claims": json.loads(result.get_id_token())
44+
if result.get_id_token() else None,
45+
"account": result.get_account(),
46+
}.items() if v}
47+
48+
3549
def _signin_silently(authority, client_id, scope):
3650
params = pymsalruntime.MSALRuntimeAuthParameters(client_id, authority)
3751
params.set_requested_scopes(scope or "https://graph.microsoft.com/.default")
@@ -43,13 +57,25 @@ def _signin_silently(authority, client_id, scope):
4357
callback_data.signal.wait()
4458
return callback_data.auth_result
4559

46-
def _signin_interactively():
60+
def _signin_interactively(
61+
authority, client_id, scope,
62+
login_hint=None,
63+
window=None,
64+
):
65+
params = pymsalruntime.MSALRuntimeAuthParameters(client_id, authority)
66+
params.set_requested_scopes(scope or "https://graph.microsoft.com/.default")
67+
params.set_redirect_uri(
68+
"https://login.microsoftonline.com/common/oauth2/nativeclient")
4769
callback_data = _CallbackData()
4870
pymsalruntime.signin_interactively(
49-
# TODO: Add other input parameters
71+
window or win32gui.GetDesktopWindow(), # TODO: Remove win32gui
72+
params,
73+
"correlation", # TODO
74+
login_hint or "", # Account hint
5075
lambda result, callback_data=callback_data: callback_data.complete(result))
5176
callback_data.signal.wait()
52-
return callback_data.auth_result
77+
return _convert_result(callback_data.auth_result)
78+
5379

5480
def _acquire_token_silently(authority, client_id, account, scope):
5581
params = pymsalruntime.MSALRuntimeAuthParameters(client_id, authority)
@@ -60,18 +86,10 @@ def _acquire_token_silently(authority, client_id, account, scope):
6086
"correlation", # TODO
6187
lambda result, callback_data=callback_data: callback_data.complete(result))
6288
callback_data.signal.wait()
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}
89+
return _convert_result(callback_data.auth_result)
90+
7391

74-
def _acquire_token_interactive(
92+
def _acquire_token_interactively(
7593
authority,
7694
client_id,
7795
account,
@@ -92,7 +110,8 @@ def _acquire_token_interactive(
92110
params.set_claims(claims_challenge)
93111
# TODO: Wire up other input parameters too
94112
callback_data = _CallbackData()
95-
pymsalruntime.signin_interactively(
113+
pymsalruntime.acquire_token_interactively(
114+
window, # TODO
96115
params,
97116
"correlation", # TODO
98117
account,
@@ -105,31 +124,14 @@ def acquire_token_interactive(
105124
authority, # type: str
106125
client_id, # type: str
107126
scopes, # type: list[str]
127+
login_hint=None,
108128
**kwargs):
109129
"""MSAL Python's acquire_token_interactive() will call this"""
110-
scope = " ".join(scopes)
111-
result = _signin_silently(authority, client_id)
112-
logger.debug("%s, %s, %s, %s, %s", client_id, scope, result, dir(result), result.get_error())
113-
if not result.get_account():
114-
result = _signin_interactively(authority, client_id)
115-
if not result.get_account():
116-
return {"error": result.get_error()} # TODO
117-
118-
result = _acquire_token_silently(
119-
authority, client_id, account, scope, **kwargs)
120-
if not result.get_access_token():
121-
result = _acquire_token_interactive(
122-
authority, client_id, account, scope, **kwargs)
123-
if not result.get_access_token():
124-
return {"error": result.get_error()} # TODO
125-
# TODO: Also store the tokens and account into MSAL's token cache
126-
return {k: v for k, v in {
127-
"access_token": result.get_access_token(),
128-
"token_type": "Bearer", # TODO: TBD
129-
"expires_in": result.get_access_token_expiry_time(),
130-
"id_token": result.get_id_token(),
131-
"scope": result.get_granted_scopes(),
132-
} if v is not None}
130+
return _signin_interactively(
131+
authority,
132+
client_id,
133+
" ".join(scopes),
134+
login_hint=login_hint)
133135

134136

135137
def acquire_token_silent(

tests/test_wam.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,17 @@
99
class TestWam(unittest.TestCase):
1010
client_id = "04b07795-8ddb-461a-bbee-02f9e1bf7b46" # A well-known app
1111

12-
@unittest.skip("Not yet implemented")
1312
def test_acquire_token_interactive(self):
14-
acquire_token_interactive(
13+
result = acquire_token_interactive(
1514
"https://login.microsoftonline.com/common",
16-
#"my_client_id",
1715
"26a7ee05-5602-4d76-a7ba-eae8b7b67941",
18-
#["foo", "bar"],
1916
["https://graph.microsoft.com/.default"],
2017
)
18+
self.assertIsNotNone(result.get("access_token"))
2119

2220
def test_acquire_token_silent(self):
2321
result = acquire_token_silent(
2422
"https://login.microsoftonline.com/common",
25-
#"my_client_id",
26-
#self.client_id,
2723
"26a7ee05-5602-4d76-a7ba-eae8b7b67941",
2824
["https://graph.microsoft.com/.default"],
2925
#{"some_sort_of_id": "placeholder"}, # TODO

0 commit comments

Comments
 (0)