From f99dd6bb0a56d57881c3f6295778abb7406e7e4b Mon Sep 17 00:00:00 2001 From: Yuhuai Liu Date: Thu, 6 Feb 2025 09:52:21 -0500 Subject: [PATCH] Revert "Only refresh token if the token have expired when returning wb creds" --- addon_service/authorized_account/models.py | 22 +++++++++---------- addon_service/common/network.py | 4 +--- .../commands/refresh_addon_tokens.py | 11 ++-------- 3 files changed, 13 insertions(+), 24 deletions(-) diff --git a/addon_service/authorized_account/models.py b/addon_service/authorized_account/models.py index 8fd74a66..cf17384d 100644 --- a/addon_service/authorized_account/models.py +++ b/addon_service/authorized_account/models.py @@ -7,7 +7,6 @@ models, transaction, ) -from django.utils import timezone from addon_service.addon_imp.instantiation import get_addon_instance from addon_service.addon_operation.models import AddonOperationModel @@ -316,7 +315,7 @@ async def execute_post_auth_hook(self, auth_extras: dict | None = None): ### # async functions for use in oauth2 callback flows - async def refresh_oauth2_access_token(self, force=False) -> None: + async def refresh_oauth2_access_token(self) -> None: ( _oauth_client_config, _oauth_token_metadata, @@ -326,16 +325,15 @@ async def refresh_oauth2_access_token(self, force=False) -> None: or await sync_to_async(lambda: _oauth_token_metadata.access_token_only)() ): return - if force or _oauth_token_metadata.access_token_expiration < timezone.now(): - _fresh_token_result = await oauth2_utils.get_refreshed_access_token( - token_endpoint_url=_oauth_client_config.token_endpoint_url, - refresh_token=_oauth_token_metadata.refresh_token, - auth_callback_url=_oauth_client_config.auth_callback_url, - client_id=_oauth_client_config.client_id, - client_secret=_oauth_client_config.client_secret, - ) - await _oauth_token_metadata.update_with_fresh_token(_fresh_token_result) - await self.arefresh_from_db() + _fresh_token_result = await oauth2_utils.get_refreshed_access_token( + token_endpoint_url=_oauth_client_config.token_endpoint_url, + refresh_token=_oauth_token_metadata.refresh_token, + auth_callback_url=_oauth_client_config.auth_callback_url, + client_id=_oauth_client_config.client_id, + client_secret=_oauth_client_config.client_secret, + ) + await _oauth_token_metadata.update_with_fresh_token(_fresh_token_result) + await self.arefresh_from_db() refresh_oauth_access_token__blocking = async_to_sync(refresh_oauth2_access_token) diff --git a/addon_service/common/network.py b/addon_service/common/network.py index 49d15d90..1e923a60 100644 --- a/addon_service/common/network.py +++ b/addon_service/common/network.py @@ -83,9 +83,7 @@ async def _do_send(self, request: HttpRequestInfo): async with self._try_send(request) as _response: yield _response except exceptions.ExpiredAccessToken: - await _PrivateNetworkInfo.get(self).account.refresh_oauth2_access_token( - force=True - ) + await _PrivateNetworkInfo.get(self).account.refresh_oauth2_access_token() # if this one fails, don't try refreshing again async with self._try_send(request) as _response: yield _response diff --git a/addon_service/management/commands/refresh_addon_tokens.py b/addon_service/management/commands/refresh_addon_tokens.py index 98943b6c..735d7b12 100644 --- a/addon_service/management/commands/refresh_addon_tokens.py +++ b/addon_service/management/commands/refresh_addon_tokens.py @@ -44,7 +44,7 @@ def refresh_addon_tokens_for_external_service( allowance -= 1 last_call = time.time() - account.refresh_oauth_access_token__blocking(force=True) + account.refresh_oauth_access_token__blocking() @celery.shared_task @@ -68,11 +68,4 @@ def add_arguments(self, parser): def handle(self, *args, **options): fake = options["fake"] - refresh_addon_tokens( - addons={ - "box": 60, - "googledrive": 14, - "mendeley": 14, - }, - fake=fake, - ) + refresh_addon_tokens(addons=["box", "googledrive", "mendeley"], fake=fake)