From 62f913802f5647c891b8339157bdb17bf1fe6773 Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Thu, 28 Feb 2019 12:58:41 +0100 Subject: [PATCH 1/2] Prevent crash if the connection was lost for auth token --- cura/OAuth2/AuthorizationHelpers.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cura/OAuth2/AuthorizationHelpers.py b/cura/OAuth2/AuthorizationHelpers.py index bf70bf693a..b7269140c7 100644 --- a/cura/OAuth2/AuthorizationHelpers.py +++ b/cura/OAuth2/AuthorizationHelpers.py @@ -41,7 +41,10 @@ class AuthorizationHelpers: "code_verifier": verification_code, "scope": self._settings.CLIENT_SCOPES if self._settings.CLIENT_SCOPES is not None else "", } - return self.parseTokenResponse(requests.post(self._token_url, data = data)) # type: ignore + try: + return self.parseTokenResponse(requests.post(self._token_url, data = data)) # type: ignore + except requests.exceptions.ConnectionError: + return AuthenticationResponse(success=False, err_message="Unable to connect to remote server") # Request the access token from the authorization server using a refresh token. # \param refresh_token: @@ -54,7 +57,10 @@ class AuthorizationHelpers: "refresh_token": refresh_token, "scope": self._settings.CLIENT_SCOPES if self._settings.CLIENT_SCOPES is not None else "", } - return self.parseTokenResponse(requests.post(self._token_url, data = data)) # type: ignore + try: + return self.parseTokenResponse(requests.post(self._token_url, data = data)) # type: ignore + except requests.exceptions.ConnectionError: + return AuthenticationResponse(success=False, err_message="Unable to connect to remote server") @staticmethod # Parse the token response from the authorization server into an AuthenticationResponse object. From a8128556f3183f5b1b6e07bd0d0598989d95d4f5 Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Thu, 28 Feb 2019 14:54:59 +0100 Subject: [PATCH 2/2] Don't drop refresh token if request failed --- cura/OAuth2/AuthorizationService.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cura/OAuth2/AuthorizationService.py b/cura/OAuth2/AuthorizationService.py index d4ae962f7c..ab4ddcebc9 100644 --- a/cura/OAuth2/AuthorizationService.py +++ b/cura/OAuth2/AuthorizationService.py @@ -121,8 +121,12 @@ class AuthorizationService: if self._auth_data is None or self._auth_data.refresh_token is None: Logger.log("w", "Unable to refresh access token, since there is no refresh token.") return - self._storeAuthData(self._auth_helpers.getAccessTokenUsingRefreshToken(self._auth_data.refresh_token)) - self.onAuthStateChanged.emit(logged_in=True) + response = self._auth_helpers.getAccessTokenUsingRefreshToken(self._auth_data.refresh_token) + if response.success: + self._storeAuthData(response) + self.onAuthStateChanged.emit(logged_in=True) + else: + self.onAuthStateChanged(logged_in = False) # Delete the authentication data that we have stored locally (eg; logout) def deleteAuthData(self) -> None: