1
- import getpass , logging , pprint , sys , msal
1
+ import functools , getpass , logging , pprint , sys , requests , msal
2
2
3
3
4
4
AZURE_CLI = "04b07795-8ddb-461a-bbee-02f9e1bf7b46"
@@ -141,15 +141,16 @@ def remove_account(app):
141
141
app .remove_account (account )
142
142
print ('Account "{}" and/or its token(s) are signed out from MSAL Python' .format (account ["username" ]))
143
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 ()))
144
+ def acquire_token_for_managed_identity (app ):
145
+ """acquire_token() - Only for managed identity"""
146
+ resource = "https://management.azure.com/" # TODO: Are there other resources?
147
+ pprint .pprint (app .acquire_token (resource ))
147
148
148
149
def exit (app ):
149
150
"""Exit"""
150
151
bug_link = (
151
152
"https://identitydivision.visualstudio.com/Engineering/_queries/query/79b3a352-a775-406f-87cd-a487c382a8ed/"
152
- if app . _enable_broker else
153
+ if getattr ( app , " _enable_broker" , None ) else
153
154
"https://github.com/AzureAD/microsoft-authentication-library-for-python/issues/new/choose"
154
155
)
155
156
print ("Bye. If you found a bug, please report it here: {}" .format (bug_link ))
@@ -161,12 +162,19 @@ def main():
161
162
{"client_id" : AZURE_CLI , "name" : "Azure CLI (Correctly configured for MSA-PT)" },
162
163
{"client_id" : VISUAL_STUDIO , "name" : "Visual Studio (Correctly configured for MSA-PT)" },
163
164
{"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)" },
165
+ {"managed_identity_client_id " : None , "name" : "System-assigned Managed Identity (Only works when running inside a supported environment, such as Azure VM, Azure App Service, Azure Automation)" },
165
166
],
166
167
option_renderer = lambda a : a ["name" ],
167
168
header = "Impersonate this app (or you can type in the client_id of your own app)" ,
168
169
accept_nonempty_string = True )
169
- authority = _select_options ([
170
+ if isinstance (chosen_app , dict ) and "managed_identity_client_id" in chosen_app :
171
+ app = msal .ManagedIdentity (
172
+ requests .Session (),
173
+ client_id = chosen_app ["managed_identity_client_id" ],
174
+ token_cache = msal .TokenCache (),
175
+ )
176
+ else :
177
+ authority = _select_options ([
170
178
"https://login.microsoftonline.com/common" ,
171
179
"https://login.microsoftonline.com/organizations" ,
172
180
"https://login.microsoftonline.com/microsoft.onmicrosoft.com" ,
@@ -175,33 +183,32 @@ def main():
175
183
],
176
184
header = "Input authority (Note that MSA-PT apps would NOT use the /common authority)" ,
177
185
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 :
186
+ )
186
187
app = msal .PublicClientApplication (
187
188
chosen_app ["client_id" ] if isinstance (chosen_app , dict ) else chosen_app ,
188
189
authority = authority ,
189
190
allow_broker = _input_boolean ("Allow broker? (Azure CLI currently only supports @microsoft.com accounts when enabling broker)" ),
190
191
)
191
192
if _input_boolean ("Enable MSAL Python's DEBUG log?" ):
192
193
logging .basicConfig (level = logging .DEBUG )
194
+ methods_to_be_tested = functools .reduce (lambda x , y : x + y , [
195
+ methods for app_type , methods in {
196
+ msal .PublicClientApplication : [
197
+ acquire_token_interactive ,
198
+ acquire_ssh_cert_silently ,
199
+ acquire_ssh_cert_interactive ,
200
+ ],
201
+ msal .ClientApplication : [
202
+ acquire_token_silent ,
203
+ acquire_token_by_username_password ,
204
+ remove_account ,
205
+ ],
206
+ msal .ManagedIdentity : [acquire_token_for_managed_identity ],
207
+ }.items () if isinstance (app , app_type )])
193
208
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:" )
209
+ func = _select_options (
210
+ methods_to_be_tested + [exit ],
211
+ option_renderer = lambda f : f .__doc__ , header = "MSAL Python APIs:" )
205
212
try :
206
213
func (app )
207
214
except ValueError as e :
0 commit comments