Fixes for storing timestamp

This commit is contained in:
ChrisTerBeke 2019-02-08 22:24:14 +01:00
parent 05c4b6012e
commit 48c756b01d
No known key found for this signature in database
GPG key ID: A49F1AB9D7E0C263
3 changed files with 16 additions and 13 deletions

View file

@ -14,6 +14,9 @@ from UM.Logger import Logger
from cura.OAuth2.Models import AuthenticationResponse, UserProfile, OAuth2Settings from cura.OAuth2.Models import AuthenticationResponse, UserProfile, OAuth2Settings
TOKEN_TIMESTAMP_FORMAT = "%Y-%m-%d %H:%M:%S"
# Class containing several helpers to deal with the authorization flow. # Class containing several helpers to deal with the authorization flow.
class AuthorizationHelpers: class AuthorizationHelpers:
def __init__(self, settings: "OAuth2Settings") -> None: def __init__(self, settings: "OAuth2Settings") -> None:
@ -77,7 +80,7 @@ class AuthorizationHelpers:
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()) received_at=datetime.now().strftime(TOKEN_TIMESTAMP_FORMAT))
# 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.

View file

@ -2,7 +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 datetime import datetime, timedelta
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
@ -12,7 +12,7 @@ from UM.Logger import Logger
from UM.Signal import Signal from UM.Signal import Signal
from cura.OAuth2.LocalAuthorizationServer import LocalAuthorizationServer from cura.OAuth2.LocalAuthorizationServer import LocalAuthorizationServer
from cura.OAuth2.AuthorizationHelpers import AuthorizationHelpers from cura.OAuth2.AuthorizationHelpers import AuthorizationHelpers, TOKEN_TIMESTAMP_FORMAT
from cura.OAuth2.Models import AuthenticationResponse from cura.OAuth2.Models import AuthenticationResponse
if TYPE_CHECKING: if TYPE_CHECKING:
@ -89,17 +89,18 @@ 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]:
# Check if the current access token is expired and refresh it if that is the case.
creation_date = self._auth_data.received_at or datetime(2000, 1, 1)
expiry_date = creation_date + timedelta(seconds = float(self._auth_data.expires_in))
if datetime.now() > expiry_date:
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")
return None return None
return self._auth_data.access_token # Check if the current access token is expired and refresh it if that is the case.
received_at = datetime.strptime(self._auth_data.received_at, TOKEN_TIMESTAMP_FORMAT) \
if self._auth_data.received_at else datetime(2000, 1, 1)
expiry_date = received_at + timedelta(seconds = float(self._auth_data.expires_in or 0))
if datetime.now() > expiry_date:
self.refreshAccessToken()
return self._auth_data.access_token if self._auth_data else None
# Try to refresh the access token. This should be used when it has expired. # Try to refresh the access token. This should be used when it has expired.
def refreshAccessToken(self) -> None: def refreshAccessToken(self) -> None:

View file

@ -1,6 +1,5 @@
# 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,13 +37,13 @@ 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] received_at = None # type: Optional[str]
# Response status template. # Response status template.
class ResponseStatus(BaseModel): class ResponseStatus(BaseModel):
code = 200 # type: int code = 200 # type: int
message = "" # type str message = "" # type: str
# Response data template. # Response data template.