Skip to content

Commit 31d8f21

Browse files
committed
refactor auth module, new types for tenant namespace
1 parent 93c400d commit 31d8f21

29 files changed

+374
-51
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Steps to access:
9393
- [Granting access via Azure AD App-Only](https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azuread)
9494
- [wiki](https://github.com/vgrem/Office365-REST-Python-Client/wiki/How-to-connect-to-SharePoint-Online-with-certificate-credentials)
9595
96-
Example: [connect_with_client_certificate.py](examples/sharepoint/auth_client_certificate.py)
96+
Example: [with_certificate.py](examples/sharepoint/auth_certificate.py)
9797
9898
#### 4. Interactive
9999

generator/import_metadata.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ def export_to_file(path, content):
2626
"--endpoint",
2727
dest="endpoint",
2828
help="Import metadata endpoint",
29-
default="graph",
29+
default="sharepoint",
3030
)
3131
parser.add_argument(
3232
"-p",
3333
"--path",
3434
dest="path",
35-
default="./metadata/Graph.xml",
35+
default="./metadata/SharePoint.xml",
3636
help="Import metadata endpoint",
3737
)
3838

generator/metadata/SharePoint.xml

Lines changed: 107 additions & 14 deletions
Large diffs are not rendered by default.

office365/runtime/auth/authentication_context.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33
from typing import Any, Callable
44

5-
from typing_extensions import Required, TypedDict
5+
from typing_extensions import Required, Self, TypedDict
66

77
from office365.runtime.auth.client_credential import ClientCredential
88
from office365.runtime.auth.providers.acs_token_provider import ACSTokenProvider
@@ -211,12 +211,14 @@ def with_credentials(self, credentials):
211211
raise ValueError("Unknown credential type")
212212

213213
def _authenticate(request):
214+
# type: (RequestOptions) -> None
214215
provider.authenticate_request(request)
215216

216217
self._authenticate = _authenticate
217218
return self
218219

219220
def acquire_token_for_user(self, username, password):
221+
# type: (str, str) -> Self
220222
"""
221223
Initializes a client to acquire a token via user credentials
222224
Status: deprecated!
@@ -227,6 +229,7 @@ def acquire_token_for_user(self, username, password):
227229
provider = SamlTokenProvider(self.url, username, password, self._browser_mode)
228230

229231
def _authenticate(request):
232+
# type: (RequestOptions) -> None
230233
provider.authenticate_request(request)
231234

232235
self._authenticate = _authenticate
@@ -244,6 +247,7 @@ def acquire_token_for_app(self, client_id, client_secret):
244247
provider = ACSTokenProvider(self.url, client_id, client_secret)
245248

246249
def _authenticate(request):
250+
# type: (RequestOptions) -> None
247251
provider.authenticate_request(request)
248252

249253
self._authenticate = _authenticate

office365/runtime/auth/providers/acs_token_provider.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,9 @@ def __init__(self, url, client_id, client_secret, environment="commercial"):
3131

3232
def authenticate_request(self, request):
3333
# type: (RequestOptions) -> None
34-
self.ensure_app_only_access_token()
34+
self._ensure_app_only_access_token()
3535
request.set_header("Authorization", self._get_authorization_header())
3636

37-
def ensure_app_only_access_token(self):
38-
if self._cached_token is None:
39-
self._cached_token = self.get_app_only_access_token()
40-
return self._cached_token and self._cached_token.is_valid
41-
4237
def get_app_only_access_token(self):
4338
"""Retrieves an app-only access token from ACS"""
4439
try:
@@ -53,6 +48,11 @@ def get_app_only_access_token(self):
5348
)
5449
raise ValueError(self.error)
5550

51+
def _ensure_app_only_access_token(self):
52+
if self._cached_token is None:
53+
self._cached_token = self.get_app_only_access_token()
54+
return self._cached_token and self._cached_token.is_valid
55+
5656
def _get_app_only_access_token(self, target_host, target_realm):
5757
"""
5858
Retrieves an app-only access token from ACS to call the specified principal
@@ -119,6 +119,3 @@ def get_security_token_service_url(realm, environment):
119119

120120
def _get_authorization_header(self):
121121
return "Bearer {0}".format(self._cached_token.accessToken)
122-
123-
def get_last_error(self):
124-
return self.error

0 commit comments

Comments
 (0)