From 8e32ecd216997251f7e22c57000f4a4eb7aa3b62 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Fri, 5 Nov 2021 17:52:50 +0000 Subject: [PATCH] CAPI: Actually try to use Refresh Token if CAPI says unauthorized The refactor for threaded CAPI worker has inadvertently caused us to always try *full* re-auth when the CAPI signals the Access Token is expired. --- EDMarketConnector.py | 7 +++++++ companion.py | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/EDMarketConnector.py b/EDMarketConnector.py index 35d2c58a..e561eb24 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -1159,6 +1159,13 @@ class AppWindow(object): # LANG: Frontier CAPI server error when fetching data self.status['text'] = _('Frontier CAPI server error') + except companion.CredentialsRequireRefresh: + # LANG: Frontier CAPI Access Token expired, trying to get a new one + self.status['text'] = _('CAPI: Refreshing access token...') + if companion.session.login(): + logger.debug('Initial query failed, but login() just worked, trying again...') + companion.session.retrying = True + except companion.CredentialsError: companion.session.retrying = False companion.session.invalidate() diff --git a/companion.py b/companion.py index 1a811858..7c9cc96a 100644 --- a/companion.py +++ b/companion.py @@ -265,6 +265,15 @@ class CredentialsError(Exception): self.args = (_('Error: Invalid Credentials'),) +class CredentialsRequireRefresh(Exception): + """Exception Class for CAPI credentials requiring refresh.""" + + def __init__(self, *args) -> None: + self.args = args + if not args: + self.args = ('CAPI: Requires refresh of Access Token',) + + class CmdrError(Exception): """Exception Class for CAPI Commander error. @@ -772,6 +781,7 @@ class Session(object): self.dump(r) if r.status_code == 401: # CAPI doesn't think we're Auth'd + # TODO: This needs to try a REFRESH, not a full re-auth # No need for translation, we'll go straight into trying new Auth # and thus any message would be overwritten. raise CredentialsError('Frontier CAPI said Auth required') from e