Skip to content

Commit c7e81ba

Browse files
authored
Merge pull request #435 from AzureAD/warning-in-acquire-token-for-client
Emit warning when common or organizations is used in acquire_token_for_client()
2 parents 1b5d2d6 + 149e5fc commit c7e81ba

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

msal/application.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,11 @@ def acquire_token_for_client(self, scopes, claims_challenge=None, **kwargs):
16751675
- an error response would contain "error" and usually "error_description".
16761676
"""
16771677
# TBD: force_refresh behavior
1678+
if self.authority.tenant.lower() in ["common", "organizations"]:
1679+
warnings.warn(
1680+
"Using /common or /organizations authority "
1681+
"in acquire_token_for_client() is unreliable. "
1682+
"Please use a specific tenant instead.", DeprecationWarning)
16781683
self._validate_ssh_cert_input_data(kwargs.get("data", {}))
16791684
telemetry_context = self._build_telemetry_context(
16801685
self.ACQUIRE_TOKEN_FOR_CLIENT_ID)

tests/test_application.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Note: Since Aug 2019 we move all e2e tests into test_e2e.py,
22
# so this test_application file contains only unit tests without dependency.
3+
import sys
34
from msal.application import *
45
from msal.application import _str2bytes
56
import msal
@@ -602,3 +603,25 @@ def test_get_accounts(self):
602603
self.assertIn("local_account_id", account, "Backward compatibility")
603604
self.assertIn("realm", account, "Backward compatibility")
604605

606+
607+
@unittest.skipUnless(
608+
sys.version_info[0] >= 3 and sys.version_info[1] >= 2,
609+
"assertWarns() is only available in Python 3.2+")
610+
class TestClientCredentialGrant(unittest.TestCase):
611+
def _test_certain_authority_should_emit_warnning(self, authority):
612+
app = ConfidentialClientApplication(
613+
"client_id", client_credential="secret", authority=authority)
614+
def mock_post(url, headers=None, *args, **kwargs):
615+
return MinimalResponse(
616+
status_code=200, text=json.dumps({"access_token": "an AT"}))
617+
with self.assertWarns(DeprecationWarning):
618+
app.acquire_token_for_client(["scope"], post=mock_post)
619+
620+
def test_common_authority_should_emit_warnning(self):
621+
self._test_certain_authority_should_emit_warnning(
622+
authority="https://login.microsoftonline.com/common")
623+
624+
def test_organizations_authority_should_emit_warnning(self):
625+
self._test_certain_authority_should_emit_warnning(
626+
authority="https://login.microsoftonline.com/organizations")
627+

0 commit comments

Comments
 (0)