5
5
6
6
Usage 1: Run it on the fly.
7
7
python -m msal
8
+ Note: We choose to not define a console script to avoid name conflict.
8
9
9
10
Usage 2: Build an all-in-one executable file for bug bash.
10
11
shiv -e msal.__main__._main -o msaltest-on-os-name.pyz .
11
- Note: We choose to not define a console script to avoid name conflict.
12
12
"""
13
- import base64 , getpass , json , logging , sys , msal
13
+ import base64 , getpass , json , logging , sys , os , atexit , msal
14
+
15
+ _token_cache_filename = "msal_cache.bin"
16
+ global_cache = msal .SerializableTokenCache ()
17
+ atexit .register (lambda :
18
+ open (_token_cache_filename , "w" ).write (global_cache .serialize ())
19
+ # Hint: The following optional line persists only when state changed
20
+ if global_cache .has_state_changed else None
21
+ )
14
22
15
23
_AZURE_CLI = "04b07795-8ddb-461a-bbee-02f9e1bf7b46"
16
24
_VISUAL_STUDIO = "04f0c124-f2bc-4f59-8241-bf6df9866bbd"
@@ -66,7 +74,7 @@ def _select_account(app):
66
74
if accounts :
67
75
return _select_options (
68
76
accounts ,
69
- option_renderer = lambda a : a ["username" ],
77
+ option_renderer = lambda a : "{}, came from {}" . format ( a ["username" ], a [ "account_source" ]) ,
70
78
header = "Account(s) already signed in inside MSAL Python:" ,
71
79
)
72
80
else :
@@ -76,7 +84,7 @@ def _acquire_token_silent(app):
76
84
"""acquire_token_silent() - with an account already signed into MSAL Python."""
77
85
account = _select_account (app )
78
86
if account :
79
- print_json (app .acquire_token_silent (
87
+ print_json (app .acquire_token_silent_with_error (
80
88
_input_scopes (),
81
89
account = account ,
82
90
force_refresh = _input_boolean ("Bypass MSAL Python's token cache?" ),
@@ -122,6 +130,15 @@ def _acquire_token_by_username_password(app):
122
130
print_json (app .acquire_token_by_username_password (
123
131
_input ("username: " ), getpass .getpass ("password: " ), scopes = _input_scopes ()))
124
132
133
+ def _acquire_token_by_device_flow (app ):
134
+ """acquire_token_by_device_flow() - Note that this one does not go through broker"""
135
+ flow = app .initiate_device_flow (scopes = _input_scopes ())
136
+ print (flow ["message" ])
137
+ sys .stdout .flush () # Some terminal needs this to ensure the message is shown
138
+ input ("After you completed the step above, press ENTER in this console to continue..." )
139
+ result = app .acquire_token_by_device_flow (flow ) # By default it will block
140
+ print_json (result )
141
+
125
142
_JWK1 = """{"kty":"RSA", "n":"2tNr73xwcj6lH7bqRZrFzgSLj7OeLfbn8216uOMDHuaZ6TEUBDN8Uz0ve8jAlKsP9CQFCSVoSNovdE-fs7c15MxEGHjDcNKLWonznximj8pDGZQjVdfK-7mG6P6z-lgVcLuYu5JcWU_PeEqIKg5llOaz-qeQ4LEDS4T1D2qWRGpAra4rJX1-kmrWmX_XIamq30C9EIO0gGuT4rc2hJBWQ-4-FnE1NXmy125wfT3NdotAJGq5lMIfhjfglDbJCwhc8Oe17ORjO3FsB5CLuBRpYmP7Nzn66lRY3Fe11Xz8AEBl3anKFSJcTvlMnFtu3EpD-eiaHfTgRBU7CztGQqVbiQ", "e":"AQAB"}"""
126
143
_SSH_CERT_DATA = {"token_type" : "ssh-cert" , "key_id" : "key1" , "req_cnf" : _JWK1 }
127
144
_SSH_CERT_SCOPE = ["https://pas.windows.net/CheckMyAccess/Linux/.default" ]
@@ -182,6 +199,27 @@ def _exit(app):
182
199
183
200
def _main ():
184
201
print ("Welcome to the Msal Python {} Tester (Experimental)\n " .format (msal .__version__ ))
202
+ cache_choice = _select_options ([
203
+ {
204
+ "choice" : "empty" ,
205
+ "desc" : "Start with an empty token cache. Suitable for one-off tests." ,
206
+ },
207
+ {
208
+ "choice" : "reuse" ,
209
+ "desc" : "Reuse the previous token cache {} (if any) "
210
+ "which was created during last test app exit. "
211
+ "Useful for testing acquire_token_silent() repeatedly" .format (
212
+ _token_cache_filename ),
213
+ },
214
+ ],
215
+ option_renderer = lambda o : o ["desc" ],
216
+ header = "What token cache state do you want to begin with?" ,
217
+ accept_nonempty_string = False )
218
+ if cache_choice ["choice" ] == "reuse" and os .path .exists (_token_cache_filename ):
219
+ try :
220
+ global_cache .deserialize (open (_token_cache_filename , "r" ).read ())
221
+ except IOError :
222
+ pass # Use empty token cache
185
223
chosen_app = _select_options ([
186
224
{"client_id" : _AZURE_CLI , "name" : "Azure CLI (Correctly configured for MSA-PT)" },
187
225
{"client_id" : _VISUAL_STUDIO , "name" : "Visual Studio (Correctly configured for MSA-PT)" },
@@ -207,6 +245,7 @@ def _main():
207
245
),
208
246
enable_broker_on_windows = enable_broker ,
209
247
enable_pii_log = enable_pii_log ,
248
+ token_cache = global_cache ,
210
249
)
211
250
if enable_debug_log :
212
251
logging .basicConfig (level = logging .DEBUG )
@@ -215,6 +254,7 @@ def _main():
215
254
_acquire_token_silent ,
216
255
_acquire_token_interactive ,
217
256
_acquire_token_by_username_password ,
257
+ _acquire_token_by_device_flow ,
218
258
_acquire_ssh_cert_silently ,
219
259
_acquire_ssh_cert_interactive ,
220
260
_acquire_pop_token_interactive ,
0 commit comments