Skip to content

Commit 378d1bb

Browse files
authored
{Auth} Add --tenant to the re-authentication message (#31742)
1 parent cf56331 commit 378d1bb

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

src/azure-cli-core/azure/cli/core/auth/msal_credentials.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ def acquire_token(self, scopes, claims_challenge=None, **kwargs):
5959
from azure.cli.core.azclierror import AuthenticationError
6060
try:
6161
# Check if an access token is returned.
62-
check_result(result, scopes=scopes, claims_challenge=claims_challenge)
62+
check_result(result, tenant=self._msal_app.authority.tenant, scopes=scopes,
63+
claims_challenge=claims_challenge)
6364
except AuthenticationError as ex:
6465
# For VM SSH ('data' is passed), if getting access token fails because
6566
# Conditional Access MFA step-up or compliance check is required, re-launch

src/azure-cli-core/azure/cli/core/auth/tests/test_util.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,20 @@ def test_generate_login_command(self):
5454
# No parameter is given
5555
assert _generate_login_command() == 'az login'
5656

57-
# scopes
57+
# tenant
58+
actual = _generate_login_command(tenant='21987a97-4e85-47c5-9a13-9dc3e11b2a9a')
59+
assert actual == 'az login --tenant "21987a97-4e85-47c5-9a13-9dc3e11b2a9a"'
60+
61+
# scope
5862
actual = _generate_login_command(scopes=["https://management.core.windows.net//.default"])
59-
assert actual == 'az login --scope https://management.core.windows.net//.default'
63+
assert actual == 'az login --scope "https://management.core.windows.net//.default"'
64+
65+
# tenant and scopes
66+
actual = _generate_login_command(tenant='21987a97-4e85-47c5-9a13-9dc3e11b2a9a',
67+
scopes=["https://management.core.windows.net//.default"])
68+
assert actual == ('az login --tenant "21987a97-4e85-47c5-9a13-9dc3e11b2a9a" '
69+
'--scope "https://management.core.windows.net//.default"')
70+
6071

6172

6273
if __name__ == '__main__':

src/azure-cli-core/azure/cli/core/auth/util.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,21 @@ def aad_error_handler(error, **kwargs):
5555
raise AuthenticationError(error_description, msal_error=error, recommendation=recommendation)
5656

5757

58-
def _generate_login_command(scopes=None, claims_challenge=None):
58+
def _generate_login_command(tenant=None, scopes=None, claims_challenge=None):
5959
login_command = ['az login']
6060

61-
# Rejected by Conditional Access policy, like MFA
61+
# Rejected by Conditional Access policy, like MFA.
62+
# MFA status is not shared between tenants. Specifying tenant triggers the MFA process for that tenant.
63+
# Double quotes are not necessary, but we add them following the best practice to avoid shell interpretation.
64+
if tenant:
65+
login_command.extend(['--tenant', f'"{tenant}"'])
66+
67+
# Some scopes (such as Graph) may require MFA while ARM may not.
68+
# Specifying scope triggers the MFA process for that scope.
6269
if scopes:
63-
login_command.append('--scope {}'.format(' '.join(scopes)))
70+
login_command.append('--scope')
71+
for s in scopes:
72+
login_command.append(f'"{s}"')
6473

6574
# Rejected by CAE
6675
if claims_challenge:

0 commit comments

Comments
 (0)