1- import getpass , logging , pprint , sys , msal
1+ import functools , getpass , logging , pprint , sys , requests , msal
22
33
44AZURE_CLI = "04b07795-8ddb-461a-bbee-02f9e1bf7b46"
@@ -141,32 +141,55 @@ def remove_account(app):
141141 app .remove_account (account )
142142 print ('Account "{}" and/or its token(s) are signed out from MSAL Python' .format (account ["username" ]))
143143
144- def acquire_token_for_client (app ):
145- """acquire_token_for_client() - Only for confidential client"""
146- pprint .pprint (app .acquire_token_for_client (_input_scopes ()))
144+ def acquire_token_for_managed_identity (app ):
145+ """acquire_token() - Only for managed identity"""
146+ pprint .pprint (app .acquire_token (_select_options ([
147+ "https://management.azure.com" ,
148+ "https://graph.microsoft.com" ,
149+ ],
150+ header = "Acquire token for this resource" ,
151+ accept_nonempty_string = True )))
147152
148153def exit (app ):
149154 """Exit"""
150155 bug_link = (
151156 "https://identitydivision.visualstudio.com/Engineering/_queries/query/79b3a352-a775-406f-87cd-a487c382a8ed/"
152- if app . _enable_broker else
157+ if getattr ( app , " _enable_broker" , None ) else
153158 "https://github.com/AzureAD/microsoft-authentication-library-for-python/issues/new/choose"
154159 )
155160 print ("Bye. If you found a bug, please report it here: {}" .format (bug_link ))
156161 sys .exit ()
157162
163+ def _managed_identity ():
164+ client_id = _select_options ([
165+ {"client_id" : None , "name" : "System-assigned managed identity" },
166+ ],
167+ option_renderer = lambda a : a ["name" ],
168+ header = "Choose the system-assigned managed identity "
169+ "(or type in your user-assigned managed identity)" ,
170+ accept_nonempty_string = True )
171+ return msal .ManagedIdentity (
172+ requests .Session (),
173+ client_id = client_id ["client_id" ]
174+ if isinstance (client_id , dict ) else client_id ,
175+ token_cache = msal .TokenCache (),
176+ )
177+
158178def main ():
159- print ("Welcome to the Msal Python Console Test App, committed at 2022-5-2 \n " )
179+ print ("Welcome to the Console Test App for MSAL Python {} \n " . format ( msal . __version__ ) )
160180 chosen_app = _select_options ([
161181 {"client_id" : AZURE_CLI , "name" : "Azure CLI (Correctly configured for MSA-PT)" },
162182 {"client_id" : VISUAL_STUDIO , "name" : "Visual Studio (Correctly configured for MSA-PT)" },
163183 {"client_id" : "95de633a-083e-42f5-b444-a4295d8e9314" , "name" : "Whiteboard Services (Non MSA-PT app. Accepts AAD & MSA accounts.)" },
164- {"client_id " : None , "client_secret" : None , " name" : "System-assigned Managed Identity (Only works when running inside a supported environment, such as Azure VM, Azure App Service, Azure Automation)" },
184+ {"test_managed_identity " : None , "name" : "Managed Identity (Only works when running inside a supported environment, such as Azure VM, Azure App Service, Azure Automation)" },
165185 ],
166186 option_renderer = lambda a : a ["name" ],
167187 header = "Impersonate this app (or you can type in the client_id of your own app)" ,
168188 accept_nonempty_string = True )
169- authority = _select_options ([
189+ if isinstance (chosen_app , dict ) and "test_managed_identity" in chosen_app :
190+ app = _managed_identity ()
191+ else :
192+ authority = _select_options ([
170193 "https://login.microsoftonline.com/common" ,
171194 "https://login.microsoftonline.com/organizations" ,
172195 "https://login.microsoftonline.com/microsoft.onmicrosoft.com" ,
@@ -175,33 +198,32 @@ def main():
175198 ],
176199 header = "Input authority (Note that MSA-PT apps would NOT use the /common authority)" ,
177200 accept_nonempty_string = True ,
178- )
179- if isinstance (chosen_app , dict ) and "client_secret" in chosen_app :
180- app = msal .ConfidentialClientApplication (
181- chosen_app ["client_id" ],
182- client_credential = chosen_app ["client_secret" ],
183- authority = authority ,
184- )
185- else :
201+ )
186202 app = msal .PublicClientApplication (
187203 chosen_app ["client_id" ] if isinstance (chosen_app , dict ) else chosen_app ,
188204 authority = authority ,
189205 allow_broker = _input_boolean ("Allow broker? (Azure CLI currently only supports @microsoft.com accounts when enabling broker)" ),
190206 )
191207 if _input_boolean ("Enable MSAL Python's DEBUG log?" ):
192208 logging .basicConfig (level = logging .DEBUG )
209+ methods_to_be_tested = functools .reduce (lambda x , y : x + y , [
210+ methods for app_type , methods in {
211+ msal .PublicClientApplication : [
212+ acquire_token_interactive ,
213+ acquire_ssh_cert_silently ,
214+ acquire_ssh_cert_interactive ,
215+ ],
216+ msal .ClientApplication : [
217+ acquire_token_silent ,
218+ acquire_token_by_username_password ,
219+ remove_account ,
220+ ],
221+ msal .ManagedIdentity : [acquire_token_for_managed_identity ],
222+ }.items () if isinstance (app , app_type )])
193223 while True :
194- func = _select_options (list (filter (None , [
195- acquire_token_silent ,
196- acquire_token_interactive ,
197- acquire_token_by_username_password ,
198- acquire_ssh_cert_silently ,
199- acquire_ssh_cert_interactive ,
200- remove_account ,
201- acquire_token_for_client if isinstance (
202- app , msal .ConfidentialClientApplication ) else None ,
203- exit ,
204- ])), option_renderer = lambda f : f .__doc__ , header = "MSAL Python APIs:" )
224+ func = _select_options (
225+ methods_to_be_tested + [exit ],
226+ option_renderer = lambda f : f .__doc__ , header = "MSAL Python APIs:" )
205227 try :
206228 func (app )
207229 except ValueError as e :
0 commit comments