Skip to content

Emit warning when common or organizations is used in acquire_token_for_client() #435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2022
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
5 changes: 5 additions & 0 deletions msal/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -1675,6 +1675,11 @@ def acquire_token_for_client(self, scopes, claims_challenge=None, **kwargs):
- an error response would contain "error" and usually "error_description".
"""
# TBD: force_refresh behavior
if self.authority.tenant.lower() in ["common", "organizations"]:
warnings.warn(
"Using /common or /organizations authority "
"in acquire_token_for_client() is unreliable. "
"Please use a specific tenant instead.", DeprecationWarning)
self._validate_ssh_cert_input_data(kwargs.get("data", {}))
telemetry_context = self._build_telemetry_context(
self.ACQUIRE_TOKEN_FOR_CLIENT_ID)
Expand Down
23 changes: 23 additions & 0 deletions tests/test_application.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Note: Since Aug 2019 we move all e2e tests into test_e2e.py,
# so this test_application file contains only unit tests without dependency.
import sys
from msal.application import *
from msal.application import _str2bytes
import msal
Expand Down Expand Up @@ -602,3 +603,25 @@ def test_get_accounts(self):
self.assertIn("local_account_id", account, "Backward compatibility")
self.assertIn("realm", account, "Backward compatibility")


@unittest.skipUnless(
sys.version_info[0] >= 3 and sys.version_info[1] >= 2,
"assertWarns() is only available in Python 3.2+")
class TestClientCredentialGrant(unittest.TestCase):
def _test_certain_authority_should_emit_warnning(self, authority):
app = ConfidentialClientApplication(
"client_id", client_credential="secret", authority=authority)
def mock_post(url, headers=None, *args, **kwargs):
return MinimalResponse(
status_code=200, text=json.dumps({"access_token": "an AT"}))
with self.assertWarns(DeprecationWarning):
app.acquire_token_for_client(["scope"], post=mock_post)

def test_common_authority_should_emit_warnning(self):
self._test_certain_authority_should_emit_warnning(
authority="https://login.microsoftonline.com/common")

def test_organizations_authority_should_emit_warnning(self):
self._test_certain_authority_should_emit_warnning(
authority="https://login.microsoftonline.com/organizations")