From 3c86bf140ceba3e6830b0d483fdba5d17857e852 Mon Sep 17 00:00:00 2001 From: Erwan MATHIEU Date: Mon, 4 Dec 2023 19:47:07 +0100 Subject: [PATCH 1/4] Add support for OAuth2 client secret --- cura/OAuth2/AuthorizationHelpers.py | 2 ++ cura/OAuth2/Models.py | 1 + 2 files changed, 3 insertions(+) diff --git a/cura/OAuth2/AuthorizationHelpers.py b/cura/OAuth2/AuthorizationHelpers.py index a654ee4bdb..569ba80659 100644 --- a/cura/OAuth2/AuthorizationHelpers.py +++ b/cura/OAuth2/AuthorizationHelpers.py @@ -40,6 +40,7 @@ class AuthorizationHelpers: """ data = { "client_id": self._settings.CLIENT_ID if self._settings.CLIENT_ID is not None else "", + "client_secret": self._settings.CLIENT_SECRET if self._settings.CLIENT_SECRET is not None else "", "redirect_uri": self._settings.CALLBACK_URL if self._settings.CALLBACK_URL is not None else "", "grant_type": "authorization_code", "code": authorization_code, @@ -64,6 +65,7 @@ class AuthorizationHelpers: Logger.log("d", "Refreshing the access token for [%s]", self._settings.OAUTH_SERVER_URL) data = { "client_id": self._settings.CLIENT_ID if self._settings.CLIENT_ID is not None else "", + "client_secret": self._settings.CLIENT_SECRET if self._settings.CLIENT_SECRET is not None else "", "redirect_uri": self._settings.CALLBACK_URL if self._settings.CALLBACK_URL is not None else "", "grant_type": "refresh_token", "refresh_token": refresh_token, diff --git a/cura/OAuth2/Models.py b/cura/OAuth2/Models.py index 4c84872a09..3427f1cc3d 100644 --- a/cura/OAuth2/Models.py +++ b/cura/OAuth2/Models.py @@ -16,6 +16,7 @@ class OAuth2Settings(BaseModel): CALLBACK_PORT = None # type: Optional[int] OAUTH_SERVER_URL = None # type: Optional[str] CLIENT_ID = None # type: Optional[str] + CLIENT_SECRET = None # type: Optional[str] CLIENT_SCOPES = None # type: Optional[str] CALLBACK_URL = None # type: Optional[str] AUTH_DATA_PREFERENCE_KEY = "" # type: str From aac9af577bf6895665eb34dcae4e7fc5967caeff Mon Sep 17 00:00:00 2001 From: Erwan MATHIEU Date: Mon, 4 Dec 2023 20:09:36 +0100 Subject: [PATCH 2/4] Fix AuthorizationService signals --- cura/OAuth2/AuthorizationService.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cura/OAuth2/AuthorizationService.py b/cura/OAuth2/AuthorizationService.py index 62bf31982a..cac9c0df38 100644 --- a/cura/OAuth2/AuthorizationService.py +++ b/cura/OAuth2/AuthorizationService.py @@ -31,15 +31,15 @@ class AuthorizationService: account information. """ - # Emit signal when authentication is completed. - onAuthStateChanged = Signal() - - # Emit signal when authentication failed. - onAuthenticationError = Signal() - - accessTokenChanged = Signal() - def __init__(self, settings: "OAuth2Settings", preferences: Optional["Preferences"] = None) -> None: + # Emit signal when authentication is completed. + self.onAuthStateChanged = Signal() + + # Emit signal when authentication failed. + self.onAuthenticationError = Signal() + + self.accessTokenChanged = Signal() + self._settings = settings self._auth_helpers = AuthorizationHelpers(settings) self._auth_url = "{}/authorize".format(self._settings.OAUTH_SERVER_URL) From b794989468653d04c3e0e24044cb8ea61ce29dff Mon Sep 17 00:00:00 2001 From: Erwan MATHIEU Date: Mon, 4 Dec 2023 20:58:11 +0100 Subject: [PATCH 3/4] Allow for OAuth service to stop after token has been retrieved --- cura/OAuth2/AuthorizationService.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cura/OAuth2/AuthorizationService.py b/cura/OAuth2/AuthorizationService.py index cac9c0df38..06478e911b 100644 --- a/cura/OAuth2/AuthorizationService.py +++ b/cura/OAuth2/AuthorizationService.py @@ -31,7 +31,10 @@ class AuthorizationService: account information. """ - def __init__(self, settings: "OAuth2Settings", preferences: Optional["Preferences"] = None) -> None: + def __init__(self, + settings: "OAuth2Settings", + preferences: Optional["Preferences"] = None, + get_user_profile: bool = True) -> None: # Emit signal when authentication is completed. self.onAuthStateChanged = Signal() @@ -45,6 +48,7 @@ class AuthorizationService: self._auth_url = "{}/authorize".format(self._settings.OAUTH_SERVER_URL) self._auth_data: Optional[AuthenticationResponse] = None self._user_profile: Optional["UserProfile"] = None + self._get_user_profile: bool = get_user_profile self._preferences = preferences self._server = LocalAuthorizationServer(self._auth_helpers, self._onAuthStateChanged, daemon=True) self._currently_refreshing_token = False # Whether we are currently in the process of refreshing auth. Don't make new requests while busy. @@ -294,7 +298,8 @@ class AuthorizationService: self._auth_data = auth_data self._currently_refreshing_token = False if auth_data: - self.getUserProfile() + if self._get_user_profile: + self.getUserProfile() self._preferences.setValue(self._settings.AUTH_DATA_PREFERENCE_KEY, json.dumps(auth_data.dump())) else: Logger.log("d", "Clearing the user profile") From ff478559b0f169a0606fbb2e34429dc929130dcb Mon Sep 17 00:00:00 2001 From: Erwan MATHIEU Date: Wed, 17 Jan 2024 19:50:46 +0100 Subject: [PATCH 4/4] Change bool setting to more flexible callback function --- cura/OAuth2/AuthorizationService.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/cura/OAuth2/AuthorizationService.py b/cura/OAuth2/AuthorizationService.py index 06478e911b..29c426e46f 100644 --- a/cura/OAuth2/AuthorizationService.py +++ b/cura/OAuth2/AuthorizationService.py @@ -34,7 +34,7 @@ class AuthorizationService: def __init__(self, settings: "OAuth2Settings", preferences: Optional["Preferences"] = None, - get_user_profile: bool = True) -> None: + callback_auth_data_retrieved: Callable[[], None] = None) -> None: # Emit signal when authentication is completed. self.onAuthStateChanged = Signal() @@ -48,7 +48,7 @@ class AuthorizationService: self._auth_url = "{}/authorize".format(self._settings.OAUTH_SERVER_URL) self._auth_data: Optional[AuthenticationResponse] = None self._user_profile: Optional["UserProfile"] = None - self._get_user_profile: bool = get_user_profile + self._callback_auth_data_retrieved = self.getUserProfile if callback_auth_data_retrieved is None else callback_auth_data_retrieved self._preferences = preferences self._server = LocalAuthorizationServer(self._auth_helpers, self._onAuthStateChanged, daemon=True) self._currently_refreshing_token = False # Whether we are currently in the process of refreshing auth. Don't make new requests while busy. @@ -298,8 +298,7 @@ class AuthorizationService: self._auth_data = auth_data self._currently_refreshing_token = False if auth_data: - if self._get_user_profile: - self.getUserProfile() + self._callback_auth_data_retrieved() self._preferences.setValue(self._settings.AUTH_DATA_PREFERENCE_KEY, json.dumps(auth_data.dump())) else: Logger.log("d", "Clearing the user profile")