Skip to content

Commit 1b0c7bd

Browse files
committed
Patch authority for backward compatibility
1 parent 586a8ad commit 1b0c7bd

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

msal/authority.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
from urlparse import urlparse
66
import logging
77

8+
# Historically some customers patched this module-wide requests instance.
9+
# We keep it here for now. They will be removed in next major release.
10+
import requests
11+
import requests as _requests
12+
813
from .exceptions import MsalServiceError
914

1015

@@ -33,6 +38,11 @@ class Authority(object):
3338
"""
3439
_domains_without_user_realm_discovery = set([])
3540

41+
@property
42+
def http_client(self): # Obsolete. We will remove this in next major release.
43+
# A workaround: if module-wide requests is patched, we honor it.
44+
return self._http_client if requests is _requests else requests
45+
3646
def __init__(self, authority_url, http_client, validate_authority=True):
3747
"""Creates an authority instance, and also validates it.
3848
@@ -42,7 +52,7 @@ def __init__(self, authority_url, http_client, validate_authority=True):
4252
This parameter only controls whether an instance discovery will be
4353
performed.
4454
"""
45-
self.http_client = http_client
55+
self._http_client = http_client
4656
authority, self.instance, tenant = canonicalize(authority_url)
4757
parts = authority.path.split('/')
4858
is_b2c = any(self.instance.endswith("." + d) for d in WELL_KNOWN_B2C_HOSTS) or (

tests/test_authority_patch.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import unittest
2+
3+
import msal
4+
from tests.http_client import MinimalHttpClient
5+
6+
7+
class DummyHttpClient(object):
8+
def get(self, url, **kwargs):
9+
raise RuntimeError("just for testing purpose")
10+
11+
12+
class TestAuthorityHonorsPatchedRequests(unittest.TestCase):
13+
"""This is only a workaround for an undocumented behavior."""
14+
def test_authority_honors_a_patched_requests(self):
15+
# First, we test that the original, unmodified authority is working
16+
a = msal.authority.Authority(
17+
"https://login.microsoftonline.com/common", MinimalHttpClient())
18+
self.assertEqual(
19+
a.authorization_endpoint,
20+
'https://login.microsoftonline.com/common/oauth2/v2.0/authorize')
21+
22+
original = msal.authority.requests
23+
try:
24+
# Now we mimic a (discouraged) practice of patching authority.requests
25+
msal.authority.requests = DummyHttpClient()
26+
# msal.authority is expected to honor that patch.
27+
with self.assertRaises(RuntimeError):
28+
a = msal.authority.Authority(
29+
"https://login.microsoftonline.com/common", MinimalHttpClient())
30+
finally: # Tricky:
31+
# Unpatch is necessary otherwise other test cases would be affected
32+
msal.authority.requests = original

0 commit comments

Comments
 (0)