Skip to content

Commit 2d6c373

Browse files
committed
Wire up verify and proxies for Authority
1 parent 43999a6 commit 2d6c373

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

msal/application.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ def __init__(
9292
"""
9393
self.client_id = client_id
9494
self.client_credential = client_credential
95+
self.verify = verify
96+
self.proxies = proxies
97+
self.timeout = timeout
9598
self.authority = Authority(
9699
authority or "https://login.microsoftonline.com/common/",
97-
validate_authority)
100+
validate_authority, verify=self.verify, proxies=self.proxies)
98101
# Here the self.authority is not the same type as authority in input
99102
self.token_cache = token_cache or TokenCache()
100-
self.verify = verify
101-
self.proxies = proxies
102-
self.timeout = timeout
103103
self.client = self._build_client(client_credential, self.authority)
104104

105105
def _build_client(self, client_credential, authority):
@@ -166,7 +166,9 @@ def get_authorization_request_url(
166166
(Under the hood, we simply merge scope and additional_scope before
167167
sending them on the wire.)
168168
"""
169-
the_authority = Authority(authority) if authority else self.authority
169+
the_authority = Authority(
170+
authority, verify=self.verify, proxies=self.proxies,
171+
) if authority else self.authority
170172
client = Client(
171173
{"authorization_endpoint": the_authority.authorization_endpoint},
172174
self.client_id)
@@ -272,7 +274,9 @@ def acquire_token_silent(
272274
- None when cache lookup does not yield anything.
273275
"""
274276
assert isinstance(scopes, list), "Invalid parameter type"
275-
the_authority = Authority(authority) if authority else self.authority
277+
the_authority = Authority(
278+
authority, verify=self.verify, proxies=self.proxies,
279+
) if authority else self.authority
276280

277281
if not force_refresh:
278282
matches = self.token_cache.find(

msal/authority.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ class Authority(object):
2121
Once constructed, it contains members named "*_endpoint" for this instance.
2222
TODO: It will also cache the previously-validated authority instances.
2323
"""
24-
def __init__(self, authority_url, validate_authority=True):
24+
def __init__(self, authority_url, validate_authority=True,
25+
verify=True, proxies=None,
26+
):
2527
"""Creates an authority instance, and also validates it.
2628
2729
:param validate_authority:
@@ -30,24 +32,29 @@ def __init__(self, authority_url, validate_authority=True):
3032
This parameter only controls whether an instance discovery will be
3133
performed.
3234
"""
35+
self.verify = verify
36+
self.proxies = proxies
3337
canonicalized, self.instance, tenant = canonicalize(authority_url)
3438
tenant_discovery_endpoint = ( # Hard code a V2 pattern as default value
3539
'https://{}/{}/v2.0/.well-known/openid-configuration'
3640
.format(WORLD_WIDE, tenant))
3741
if validate_authority and self.instance not in WELL_KNOWN_AUTHORITY_HOSTS:
3842
tenant_discovery_endpoint = instance_discovery(
39-
canonicalized + "/oauth2/v2.0/authorize")
40-
openid_config = tenant_discovery(tenant_discovery_endpoint)
43+
canonicalized + "/oauth2/v2.0/authorize",
44+
verify=verify, proxies=proxies)
45+
openid_config = tenant_discovery(
46+
tenant_discovery_endpoint, verify=verify, proxies=proxies)
4147
self.authorization_endpoint = openid_config['authorization_endpoint']
4248
self.token_endpoint = openid_config['token_endpoint']
4349
_, _, self.tenant = canonicalize(self.token_endpoint) # Usually a GUID
4450
self.is_adfs = self.tenant.lower() == 'adfs'
4551

46-
def user_realm_discovery(self, username, **kwargs):
52+
def user_realm_discovery(self, username):
4753
resp = requests.get(
4854
"https://{netloc}/common/userrealm/{username}?api-version=1.0".format(
4955
netloc=self.instance, username=username),
50-
headers={'Accept':'application/json'}, **kwargs)
56+
headers={'Accept':'application/json'},
57+
verify=self.verify, proxies=self.proxies)
5158
resp.raise_for_status()
5259
return resp.json()
5360
# It will typically contain "ver", "account_type",
@@ -64,17 +71,20 @@ def canonicalize(url):
6471
"https://login.microsoftonline.com/<tenant_name>" % url)
6572
return match_object.group(0), match_object.group(1), match_object.group(2)
6673

67-
def instance_discovery(url, response=None): # Returns tenant discovery endpoint
74+
def instance_discovery(url, response=None, verify=True, proxies=None):
75+
# Returns tenant discovery endpoint
6876
resp = requests.get( # Note: This URL seemingly returns V1 endpoint only
6977
'https://{}/common/discovery/instance'.format(WORLD_WIDE),
70-
params={'authorization_endpoint': url, 'api-version': '1.0'})
78+
params={'authorization_endpoint': url, 'api-version': '1.0'},
79+
verify=verify, proxies=proxies)
7180
payload = response or resp.json()
7281
if 'tenant_discovery_endpoint' not in payload:
7382
raise MsalServiceError(status_code=resp.status_code, **payload)
7483
return payload['tenant_discovery_endpoint']
7584

76-
def tenant_discovery(tenant_discovery_endpoint): # Returns Openid Configuration
77-
resp = requests.get(tenant_discovery_endpoint)
85+
def tenant_discovery(tenant_discovery_endpoint, verify=True, proxies=None):
86+
# Returns Openid Configuration
87+
resp = requests.get(tenant_discovery_endpoint, verify=verify, proxies=proxies)
7888
payload = resp.json()
7989
if 'authorization_endpoint' in payload and 'token_endpoint' in payload:
8090
return payload

tests/test_authority.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,6 @@ def test_instance_discovery_with_unknown_instance(self):
9494
def test_instance_discovery_with_mocked_response(self):
9595
mock_response = {'tenant_discovery_endpoint': 'http://a.com/t/openid'}
9696
endpoint = instance_discovery(
97-
"https://login.microsoftonline.in/tenant.com", mock_response)
97+
"https://login.microsoftonline.in/tenant.com", response=mock_response)
9898
self.assertEqual(endpoint, mock_response['tenant_discovery_endpoint'])
9999

0 commit comments

Comments
 (0)