Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oauth workflow changes followup #307

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions koordinates/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from qgis.PyQt.QtCore import (
QThread,
pyqtSignal,
QUrl
QUrl,
QUrlQuery
)
from qgis.PyQt.QtGui import QDesktopServices
from qgis.PyQt.QtNetwork import (
Expand Down Expand Up @@ -158,6 +159,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 +186,8 @@ class OAuthWorkflow(QThread):
finished = pyqtSignal(str, str)
error_occurred = pyqtSignal(str)

EXPIRY_DURATION_SECONDS = 0

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

Expand All @@ -209,16 +213,17 @@ def __init__(self):
self._refresh_kx_key_reply: Optional[QNetworkReply] = None

def refresh(self, refresh_token: str):
body = (f"grant_type=refresh_token&"
f"client_id={CLIENT_ID}&"
f"refresh_token={refresh_token}")
query = QUrlQuery()
query.addQueryItem('grant_type', 'refresh_token')
query.addQueryItem('client_id', CLIENT_ID)
query.addQueryItem('refresh_token', refresh_token)

network_request = QNetworkRequest(QUrl(TOKEN_URL))
network_request.setHeader(QNetworkRequest.ContentTypeHeader,
'application/x-www-form-urlencoded')
self._refresh_reply = QgsNetworkAccessManager.instance().post(
network_request,
body.encode()
query.toString(QUrl.FullyEncoded).encode()
)
self._refresh_reply.finished.connect(
partial(self._refresh_oauth_finished, self._refresh_reply))
Expand All @@ -239,6 +244,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 +265,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 +285,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 +306,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 +315,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
Loading