@@ -47,8 +47,11 @@ def _input_scopes():
47
47
raise ValueError ("SSH Cert scope shall be tested by its dedicated functions" )
48
48
return scopes
49
49
50
- def _select_account (app ):
50
+ def _select_account (app , show_confidential_app_placeholder = False ):
51
51
accounts = app .get_accounts ()
52
+ if show_confidential_app_placeholder and isinstance (
53
+ app , msal .ConfidentialClientApplication ):
54
+ accounts .insert (0 , {"username" : "This Client" })
52
55
if accounts :
53
56
return _select_options (
54
57
accounts ,
@@ -60,11 +63,11 @@ def _select_account(app):
60
63
61
64
def acquire_token_silent (app ):
62
65
"""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 )
64
67
if account :
65
68
pprint .pprint (app .acquire_token_silent (
66
69
_input_scopes (),
67
- account = account ,
70
+ account = account if "home_account_id" in account else None ,
68
71
force_refresh = _input_boolean ("Bypass MSAL Python's token cache?" ),
69
72
))
70
73
@@ -138,6 +141,10 @@ def remove_account(app):
138
141
app .remove_account (account )
139
142
print ('Account "{}" and/or its token(s) are signed out from MSAL Python' .format (account ["username" ]))
140
143
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
+
141
148
def exit (app ):
142
149
"""Exit"""
143
150
bug_link = (
@@ -154,13 +161,12 @@ def main():
154
161
{"client_id" : AZURE_CLI , "name" : "Azure CLI (Correctly configured for MSA-PT)" },
155
162
{"client_id" : VISUAL_STUDIO , "name" : "Visual Studio (Correctly configured for MSA-PT)" },
156
163
{"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)" },
157
165
],
158
166
option_renderer = lambda a : a ["name" ],
159
167
header = "Impersonate this app (or you can type in the client_id of your own app)" ,
160
168
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 ([
164
170
"https://login.microsoftonline.com/common" ,
165
171
"https://login.microsoftonline.com/organizations" ,
166
172
"https://login.microsoftonline.com/microsoft.onmicrosoft.com" ,
@@ -169,21 +175,33 @@ def main():
169
175
],
170
176
header = "Input authority (Note that MSA-PT apps would NOT use the /common authority)" ,
171
177
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
+ )
175
191
if _input_boolean ("Enable MSAL Python's DEBUG log?" ):
176
192
logging .basicConfig (level = logging .DEBUG )
177
193
while True :
178
- func = _select_options ([
194
+ func = _select_options (list ( filter ( None , [
179
195
acquire_token_silent ,
180
196
acquire_token_interactive ,
181
197
acquire_token_by_username_password ,
182
198
acquire_ssh_cert_silently ,
183
199
acquire_ssh_cert_interactive ,
184
200
remove_account ,
201
+ acquire_token_for_client if isinstance (
202
+ app , msal .ConfidentialClientApplication ) else None ,
185
203
exit ,
186
- ], option_renderer = lambda f : f .__doc__ , header = "MSAL Python APIs:" )
204
+ ])) , option_renderer = lambda f : f .__doc__ , header = "MSAL Python APIs:" )
187
205
try :
188
206
func (app )
189
207
except ValueError as e :
0 commit comments