mirror of
https://git.mirrors.martin98.com/https://github.com/Ultimaker/Cura
synced 2025-08-12 18:09:03 +08:00
Calculate expiry date to determine if token refresh is needed
This commit is contained in:
parent
9d6bd4b29a
commit
05c4b6012e
@ -57,7 +57,6 @@ class Account(QObject):
|
|||||||
|
|
||||||
def initialize(self) -> None:
|
def initialize(self) -> None:
|
||||||
self._authorization_service.initialize(self._application.getPreferences())
|
self._authorization_service.initialize(self._application.getPreferences())
|
||||||
|
|
||||||
self._authorization_service.onAuthStateChanged.connect(self._onLoginStateChanged)
|
self._authorization_service.onAuthStateChanged.connect(self._onLoginStateChanged)
|
||||||
self._authorization_service.onAuthenticationError.connect(self._onLoginStateChanged)
|
self._authorization_service.onAuthenticationError.connect(self._onLoginStateChanged)
|
||||||
self._authorization_service.loadAuthDataFromPreferences()
|
self._authorization_service.loadAuthDataFromPreferences()
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
# Copyright (c) 2018 Ultimaker B.V.
|
# Copyright (c) 2018 Ultimaker B.V.
|
||||||
# Cura is released under the terms of the LGPLv3 or higher.
|
# Cura is released under the terms of the LGPLv3 or higher.
|
||||||
|
from datetime import datetime
|
||||||
import json
|
import json
|
||||||
import random
|
import random
|
||||||
from hashlib import sha512
|
from hashlib import sha512
|
||||||
from base64 import b64encode
|
from base64 import b64encode
|
||||||
from typing import Dict, Optional
|
from typing import Optional
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
@ -75,7 +76,8 @@ class AuthorizationHelpers:
|
|||||||
access_token=token_data["access_token"],
|
access_token=token_data["access_token"],
|
||||||
refresh_token=token_data["refresh_token"],
|
refresh_token=token_data["refresh_token"],
|
||||||
expires_in=token_data["expires_in"],
|
expires_in=token_data["expires_in"],
|
||||||
scope=token_data["scope"])
|
scope=token_data["scope"],
|
||||||
|
received_at=datetime.now())
|
||||||
|
|
||||||
# Calls the authentication API endpoint to get the token data.
|
# Calls the authentication API endpoint to get the token data.
|
||||||
# \param access_token: The encoded JWT token.
|
# \param access_token: The encoded JWT token.
|
||||||
@ -99,7 +101,8 @@ class AuthorizationHelpers:
|
|||||||
return UserProfile(
|
return UserProfile(
|
||||||
user_id = user_data["user_id"],
|
user_id = user_data["user_id"],
|
||||||
username = user_data["username"],
|
username = user_data["username"],
|
||||||
profile_image_url = user_data.get("profile_image_url", "")
|
profile_image_url = user_data.get("profile_image_url", ""),
|
||||||
|
generated_at = datetime.now()
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -9,7 +9,6 @@ from cura.OAuth2.Models import AuthenticationResponse, ResponseData, HTTP_STATUS
|
|||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from cura.OAuth2.Models import ResponseStatus
|
from cura.OAuth2.Models import ResponseStatus
|
||||||
from cura.OAuth2.AuthorizationHelpers import AuthorizationHelpers
|
|
||||||
|
|
||||||
|
|
||||||
# This handler handles all HTTP requests on the local web server.
|
# This handler handles all HTTP requests on the local web server.
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
# Cura is released under the terms of the LGPLv3 or higher.
|
# Cura is released under the terms of the LGPLv3 or higher.
|
||||||
import json
|
import json
|
||||||
import webbrowser
|
import webbrowser
|
||||||
|
from datetime import timedelta, datetime
|
||||||
from typing import Optional, TYPE_CHECKING
|
from typing import Optional, TYPE_CHECKING
|
||||||
from urllib.parse import urlencode
|
from urllib.parse import urlencode
|
||||||
import requests.exceptions
|
import requests.exceptions
|
||||||
@ -51,11 +52,12 @@ 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"]:
|
||||||
try:
|
if not self._user_profile:
|
||||||
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,10 +89,10 @@ class AuthorizationService:
|
|||||||
|
|
||||||
# Get the access token as provided by the response 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():
|
# Check if the current access token is expired and refresh it if that is the case.
|
||||||
# We check if we can get the user profile.
|
creation_date = self._auth_data.received_at or datetime(2000, 1, 1)
|
||||||
# If we can't get it, that means the access token (JWT) was invalid or expired.
|
expiry_date = creation_date + timedelta(seconds = float(self._auth_data.expires_in))
|
||||||
# In that case we try to refresh the access token.
|
if datetime.now() > expiry_date:
|
||||||
self.refreshAccessToken()
|
self.refreshAccessToken()
|
||||||
|
|
||||||
if self._auth_data is None:
|
if self._auth_data is None:
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
# Copyright (c) 2018 Ultimaker B.V.
|
# Copyright (c) 2018 Ultimaker B.V.
|
||||||
# Cura is released under the terms of the LGPLv3 or higher.
|
# Cura is released under the terms of the LGPLv3 or higher.
|
||||||
|
from datetime import datetime
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
@ -38,6 +38,7 @@ class AuthenticationResponse(BaseModel):
|
|||||||
expires_in = None # type: Optional[str]
|
expires_in = None # type: Optional[str]
|
||||||
scope = None # type: Optional[str]
|
scope = None # type: Optional[str]
|
||||||
err_message = None # type: Optional[str]
|
err_message = None # type: Optional[str]
|
||||||
|
received_at = None # type: Optional[datetime]
|
||||||
|
|
||||||
|
|
||||||
# Response status template.
|
# Response status template.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user