mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-08-05 13:03:59 -06:00
Make it optional for the AuthService to have a preference object
This should make it easier if we ever want to re-use the authService, since it no longer has a hard link with the Preferences CURA-5744
This commit is contained in:
parent
0ccbabd857
commit
649f1c8961
1 changed files with 12 additions and 6 deletions
|
@ -14,6 +14,7 @@ from cura.OAuth2.Models import AuthenticationResponse
|
|||
|
||||
if TYPE_CHECKING:
|
||||
from cura.OAuth2.Models import UserProfile, OAuth2Settings
|
||||
from UM.Preferences import Preferences
|
||||
|
||||
|
||||
class AuthorizationService:
|
||||
|
@ -28,15 +29,18 @@ class AuthorizationService:
|
|||
# Emit signal when authentication failed.
|
||||
onAuthenticationError = Signal()
|
||||
|
||||
def __init__(self, preferences, settings: "OAuth2Settings") -> None:
|
||||
def __init__(self, preferences: Optional["Preferences"], settings: "OAuth2Settings") -> None:
|
||||
self._settings = settings
|
||||
self._auth_helpers = AuthorizationHelpers(settings)
|
||||
self._auth_url = "{}/authorize".format(self._settings.OAUTH_SERVER_URL)
|
||||
self._auth_data = None # type: Optional[AuthenticationResponse]
|
||||
self._user_profile = None # type: Optional["UserProfile"]
|
||||
self._cura_preferences = preferences
|
||||
self._preferences = preferences
|
||||
self._server = LocalAuthorizationServer(self._auth_helpers, self._onAuthStateChanged, daemon=True)
|
||||
|
||||
if self._preferences:
|
||||
self._preferences.addPreference(self._settings.AUTH_DATA_PREFERENCE_KEY, "{}")
|
||||
|
||||
# Get the user profile as obtained from the JWT (JSON Web Token).
|
||||
# If the JWT is not yet parsed, calling this will take care of that.
|
||||
# \return UserProfile if a user is logged in, None otherwise.
|
||||
|
@ -135,9 +139,11 @@ class AuthorizationService:
|
|||
|
||||
# Load authentication data from preferences.
|
||||
def loadAuthDataFromPreferences(self) -> None:
|
||||
self._cura_preferences.addPreference(self._settings.AUTH_DATA_PREFERENCE_KEY, "{}")
|
||||
if self._preferences is None:
|
||||
Logger.logException("e", "Unable to load authentication data, since no preference has been set!")
|
||||
return
|
||||
try:
|
||||
preferences_data = json.loads(self._cura_preferences.getValue(self._settings.AUTH_DATA_PREFERENCE_KEY))
|
||||
preferences_data = json.loads(self._preferences.getValue(self._settings.AUTH_DATA_PREFERENCE_KEY))
|
||||
if preferences_data:
|
||||
self._auth_data = AuthenticationResponse(**preferences_data)
|
||||
self.onAuthStateChanged.emit(logged_in=True)
|
||||
|
@ -149,7 +155,7 @@ class AuthorizationService:
|
|||
self._auth_data = auth_data
|
||||
if auth_data:
|
||||
self._user_profile = self.getUserProfile()
|
||||
self._cura_preferences.setValue(self._settings.AUTH_DATA_PREFERENCE_KEY, json.dumps(vars(auth_data)))
|
||||
self._preferences.setValue(self._settings.AUTH_DATA_PREFERENCE_KEY, json.dumps(vars(auth_data)))
|
||||
else:
|
||||
self._user_profile = None
|
||||
self._cura_preferences.resetPreference(self._settings.AUTH_DATA_PREFERENCE_KEY)
|
||||
self._preferences.resetPreference(self._settings.AUTH_DATA_PREFERENCE_KEY)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue