Skip to content

Commit

Permalink
don't logout if access token not expired
Browse files Browse the repository at this point in the history
  • Loading branch information
hasan7n committed Aug 15, 2024
1 parent bf9fdb3 commit 4030032
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 8 additions & 3 deletions cli/medperf/comms/auth/auth0.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ def _access_token(self):
- config.refresh_token_expiration_leeway
)
current_time = time.time()

if current_time < sliding_expiration_time:
# Access token not expired. No need to refresh.
return access_token

# So we need to refresh.
if current_time > absolute_expiration_time:
# Expired refresh token. Force logout and ask the user to re-authenticate
logging.debug(
Expand All @@ -212,9 +218,8 @@ def _access_token(self):
self.logout()
raise AuthenticationError("Token expired. Please re-authenticate")

if current_time > sliding_expiration_time:
# Expired access token. Refresh it.
access_token = self.__refresh_access_token(refresh_token)
# Expired access token and not expired refresh token. Refresh.
access_token = self.__refresh_access_token(refresh_token)

return access_token

Expand Down
6 changes: 5 additions & 1 deletion cli/medperf/tests/comms/test_auth0.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_token_is_not_refreshed_if_not_expired(mocker, setup):
"access_token": "",
"token_expires_in": 900,
"token_issued_at": time.time(),
"logged_in_at": time.time(),
}
mocker.patch(PATCH_AUTH.format("read_credentials"), return_value=creds)
spy = mocker.patch(PATCH_AUTH.format("Auth0._Auth0__refresh_access_token"))
Expand All @@ -58,6 +59,7 @@ def test_token_is_refreshed_if_expired(mocker, setup):
"access_token": "",
"token_expires_in": expiration_time,
"token_issued_at": mocked_issued_at,
"logged_in_at": time.time(),
}
mocker.patch(PATCH_AUTH.format("read_credentials"), return_value=creds)
spy = mocker.patch(PATCH_AUTH.format("Auth0._Auth0__refresh_access_token"))
Expand All @@ -73,12 +75,14 @@ def test_logs_out_if_session_reaches_token_absolute_expiration_time(mocker, setu
# Arrange
expiration_time = 900
absolute_expiration_time = config.token_absolute_expiry
mocked_issued_at = time.time() - absolute_expiration_time
mocked_logged_in_at = time.time() - absolute_expiration_time
mocked_issued_at = time.time() - expiration_time
creds = {
"refresh_token": "",
"access_token": "",
"token_expires_in": expiration_time,
"token_issued_at": mocked_issued_at,
"logged_in_at": mocked_logged_in_at,
}
mocker.patch(PATCH_AUTH.format("read_credentials"), return_value=creds)
spy = mocker.patch(PATCH_AUTH.format("Auth0.logout"))
Expand Down

0 comments on commit 4030032

Please sign in to comment.