From 971fb9d1041b9385591da8ef6f371c160b2eeb82 Mon Sep 17 00:00:00 2001 From: Remco Burema Date: Tue, 30 Sep 2025 11:37:36 +0200 Subject: [PATCH] Save authorization per-cluster instead of in 1 value. This would otherwise invalidate all authorization information when switching printers. part of CURA-12708 --- .../src/Network/ClusterApiClient.py | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/plugins/UM3NetworkPrinting/src/Network/ClusterApiClient.py b/plugins/UM3NetworkPrinting/src/Network/ClusterApiClient.py index bbeca0d5ae..a87f4d442b 100644 --- a/plugins/UM3NetworkPrinting/src/Network/ClusterApiClient.py +++ b/plugins/UM3NetworkPrinting/src/Network/ClusterApiClient.py @@ -70,14 +70,27 @@ class ClusterApiClient: self._auth_tries = 0 prefs = CuraApplication.getInstance().getPreferences() - prefs.addPreference("cluster_api/auth_id", None) - prefs.addPreference("cluster_api/auth_key", None) - prefs.addPreference("cluster_api/nonce_count", 1) - prefs.addPreference("cluster_api/nonce", None) - self._auth_id = prefs.getValue("cluster_api/auth_id") - self._auth_key = prefs.getValue("cluster_api/auth_key") - self._nonce_count = int(prefs.getValue("cluster_api/nonce_count")) - self._nonce = prefs.getValue("cluster_api/nonce") + prefs.addPreference("cluster_api/auth_ids", "{}") + prefs.addPreference("cluster_api/auth_keys", "{}") + prefs.addPreference("cluster_api/nonce_counts", "{}") + prefs.addPreference("cluster_api/nonces", "{}") + try: + self._auth_id = json.loads(prefs.getValue("cluster_api/auth_ids")).get(self._address, None) + self._auth_key = json.loads(prefs.getValue("cluster_api/auth_keys")).get(self._address, None) + self._nonce_count = int(json.loads(prefs.getValue("cluster_api/nonce_counts")).get(self._address, 1)) + self._nonce = json.loads(prefs.getValue("cluster_api/nonces")).get(self._address, None) + except (JSONDecodeError, TypeError, KeyError) as ex: + Logger.info(f"Get new cluster-API auth info ('{str(ex)}').") + self._auth_id = None + self._auth_key = None + self._nonce_count = 1 + self._nonce = None + + def _setLocalValueToPrefDict(self, name: str, value: Any) -> None: + prefs = CuraApplication.getInstance().getPreferences() + values_per_address = json.loads(prefs.getValue(name)) + values_per_address[self._address] = value + prefs.setValue(name, json.dumps(values_per_address)) def getSystem(self, on_finished: Callable) -> None: """Get printer system information. @@ -164,8 +177,7 @@ class ClusterApiClient: digest_str = self._makeAuthDigestHeaderPart(path, method=method) request.setRawHeader(b"Authorization", f"Digest {digest_str}".encode("utf-8")) self._nonce_count += 1 - prefs = CuraApplication.getInstance().getPreferences() - prefs.setValue("cluster_api/nonce_count", self._nonce_count) + self._setLocalValueToPrefDict("cluster_api/nonce_counts", self._nonce_count) CuraApplication.getInstance().savePreferences() elif not skip_auth: self._setupAuth() @@ -251,9 +263,8 @@ class ClusterApiClient: auth_info = json.loads(resp.data().decode()) self._auth_id = auth_info["id"] self._auth_key = auth_info["key"] - prefs = CuraApplication.getInstance().getPreferences() - prefs.setValue("cluster_api/auth_id", self._auth_id) - prefs.setValue("cluster_api/auth_key", self._auth_key) + self._setLocalValueToPrefDict("cluster_api/auth_ids", self._auth_id) + self._setLocalValueToPrefDict("cluster_api/auth_keys", self._auth_key) CuraApplication.getInstance().savePreferences() except Exception as ex: Logger.warning(f"Couldn't get temporary digest token: {str(ex)}") @@ -295,9 +306,8 @@ class ClusterApiClient: if nonce_match: self._nonce = nonce_match.group(1) self._nonce_count = 1 - prefs = CuraApplication.getInstance().getPreferences() - prefs.setValue("cluster_api/nonce_count", self._nonce_count) - prefs.setValue("cluster_api/nonce", self._nonce) + self._setLocalValueToPrefDict("cluster_api/nonce_counts", self._nonce_count) + self._setLocalValueToPrefDict("cluster_api/nonces", self._nonce) CuraApplication.getInstance().savePreferences() self._on_error(reply.errorString()) return