Skip to content

Commit 7bdab8f

Browse files
committed
refactor auth module: azure env
1 parent d14de37 commit 7bdab8f

File tree

4 files changed

+50
-21
lines changed

4 files changed

+50
-21
lines changed

office365/azure_env.py

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,54 @@ class AzureEnvironment(object):
33
Global = "Global"
44
"""Referred to as Azure or Azure Global."""
55

6-
GCC = "GCC"
6+
USGovernment = "GCC"
77
"""Government Community Cloud"""
88

9-
GCC_High = "GCC High"
9+
USGovernmentHigh = "GCC High"
1010
"""Government Community Cloud High."""
1111

12-
CN = "CN"
12+
China = "CN"
1313
"""Operated by 21Vianet under Chinese regulations."""
1414

15-
Azure_DE = "Azure DE"
15+
Germany = "Azure DE"
1616
"""Legacy; for data sovereignty in Germany."""
1717

18-
DoD = "DoD"
18+
USGovernmentDoD = "DoD"
1919
"""Azure for U.S. Department of Defense (DoD)"""
20+
21+
_authority_endpoints = {
22+
Global: {
23+
"graph": "https://graph.microsoft.com",
24+
"login": "https://login.microsoftonline.com",
25+
},
26+
USGovernment: {
27+
"graph": "https://graph.microsoft.com",
28+
"login": "https://login.microsoftonline.com",
29+
},
30+
USGovernmentHigh: {
31+
"graph": "https://graph.microsoft.us",
32+
"login": "https://login.microsoftonline.us",
33+
},
34+
USGovernmentDoD: {
35+
"graph": "https://dod-graph.microsoft.us",
36+
"login": "https://login.microsoftonline.us",
37+
},
38+
Germany: {
39+
"graph": "https://graph.microsoft.de",
40+
"login": "https://login.microsoftonline.de",
41+
},
42+
China: {
43+
"graph": "https://microsoftgraph.chinacloudapi.cn",
44+
"login": "https://login.chinacloudapi.cn",
45+
},
46+
}
47+
48+
@classmethod
49+
def get_graph_authority(cls, env):
50+
# type: (str) -> str
51+
return cls._authority_endpoints.get(env)["graph"]
52+
53+
@classmethod
54+
def get_login_authority(cls, env):
55+
# type: (str) -> str
56+
return cls._authority_endpoints.get(env)["login"]

office365/graph_request.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ def __init__(
1515
super(GraphRequest, self).__init__(V4JsonFormat())
1616
self._version = version
1717
self._environment = environment
18-
self._endpoints = {
19-
AzureEnvironment.Global: "https://graph.microsoft.com",
20-
AzureEnvironment.GCC: "https://graph.microsoft.us",
21-
AzureEnvironment.GCC_High: "https://graph.microsoft.us",
22-
AzureEnvironment.DoD: "https://dod-graph.microsoft.us",
23-
}
2418
self._auth_context = AuthenticationContext(
2519
environment=environment, tenant=tenant
2620
)
@@ -90,4 +84,6 @@ def authenticate_request(self, request):
9084
@property
9185
def service_root_url(self):
9286
# type: () -> str
93-
return "{0}/{1}".format(self._endpoints.get(self._environment), self._version)
87+
return "{0}/{1}".format(
88+
AzureEnvironment.get_graph_authority(self._environment), self._version
89+
)

office365/runtime/auth/entra/authentication_context.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,13 @@ def __init__(
2222
"""
2323
self._tenant = tenant
2424
if scopes is None:
25-
scopes = ["https://graph.microsoft.com/.default"]
25+
scopes = [
26+
"{0}/.default".format(AzureEnvironment.get_graph_authority(environment))
27+
]
2628
self._scopes = scopes
2729
self._token_cache = token_cache
2830
self._token_callback = None
2931
self._environment = environment
30-
self._authority_urls = {
31-
AzureEnvironment.Global: "https://login.microsoftonline.com",
32-
AzureEnvironment.GCC: "https://login.microsoftonline.us",
33-
AzureEnvironment.GCC_High: "https://login.microsoftonline.us",
34-
AzureEnvironment.CN: "https://login.chinacloudapi.cn",
35-
}
3632

3733
def acquire_token(self):
3834
# type: () -> TokenResponse
@@ -165,5 +161,5 @@ def _acquire_token():
165161
@property
166162
def authority_url(self):
167163
return "{0}/{1}".format(
168-
self._authority_urls.get(self._environment), self._tenant
164+
AzureEnvironment.get_login_authority(self._environment), self._tenant
169165
)

tests/sharepoint/test_tenant.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def test_32_get_admin_endpoints(self):
226226
result = self.tenant.admin_endpoints.get().execute_query()
227227
self.assertIsNotNone(result.resource_path)
228228

229-
#def test_33_get_top_files_sharing_insights(self):
229+
# def test_33_get_top_files_sharing_insights(self):
230230
# result = self.tenant.get_top_files_sharing_insights().execute_query()
231231
# self.assertIsNotNone(result.resource_path)
232232

0 commit comments

Comments
 (0)