55
66Usage 1: Run it on the fly.
77 python -m msal
8+ Note: We choose to not define a console script to avoid name conflict.
89
910Usage 2: Build an all-in-one executable file for bug bash.
1011 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.
1212"""
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+ )
1422
1523_AZURE_CLI = "04b07795-8ddb-461a-bbee-02f9e1bf7b46"
1624_VISUAL_STUDIO = "04f0c124-f2bc-4f59-8241-bf6df9866bbd"
@@ -66,7 +74,7 @@ def _select_account(app):
6674 if accounts :
6775 return _select_options (
6876 accounts ,
69- option_renderer = lambda a : a ["username" ],
77+ option_renderer = lambda a : "{}, came from {}" . format ( a ["username" ], a [ "account_source" ]) ,
7078 header = "Account(s) already signed in inside MSAL Python:" ,
7179 )
7280 else :
@@ -76,7 +84,7 @@ def _acquire_token_silent(app):
7684 """acquire_token_silent() - with an account already signed into MSAL Python."""
7785 account = _select_account (app )
7886 if account :
79- print_json (app .acquire_token_silent (
87+ print_json (app .acquire_token_silent_with_error (
8088 _input_scopes (),
8189 account = account ,
8290 force_refresh = _input_boolean ("Bypass MSAL Python's token cache?" ),
@@ -122,6 +130,15 @@ def _acquire_token_by_username_password(app):
122130 print_json (app .acquire_token_by_username_password (
123131 _input ("username: " ), getpass .getpass ("password: " ), scopes = _input_scopes ()))
124132
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+
125142_JWK1 = """{"kty":"RSA", "n":"2tNr73xwcj6lH7bqRZrFzgSLj7OeLfbn8216uOMDHuaZ6TEUBDN8Uz0ve8jAlKsP9CQFCSVoSNovdE-fs7c15MxEGHjDcNKLWonznximj8pDGZQjVdfK-7mG6P6z-lgVcLuYu5JcWU_PeEqIKg5llOaz-qeQ4LEDS4T1D2qWRGpAra4rJX1-kmrWmX_XIamq30C9EIO0gGuT4rc2hJBWQ-4-FnE1NXmy125wfT3NdotAJGq5lMIfhjfglDbJCwhc8Oe17ORjO3FsB5CLuBRpYmP7Nzn66lRY3Fe11Xz8AEBl3anKFSJcTvlMnFtu3EpD-eiaHfTgRBU7CztGQqVbiQ", "e":"AQAB"}"""
126143_SSH_CERT_DATA = {"token_type" : "ssh-cert" , "key_id" : "key1" , "req_cnf" : _JWK1 }
127144_SSH_CERT_SCOPE = ["https://pas.windows.net/CheckMyAccess/Linux/.default" ]
@@ -182,6 +199,27 @@ def _exit(app):
182199
183200def _main ():
184201 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
185223 chosen_app = _select_options ([
186224 {"client_id" : _AZURE_CLI , "name" : "Azure CLI (Correctly configured for MSA-PT)" },
187225 {"client_id" : _VISUAL_STUDIO , "name" : "Visual Studio (Correctly configured for MSA-PT)" },
@@ -207,6 +245,7 @@ def _main():
207245 ),
208246 enable_broker_on_windows = enable_broker ,
209247 enable_pii_log = enable_pii_log ,
248+ token_cache = global_cache ,
210249 )
211250 if enable_debug_log :
212251 logging .basicConfig (level = logging .DEBUG )
@@ -215,6 +254,7 @@ def _main():
215254 _acquire_token_silent ,
216255 _acquire_token_interactive ,
217256 _acquire_token_by_username_password ,
257+ _acquire_token_by_device_flow ,
218258 _acquire_ssh_cert_silently ,
219259 _acquire_ssh_cert_interactive ,
220260 _acquire_pop_token_interactive ,
0 commit comments