Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/azure-cli-core/azure/cli/core/_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ class Identity:

CLOUD_SHELL_IDENTITY_UNIQUE_NAME = "unique_name"

def __init__(self, authority=None, tenant_id=None, client_id=_CLIENT_ID, **kwargs):
def __init__(self, authority=None, tenant_id=None, client_id=None, **kwargs):
self.authority = authority
self.tenant_id = tenant_id
self.client_id = client_id
self.tenant_id = tenant_id or "organizations"
self.client_id = client_id or _CLIENT_ID
self._cred_cache = kwargs.pop('cred_cache', None)
# todo: MSAL support force encryption
self.allow_unencrypted = True
Expand All @@ -92,7 +92,9 @@ def _msal_app(self):

# Store for user token persistence
cache = load_persistent_cache(self.allow_unencrypted)
return PublicClientApplication(authority=self.authority, client_id=self.client_id, token_cache=cache)
# Build the authority in MSAL style
msal_authority = "https://{}/{}".format(self.authority, self.tenant_id)
return PublicClientApplication(authority=msal_authority, client_id=self.client_id, token_cache=cache)

def login_with_interactive_browser(self):
# Use InteractiveBrowserCredential
Expand Down Expand Up @@ -256,16 +258,15 @@ def _decode_managed_identity_token(self, credential, resource):
return decoded

def get_user(self, user_or_sp=None):
try:
return self._msal_app.get_accounts(user_or_sp)
except ValueError:
pass
accounts = self._msal_app.get_accounts(user_or_sp)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove the try-catch? it throws an exception if the user does not exist which is not handled in the logout function

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ValueError is thrown due to the incorrect authority given to PublicClientApplication. If the username is not found, [] will be return.

return accounts

def logout_user(self, user_or_sp):
accounts = self._msal_app.get_accounts(user_or_sp)
logger.info('Before account removal:')
logger.info(json.dumps(accounts))

# `accounts` are the same user in all tenants, log out all of them
for account in accounts:
self._msal_app.remove_account(account)

Expand All @@ -276,6 +277,7 @@ def logout_user(self, user_or_sp):
self._msal_store.remove_cached_creds(user_or_sp)

def logout_all(self):
# TODO: Support multi-authority logout
accounts = self._msal_app.get_accounts()
logger.info('Before account removal:')
logger.info(json.dumps(accounts))
Expand Down
2 changes: 1 addition & 1 deletion src/azure-cli-core/azure/cli/core/_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def logout(self, user_or_sp, clear_credential):
# https://english.stackexchange.com/questions/5302/log-in-to-or-log-into-or-login-to
logger.warning("Account %s was not logged in to Azure CLI.", user_or_sp)

# Deal with MSAL cache
# Log out from MSAL cache
identity = Identity(self._authority)
accounts = identity.get_user(user_or_sp)
if accounts:
Expand Down