Skip to content

Commit 8a37076

Browse files
authored
fix: race-condition on token request close to expiry (#102)
1 parent 626cc5e commit 8a37076

3 files changed

Lines changed: 5 additions & 4 deletions

File tree

src/fds/sdk/utils/authentication/confidential.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def _is_cached_token_valid(self) -> bool:
242242
if not self._cached_token:
243243
log.debug("Access Token cache is empty")
244244
return False
245-
if time.time() < self._cached_token[CONSTS.TOKEN_EXPIRES_AT]:
245+
if time.time() < self._cached_token[CONSTS.TOKEN_EXPIRES_AT] - CONSTS.TOKEN_EXPIRY_OFFSET_SECS:
246246
return True
247247
else:
248248
log.debug("Cached access token has expired at %s", self._cached_token[CONSTS.TOKEN_EXPIRES_AT])

src/fds/sdk/utils/authentication/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class CONSTS:
1414
# access token
1515
TOKEN_ACCESS_TOKEN = "access_token"
1616
TOKEN_EXPIRES_AT = "expires_at"
17+
TOKEN_EXPIRY_OFFSET_SECS = 30
1718

1819
# config
1920
CONFIG_CLIENT_ID = "clientId"

tests/fds/sdk/utils/authentication/test_confidential.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ def test_get_access_token_cached(example_config, mocker, caplog):
409409
mock_oauth2_session = mocker.patch("fds.sdk.utils.authentication.confidential.OAuth2Session")
410410
mock_oauth2_session.return_value.fetch_token.return_value = {
411411
"access_token": "test",
412-
"expires_at": 10,
412+
"expires_at": 40,
413413
}
414414
mocker.patch("fds.sdk.utils.authentication.confidential.time.time", return_value=0)
415415

@@ -418,7 +418,7 @@ def test_get_access_token_cached(example_config, mocker, caplog):
418418
assert client.get_access_token() == client.get_access_token()
419419
mock_oauth2_session.return_value.fetch_token.assert_called_once()
420420

421-
assert "Retrieving cached token. Expires in '10' seconds" in caplog.text
421+
assert "Retrieving cached token. Expires in '40' seconds" in caplog.text
422422

423423

424424
def test_get_access_token_cache_expired(client, mocker, caplog):
@@ -428,7 +428,7 @@ def test_get_access_token_cache_expired(client, mocker, caplog):
428428
"fds.sdk.utils.authentication.confidential.OAuth2Session.fetch_token",
429429
return_value={
430430
"access_token": "test",
431-
"expires_at": 10,
431+
"expires_at": 30,
432432
},
433433
)
434434

0 commit comments

Comments
 (0)