8
8
import logging
9
9
10
10
import pymsalruntime # See https://github.com/AzureAD/microsoft-authentication-library-for-cpp/pull/2419/files#diff-d5ea5122ff04e14411a4f695895c923daba73c117d6c8ceb19c4fa3520c3c08a
11
+ import win32gui # Came from package pywin32
11
12
12
13
13
14
logger = logging .getLogger (__name__ )
@@ -32,6 +33,19 @@ def _read_account_by_id(account_id):
32
33
callback_data .signal .wait ()
33
34
return callback_data .auth_result
34
35
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
+
35
49
def _signin_silently (authority , client_id , scope ):
36
50
params = pymsalruntime .MSALRuntimeAuthParameters (client_id , authority )
37
51
params .set_requested_scopes (scope or "https://graph.microsoft.com/.default" )
@@ -43,13 +57,25 @@ def _signin_silently(authority, client_id, scope):
43
57
callback_data .signal .wait ()
44
58
return callback_data .auth_result
45
59
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" )
47
69
callback_data = _CallbackData ()
48
70
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
50
75
lambda result , callback_data = callback_data : callback_data .complete (result ))
51
76
callback_data .signal .wait ()
52
- return callback_data .auth_result
77
+ return _convert_result (callback_data .auth_result )
78
+
53
79
54
80
def _acquire_token_silently (authority , client_id , account , scope ):
55
81
params = pymsalruntime .MSALRuntimeAuthParameters (client_id , authority )
@@ -60,18 +86,10 @@ def _acquire_token_silently(authority, client_id, account, scope):
60
86
"correlation" , # TODO
61
87
lambda result , callback_data = callback_data : callback_data .complete (result ))
62
88
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
+
73
91
74
- def _acquire_token_interactive (
92
+ def _acquire_token_interactively (
75
93
authority ,
76
94
client_id ,
77
95
account ,
@@ -92,7 +110,8 @@ def _acquire_token_interactive(
92
110
params .set_claims (claims_challenge )
93
111
# TODO: Wire up other input parameters too
94
112
callback_data = _CallbackData ()
95
- pymsalruntime .signin_interactively (
113
+ pymsalruntime .acquire_token_interactively (
114
+ window , # TODO
96
115
params ,
97
116
"correlation" , # TODO
98
117
account ,
@@ -105,31 +124,14 @@ def acquire_token_interactive(
105
124
authority , # type: str
106
125
client_id , # type: str
107
126
scopes , # type: list[str]
127
+ login_hint = None ,
108
128
** kwargs ):
109
129
"""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 )
133
135
134
136
135
137
def acquire_token_silent (
0 commit comments