Skip to content

Commit

Permalink
Revert "Only refresh token if the token have expired when returning w…
Browse files Browse the repository at this point in the history
…b creds"
  • Loading branch information
adlius authored Feb 6, 2025
1 parent 8804e2a commit f99dd6b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 24 deletions.
22 changes: 10 additions & 12 deletions addon_service/authorized_account/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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)

Expand Down
4 changes: 1 addition & 3 deletions addon_service/common/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 2 additions & 9 deletions addon_service/management/commands/refresh_addon_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

0 comments on commit f99dd6b

Please sign in to comment.