From be08db05346b565801fe80c28d9c2526f93e2399 Mon Sep 17 00:00:00 2001 From: Benjamin Pritchard Date: Thu, 26 Oct 2023 12:06:09 -0400 Subject: [PATCH] Fix handling of jwt refreshes --- qcportal/qcportal/client_base.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/qcportal/qcportal/client_base.py b/qcportal/qcportal/client_base.py index 8b211896c..460ce482a 100644 --- a/qcportal/qcportal/client_base.py +++ b/qcportal/qcportal/client_base.py @@ -280,7 +280,11 @@ def _refresh_JWT_token(self) -> None: ) self._jwt_access_exp = decoded_access_token["exp"] - else: # shouldn't happen unless user is blacklisted + elif ret.status_code == 401 and "Token has expired" in ret.json()["msg"]: + # If the refresh token has expired, try to log in again + self._get_JWT_token() + else: # shouldn't happen unless user is blacklisted or something + print(ret, ret.text) raise ConnectionRefusedError("Unable to refresh JWT authorization token! This is a server issue!!") def _request( @@ -294,14 +298,14 @@ def _request( allow_retries: bool = True, ) -> requests.Response: - # If JWT token is expired, automatically renew it - if self._jwt_access_exp and self._jwt_access_exp < time.time(): - self._refresh_JWT_token() - # If refresh token has expired, log in again if self._jwt_refresh_exp and self._jwt_refresh_exp < time.time(): self._get_JWT_token() + # If only the JWT token is expired, automatically renew it + if self._jwt_access_exp and self._jwt_access_exp < time.time(): + self._refresh_JWT_token() + full_uri = self.address + endpoint req = requests.Request(method=method.upper(), url=full_uri, data=body, params=url_params)