Skip to content

Commit 8b889d9

Browse files
authored
Merge pull request #953 from kamilkrukowski/master
Support Access Token Refresh for MSAL Provided Tokens (`with_interactive`, `with_device_flow`, `with_client_certificate`)
2 parents b038a95 + f01f74c commit 8b889d9

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

office365/runtime/auth/authentication_context.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import sys
3+
from datetime import datetime, timedelta, timezone
34
from typing import Any, Callable
45

56
from typing_extensions import Required, Self, TypedDict
@@ -46,6 +47,7 @@ def __init__(self, url, environment=None, allow_ntlm=False, browser_mode=False):
4647
self._environment = environment
4748
self._allow_ntlm = allow_ntlm
4849
self._browser_mode = browser_mode
50+
self._token_expires = datetime.max
4951

5052
def with_client_certificate(
5153
self,
@@ -178,8 +180,15 @@ def with_access_token(self, token_func):
178180
"""
179181

180182
def _authenticate(request):
181-
if self._cached_token is None:
183+
184+
request_time = datetime.now(timezone.utc)
185+
186+
if self._cached_token is None or request_time > self._token_expires:
182187
self._cached_token = token_func()
188+
if hasattr(self._cached_token, "expiresIn"):
189+
self._token_expires = request_time + timedelta(
190+
seconds=self._cached_token.expiresIn
191+
)
183192
request.set_header(
184193
"Authorization", _get_authorization_header(self._cached_token)
185194
)

0 commit comments

Comments
 (0)