Call getAccessTokenUsingRefreshToken asynchronously

Getting into nested inline functions into inline functions here. Let's see what the reviewer thinks of all this!

Contributes to issue CURA-8539.
This commit is contained in:
Ghostkeeper 2021-11-19 17:04:37 +01:00
parent 7091c5f3aa
commit 9895015235
No known key found for this signature in database
GPG Key ID: D2A8871EE34EC59A

View File

@ -115,17 +115,20 @@ class AuthorizationService:
Logger.warning("There was no refresh token in the auth data.") Logger.warning("There was no refresh token in the auth data.")
callback(None) callback(None)
return return
self._auth_data = self._auth_helpers.getAccessTokenUsingRefreshToken(self._auth_data.refresh_token) # TODO: Blocks main thread, preventing HttpRequestManager from functioning?
if not self._auth_data or self._auth_data.access_token is None: def process_auth_data(auth_data: AuthenticationResponse):
if auth_data.access_token is None:
Logger.warning("Unable to use the refresh token to get a new access token.") Logger.warning("Unable to use the refresh token to get a new access token.")
callback(None) callback(None)
return return
# Ensure it gets stored as otherwise we only have it in memory. The stored refresh token has been deleted # Ensure it gets stored as otherwise we only have it in memory. The stored refresh token has been
# from the server already. Do not store the auth_data if we could not get new auth_data (e.g. due to a # deleted from the server already. Do not store the auth_data if we could not get new auth_data (e.g.
# network error), since this would cause an infinite loop trying to get new auth-data. # due to a network error), since this would cause an infinite loop trying to get new auth-data.
if self._auth_data.success: if auth_data.success:
self._storeAuthData(self._auth_data) self._storeAuthData(auth_data)
self._auth_helpers.checkToken(self._auth_data.access_token, callback, lambda: callback(None)) self._auth_helpers.checkToken(auth_data.access_token, callback, lambda: callback(None))
self._auth_helpers.getAccessTokenUsingRefreshToken(self._auth_data.refresh_token, process_auth_data)
self._auth_helpers.checkToken(self._auth_data.access_token, check_user_profile) self._auth_helpers.checkToken(self._auth_data.access_token, check_user_profile)
@ -152,14 +155,17 @@ class AuthorizationService:
if self._auth_data is None or self._auth_data.refresh_token is None: 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.") Logger.log("w", "Unable to refresh access token, since there is no refresh token.")
return return
response = self._auth_helpers.getAccessTokenUsingRefreshToken(self._auth_data.refresh_token)
def process_auth_data(response: AuthenticationResponse):
if response.success: if response.success:
self._storeAuthData(response) self._storeAuthData(response)
self.onAuthStateChanged.emit(logged_in = True) self.onAuthStateChanged.emit(logged_in = True)
else: else:
Logger.log("w", "Failed to get a new access token from the server.") Logger.warning("Failed to get a new access token from the server.")
self.onAuthStateChanged.emit(logged_in = False) self.onAuthStateChanged.emit(logged_in = False)
self._auth_helpers.getAccessTokenUsingRefreshToken(self._auth_data.refresh_token, process_auth_data)
def deleteAuthData(self) -> None: def deleteAuthData(self) -> None:
"""Delete the authentication data that we have stored locally (eg; logout)""" """Delete the authentication data that we have stored locally (eg; logout)"""