Browse Source

Fixes for storing timestamp

ChrisTerBeke 6 years ago
parent
commit
48c756b01d
3 changed files with 16 additions and 13 deletions
  1. 4 1
      cura/OAuth2/AuthorizationHelpers.py
  2. 10 9
      cura/OAuth2/AuthorizationService.py
  3. 2 3
      cura/OAuth2/Models.py

+ 4 - 1
cura/OAuth2/AuthorizationHelpers.py

@@ -14,6 +14,9 @@ from UM.Logger import Logger
 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 AuthorizationHelpers:
     def __init__(self, settings: "OAuth2Settings") -> None:
@@ -77,7 +80,7 @@ class AuthorizationHelpers:
                                       refresh_token=token_data["refresh_token"],
                                       expires_in=token_data["expires_in"],
                                       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.
     #   \param access_token: The encoded JWT token.

+ 10 - 9
cura/OAuth2/AuthorizationService.py

@@ -2,7 +2,7 @@
 # Cura is released under the terms of the LGPLv3 or higher.
 import json
 import webbrowser
-from datetime import timedelta, datetime
+from datetime import datetime, timedelta
 from typing import Optional, TYPE_CHECKING
 from urllib.parse import urlencode
 import requests.exceptions
@@ -12,7 +12,7 @@ from UM.Logger import Logger
 from UM.Signal import Signal
 
 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
 
 if TYPE_CHECKING:
@@ -89,17 +89,18 @@ class AuthorizationService:
 
     #   Get the access token as provided by the response data.
     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:
             Logger.log("d", "No auth data to retrieve the access_token from")
             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.
     def refreshAccessToken(self) -> None:

+ 2 - 3
cura/OAuth2/Models.py

@@ -1,6 +1,5 @@
 # Copyright (c) 2018 Ultimaker B.V.
 # Cura is released under the terms of the LGPLv3 or higher.
-from datetime import datetime
 from typing import Optional
 
 
@@ -38,13 +37,13 @@ class AuthenticationResponse(BaseModel):
     expires_in = None  # type: Optional[str]
     scope = 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.
 class ResponseStatus(BaseModel):
     code = 200  # type: int
-    message = ""  # type str
+    message = ""  # type: str
 
 
 # Response data template.