Skip to content

Commit

Permalink
fix: race-condition on token request close to expiry (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip1x9 authored Aug 28, 2024
1 parent 626cc5e commit 8a37076
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/fds/sdk/utils/authentication/confidential.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def _is_cached_token_valid(self) -> bool:
if not self._cached_token:
log.debug("Access Token cache is empty")
return False
if time.time() < self._cached_token[CONSTS.TOKEN_EXPIRES_AT]:
if time.time() < self._cached_token[CONSTS.TOKEN_EXPIRES_AT] - CONSTS.TOKEN_EXPIRY_OFFSET_SECS:
return True
else:
log.debug("Cached access token has expired at %s", self._cached_token[CONSTS.TOKEN_EXPIRES_AT])
Expand Down
1 change: 1 addition & 0 deletions src/fds/sdk/utils/authentication/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class CONSTS:
# access token
TOKEN_ACCESS_TOKEN = "access_token"
TOKEN_EXPIRES_AT = "expires_at"
TOKEN_EXPIRY_OFFSET_SECS = 30

# config
CONFIG_CLIENT_ID = "clientId"
Expand Down
6 changes: 3 additions & 3 deletions tests/fds/sdk/utils/authentication/test_confidential.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def test_get_access_token_cached(example_config, mocker, caplog):
mock_oauth2_session = mocker.patch("fds.sdk.utils.authentication.confidential.OAuth2Session")
mock_oauth2_session.return_value.fetch_token.return_value = {
"access_token": "test",
"expires_at": 10,
"expires_at": 40,
}
mocker.patch("fds.sdk.utils.authentication.confidential.time.time", return_value=0)

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

assert "Retrieving cached token. Expires in '10' seconds" in caplog.text
assert "Retrieving cached token. Expires in '40' seconds" in caplog.text


def test_get_access_token_cache_expired(client, mocker, caplog):
Expand All @@ -428,7 +428,7 @@ def test_get_access_token_cache_expired(client, mocker, caplog):
"fds.sdk.utils.authentication.confidential.OAuth2Session.fetch_token",
return_value={
"access_token": "test",
"expires_at": 10,
"expires_at": 30,
},
)

Expand Down

0 comments on commit 8a37076

Please sign in to comment.