@@ -41,8 +41,11 @@ def _input_scopes():
41
41
accept_nonempty_string = True ,
42
42
).split ()
43
43
44
- def _select_account (app ):
44
+ def _select_account (app , show_confidential_app_placeholder = False ):
45
45
accounts = app .get_accounts ()
46
+ if show_confidential_app_placeholder and isinstance (
47
+ app , msal .ConfidentialClientApplication ):
48
+ accounts .insert (0 , {"username" : "This Client" })
46
49
if accounts :
47
50
return _select_options (
48
51
accounts ,
@@ -54,11 +57,11 @@ def _select_account(app):
54
57
55
58
def acquire_token_silent (app ):
56
59
"""acquire_token_silent() - with an account already signed into MSAL Python."""
57
- account = _select_account (app )
60
+ account = _select_account (app , show_confidential_app_placeholder = True )
58
61
if account :
59
62
pprint .pprint (app .acquire_token_silent (
60
63
_input_scopes (),
61
- account = account ,
64
+ account = account if "home_account_id" in account else None ,
62
65
force_refresh = _input_boolean ("Bypass MSAL Python's token cache?" ),
63
66
))
64
67
@@ -127,6 +130,10 @@ def remove_account(app):
127
130
app .remove_account (account )
128
131
print ('Account "{}" and/or its token(s) are signed out from MSAL Python' .format (account ["username" ]))
129
132
133
+ def acquire_token_for_client (app ):
134
+ """acquire_token_for_client() - Only for confidential client"""
135
+ pprint .pprint (app .acquire_token_for_client (_input_scopes ()))
136
+
130
137
def exit (_ ):
131
138
"""Exit"""
132
139
bug_link = "https://github.com/AzureAD/microsoft-authentication-library-for-python/issues/new/choose"
@@ -139,13 +146,12 @@ def main():
139
146
{"client_id" : "04b07795-8ddb-461a-bbee-02f9e1bf7b46" , "name" : "Azure CLI (Correctly configured for MSA-PT)" },
140
147
{"client_id" : "04f0c124-f2bc-4f59-8241-bf6df9866bbd" , "name" : "Visual Studio (Correctly configured for MSA-PT)" },
141
148
{"client_id" : "95de633a-083e-42f5-b444-a4295d8e9314" , "name" : "Whiteboard Services (Non MSA-PT app. Accepts AAD & MSA accounts.)" },
149
+ {"client_id" : None , "client_secret" : None , "name" : "System-assigned Managed Identity (Only works when running inside a supported environment, such as Azure VM)" },
142
150
],
143
151
option_renderer = lambda a : a ["name" ],
144
152
header = "Impersonate this app (or you can type in the client_id of your own app)" ,
145
153
accept_nonempty_string = True )
146
- app = msal .PublicClientApplication (
147
- chosen_app ["client_id" ] if isinstance (chosen_app , dict ) else chosen_app ,
148
- authority = _select_options ([
154
+ authority = _select_options ([
149
155
"https://login.microsoftonline.com/common" ,
150
156
"https://login.microsoftonline.com/organizations" ,
151
157
"https://login.microsoftonline.com/microsoft.onmicrosoft.com" ,
@@ -154,20 +160,32 @@ def main():
154
160
],
155
161
header = "Input authority (Note that MSA-PT apps would NOT use the /common authority)" ,
156
162
accept_nonempty_string = True ,
157
- ),
158
163
)
164
+ if isinstance (chosen_app , dict ) and "client_secret" in chosen_app :
165
+ app = msal .ConfidentialClientApplication (
166
+ chosen_app ["client_id" ],
167
+ client_credential = chosen_app ["client_secret" ],
168
+ authority = authority ,
169
+ )
170
+ else :
171
+ app = msal .PublicClientApplication (
172
+ chosen_app ["client_id" ] if isinstance (chosen_app , dict ) else chosen_app ,
173
+ authority = authority ,
174
+ )
159
175
if _input_boolean ("Enable MSAL Python's DEBUG log?" ):
160
176
logging .basicConfig (level = logging .DEBUG )
161
177
while True :
162
- func = _select_options ([
178
+ func = _select_options (list ( filter ( None , [
163
179
acquire_token_silent ,
164
180
acquire_token_interactive ,
165
181
acquire_token_by_username_password ,
166
182
acquire_ssh_cert_silently ,
167
183
acquire_ssh_cert_interactive ,
168
184
remove_account ,
185
+ acquire_token_for_client if isinstance (
186
+ app , msal .ConfidentialClientApplication ) else None ,
169
187
exit ,
170
- ], option_renderer = lambda f : f .__doc__ , header = "MSAL Python APIs:" )
188
+ ])) , option_renderer = lambda f : f .__doc__ , header = "MSAL Python APIs:" )
171
189
try :
172
190
func (app )
173
191
except KeyboardInterrupt : # Useful for bailing out a stuck interactive flow
0 commit comments