@@ -47,8 +47,11 @@ def _input_scopes():
4747 raise ValueError ("SSH Cert scope shall be tested by its dedicated functions" )
4848 return scopes
4949
50- def _select_account (app ):
50+ def _select_account (app , show_confidential_app_placeholder = False ):
5151 accounts = app .get_accounts ()
52+ if show_confidential_app_placeholder and isinstance (
53+ app , msal .ConfidentialClientApplication ):
54+ accounts .insert (0 , {"username" : "This Client" })
5255 if accounts :
5356 return _select_options (
5457 accounts ,
@@ -60,11 +63,11 @@ def _select_account(app):
6063
6164def acquire_token_silent (app ):
6265 """acquire_token_silent() - with an account already signed into MSAL Python."""
63- account = _select_account (app )
66+ account = _select_account (app , show_confidential_app_placeholder = True )
6467 if account :
6568 pprint .pprint (app .acquire_token_silent (
6669 _input_scopes (),
67- account = account ,
70+ account = account if "home_account_id" in account else None ,
6871 force_refresh = _input_boolean ("Bypass MSAL Python's token cache?" ),
6972 ))
7073
@@ -138,6 +141,10 @@ def remove_account(app):
138141 app .remove_account (account )
139142 print ('Account "{}" and/or its token(s) are signed out from MSAL Python' .format (account ["username" ]))
140143
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 ()))
147+
141148def exit (app ):
142149 """Exit"""
143150 bug_link = (
@@ -154,13 +161,12 @@ def main():
154161 {"client_id" : AZURE_CLI , "name" : "Azure CLI (Correctly configured for MSA-PT)" },
155162 {"client_id" : VISUAL_STUDIO , "name" : "Visual Studio (Correctly configured for MSA-PT)" },
156163 {"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)" },
157165 ],
158166 option_renderer = lambda a : a ["name" ],
159167 header = "Impersonate this app (or you can type in the client_id of your own app)" ,
160168 accept_nonempty_string = True )
161- app = msal .PublicClientApplication (
162- chosen_app ["client_id" ] if isinstance (chosen_app , dict ) else chosen_app ,
163- authority = _select_options ([
169+ authority = _select_options ([
164170 "https://login.microsoftonline.com/common" ,
165171 "https://login.microsoftonline.com/organizations" ,
166172 "https://login.microsoftonline.com/microsoft.onmicrosoft.com" ,
@@ -169,21 +175,33 @@ def main():
169175 ],
170176 header = "Input authority (Note that MSA-PT apps would NOT use the /common authority)" ,
171177 accept_nonempty_string = True ,
172- ),
173- allow_broker = _input_boolean ("Allow broker? (Azure CLI currently only supports @microsoft.com accounts when enabling broker)" ),
174- )
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 :
186+ app = msal .PublicClientApplication (
187+ chosen_app ["client_id" ] if isinstance (chosen_app , dict ) else chosen_app ,
188+ authority = authority ,
189+ allow_broker = _input_boolean ("Allow broker? (Azure CLI currently only supports @microsoft.com accounts when enabling broker)" ),
190+ )
175191 if _input_boolean ("Enable MSAL Python's DEBUG log?" ):
176192 logging .basicConfig (level = logging .DEBUG )
177193 while True :
178- func = _select_options ([
194+ func = _select_options (list ( filter ( None , [
179195 acquire_token_silent ,
180196 acquire_token_interactive ,
181197 acquire_token_by_username_password ,
182198 acquire_ssh_cert_silently ,
183199 acquire_ssh_cert_interactive ,
184200 remove_account ,
201+ acquire_token_for_client if isinstance (
202+ app , msal .ConfidentialClientApplication ) else None ,
185203 exit ,
186- ], option_renderer = lambda f : f .__doc__ , header = "MSAL Python APIs:" )
204+ ])) , option_renderer = lambda f : f .__doc__ , header = "MSAL Python APIs:" )
187205 try :
188206 func (app )
189207 except ValueError as e :
0 commit comments