Skip to content

Commit 1af8375

Browse files
committed
Emit warning when common or organizations is used in acquire_token_for_client()
1 parent 23e5341 commit 1af8375

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
@@ -1642,6 +1642,11 @@ def acquire_token_for_client(self, scopes, claims_challenge=None, **kwargs):
16421642
- an error response would contain "error" and usually "error_description".
16431643
"""
16441644
# TBD: force_refresh behavior
1645+
if self.authority.tenant.lower() in ["common", "organizations"]:
1646+
warnings.warn(
1647+
"Using /common or /organizations authority "
1648+
"in acquire_token_for_client() is unreliable. "
1649+
"Please use a specific tenant instead.", DeprecationWarning)
16451650
self._validate_ssh_cert_input_data(kwargs.get("data", {}))
16461651
telemetry_context = self._build_telemetry_context(
16471652
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)