Skip to content

Commit

Permalink
Don't use hardcoded expiry time, take from reply
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 11, 2023
1 parent 60a8a89 commit f15a7dc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
14 changes: 11 additions & 3 deletions koordinates/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def do_GET(self):

self.server.apikey = resp["key"]
self.server.refresh_token = refresh_token
self.server.expires_in = expires_in
self._send_response()

def _send_response(self):
Expand All @@ -184,6 +185,8 @@ class OAuthWorkflow(QThread):
finished = pyqtSignal(str, str)
error_occurred = pyqtSignal(str)

EXPIRY_DURATION_SECONDS = 0

def __init__(self):
super().__init__()

Expand Down Expand Up @@ -239,6 +242,7 @@ def _refresh_oauth_finished(self, reply: QNetworkReply):

access_token = result['access_token']
refresh_token = result['refresh_token']
expires_in = result['expires_in']

body = {
"scope": SCOPE_KX,
Expand All @@ -259,11 +263,13 @@ def _refresh_oauth_finished(self, reply: QNetworkReply):
self._refresh_kx_key_reply.finished.connect(
partial(self._refresh_kx_key_finished,
self._refresh_kx_key_reply,
refresh_token))
refresh_token,
expires_in))

def _refresh_kx_key_finished(self,
reply: QNetworkReply,
refresh_token: str):
refresh_token: str,
expires_in: int):
if (reply != self._refresh_kx_key_reply or
sip.isdeleted(self._refresh_kx_key_reply)):
return
Expand All @@ -277,7 +283,7 @@ def _refresh_kx_key_finished(self,
return

kx_key = result['key']

OAuthWorkflow.EXPIRY_DURATION_SECONDS = expires_in
self.finished.emit(kx_key, refresh_token)

def force_stop(self):
Expand All @@ -298,6 +304,7 @@ def run(self):
self.server.code_verifier = self.code_verifier
self.server.apikey = None
self.server.refresh_token = None
self.server.expires_in = None
self.server.error = None
QDesktopServices.openUrl(QUrl(self.authorization_url))

Expand All @@ -306,6 +313,7 @@ def run(self):
err = self.server.error
apikey = self.server.apikey
refresh_token = self.server.refresh_token
OAuthWorkflow.EXPIRY_DURATION_SECONDS = self.server.expires_in or 0

if err:
self.error_occurred.emit(err)
Expand Down
15 changes: 8 additions & 7 deletions koordinates/gui/login_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,14 @@ def _auth_finished(self, key: str, refresh_token: Optional[str]):
self.login_button.set_state(AuthState.LoggedIn)
self.open_login_window_label.hide()

self.oauth_refresh_timer = QTimer(self)
self.oauth_refresh_timer.setSingleShot(True)
# request refresh after 9 hours, even though the token
# expires after 10...
self.oauth_refresh_timer.setInterval(32400*1000)
self.oauth_refresh_timer.timeout.connect(self._refresh_auth)
self.oauth_refresh_timer.start()
if OAuthWorkflow.EXPIRY_DURATION_SECONDS > 0:
self.oauth_refresh_timer = QTimer(self)
self.oauth_refresh_timer.setSingleShot(True)
# request refresh 30 mins before expiry
self.oauth_refresh_timer.setInterval(
(OAuthWorkflow.EXPIRY_DURATION_SECONDS - 1800) * 1000)
self.oauth_refresh_timer.timeout.connect(self._refresh_auth)
self.oauth_refresh_timer.start()

except FileExistsError:
iface.messageBar().pushMessage(
Expand Down

0 comments on commit f15a7dc

Please sign in to comment.