mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-16 03:07:53 -06:00
Check access token before using it
This commit is contained in:
parent
85607d0e68
commit
9d6bd4b29a
2 changed files with 11 additions and 12 deletions
|
@ -51,13 +51,11 @@ class AuthorizationService:
|
||||||
# \return UserProfile if a user is logged in, None otherwise.
|
# \return UserProfile if a user is logged in, None otherwise.
|
||||||
# \sa _parseJWT
|
# \sa _parseJWT
|
||||||
def getUserProfile(self) -> Optional["UserProfile"]:
|
def getUserProfile(self) -> Optional["UserProfile"]:
|
||||||
if not self._user_profile:
|
try:
|
||||||
# If no user profile was stored locally, we try to get it from JWT.
|
self._user_profile = self._parseJWT()
|
||||||
try:
|
except requests.exceptions.ConnectionError:
|
||||||
self._user_profile = self._parseJWT()
|
# Unable to get connection, can't login.
|
||||||
except requests.exceptions.ConnectionError:
|
return None
|
||||||
# Unable to get connection, can't login.
|
|
||||||
return None
|
|
||||||
|
|
||||||
if not self._user_profile and self._auth_data:
|
if not self._user_profile and self._auth_data:
|
||||||
# If there is still no user profile from the JWT, we have to log in again.
|
# If there is still no user profile from the JWT, we have to log in again.
|
||||||
|
@ -87,13 +85,13 @@ class AuthorizationService:
|
||||||
|
|
||||||
return self._auth_helpers.parseJWT(self._auth_data.access_token)
|
return self._auth_helpers.parseJWT(self._auth_data.access_token)
|
||||||
|
|
||||||
# Get the access token as provided by the repsonse data.
|
# Get the access token as provided by the response data.
|
||||||
def getAccessToken(self) -> Optional[str]:
|
def getAccessToken(self) -> Optional[str]:
|
||||||
if not self.getUserProfile():
|
if not self.getUserProfile():
|
||||||
# We check if we can get the user profile.
|
# We check if we can get the user profile.
|
||||||
# If we can't get it, that means the access token (JWT) was invalid or expired.
|
# If we can't get it, that means the access token (JWT) was invalid or expired.
|
||||||
Logger.log("w", "Unable to get the user profile.")
|
# In that case we try to refresh the access token.
|
||||||
return None
|
self.refreshAccessToken()
|
||||||
|
|
||||||
if self._auth_data is None:
|
if self._auth_data is None:
|
||||||
Logger.log("d", "No auth data to retrieve the access_token from")
|
Logger.log("d", "No auth data to retrieve the access_token from")
|
||||||
|
|
|
@ -103,8 +103,9 @@ class CloudApiClient:
|
||||||
request = QNetworkRequest(QUrl(path))
|
request = QNetworkRequest(QUrl(path))
|
||||||
if content_type:
|
if content_type:
|
||||||
request.setHeader(QNetworkRequest.ContentTypeHeader, content_type)
|
request.setHeader(QNetworkRequest.ContentTypeHeader, content_type)
|
||||||
if self._account.isLoggedIn:
|
access_token = self._account.accessToken
|
||||||
request.setRawHeader(b"Authorization", "Bearer {}".format(self._account.accessToken).encode())
|
if access_token:
|
||||||
|
request.setRawHeader(b"Authorization", "Bearer {}".format(access_token).encode())
|
||||||
return request
|
return request
|
||||||
|
|
||||||
## Parses the given JSON network reply into a status code and a dictionary, handling unexpected errors as well.
|
## Parses the given JSON network reply into a status code and a dictionary, handling unexpected errors as well.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue