From 73542e808970ae7500cc6b04895ac4a7c30cacf7f960c5c48932e987c107955d Mon Sep 17 00:00:00 2001 From: Daniel Stoppek Date: Thu, 27 Feb 2025 14:45:27 +0100 Subject: [PATCH] Backup Config --- Gui/BackupSettings.qml | 13 +++++--- Gui/main.qml | 2 +- lib/ConfigLoader.py | 75 ++++++++++++++++++++++++++++-------------- lib/Vermasseln.py | 1 - 4 files changed, 61 insertions(+), 30 deletions(-) diff --git a/Gui/BackupSettings.qml b/Gui/BackupSettings.qml index 475fbb4..31a6076 100644 --- a/Gui/BackupSettings.qml +++ b/Gui/BackupSettings.qml @@ -3,6 +3,8 @@ import QtQuick.Controls import QtQuick.Layouts import QtQuick.Dialogs import QtCore +import "../js/qmldict.js" as JsLib + GridLayout @@ -57,7 +59,8 @@ GridLayout { if (configPwd.text === repeatConfigPwd.text) - { console.log("true") + { + saveConfigFile.open() } else @@ -67,12 +70,12 @@ GridLayout configPwd.placeholderTextColor = "red" repeatConfigPwd.placeholderText = qsTr("") repeatConfigPwd.text = "" - console.log("false") } } onRejected: console.log("Cancel clicked") GridLayout { + id: gridPw columns: 2 Label { @@ -80,6 +83,7 @@ GridLayout } TextField { + id: configPwd placeholderText: qsTr("Sicherungspasswort festlegen") } @@ -89,6 +93,7 @@ GridLayout } TextField { + property string name: "password" id: repeatConfigPwd placeholderText: qsTr("Sicherungspasswort wiederholen") } @@ -103,8 +108,8 @@ GridLayout currentFolder: StandardPaths.standardLocations(StandardPaths.DocumentsLocation)[0] onAccepted: { - var test = config.backupConfig(saveConfigFile.currentFile) - console.log() + var pw = JsLib.parseForm(gridPw) + config.backupConfig(saveConfigFile.currentFile, pw["password"]) } } } diff --git a/Gui/main.qml b/Gui/main.qml index cb948c4..3d43405 100644 --- a/Gui/main.qml +++ b/Gui/main.qml @@ -93,7 +93,7 @@ ApplicationWindow title: qsTr("PYQCRM Einstellungen") currentFolder: StandardPaths.standardLocations(StandardPaths.DocumentsLocation)[0] modality: "ApplicationModal" - nameFilters: [qsTr("PYQCRM Einstellungen (*.pyqcrm)")] + nameFilters: [qsTr("PYQCRM Einstellungen (*.pyqrec)")] onAccepted: { exportFilePassword.open() diff --git a/lib/ConfigLoader.py b/lib/ConfigLoader.py index 8b2ee1f..b56ce66 100644 --- a/lib/ConfigLoader.py +++ b/lib/ConfigLoader.py @@ -110,6 +110,12 @@ class ConfigLoader(QObject): self.backupEncryptionKey.emit() return admin + """###################################################""" + """ """ + """ TODO: Rename method and rename self.__encrypt_key """ + """ """ + """###################################################""" + @Slot(str, str) def saveRecoveryKey(self, recovery_file, recovery_password): # print(f"In {__file__} file, saveRecoveryKey()") @@ -131,30 +137,39 @@ class ConfigLoader(QObject): @Slot(str, str) def getRecoveryKey(self, recovery_file, recovery_password): - # print(f"In {__file__} file, getRecoveryKey()") - local = False rec_file = urlparse(recovery_file) rec_file = rec_file.path if os.name == "nt": rec_file = rec_file [1:] try: - with open(rec_file, "r") as f: - rf = f.read() - rf = Vermasseln().entschluesseln(rf, local) - ek = rf[128:] - ek = ek[:-32] - password = rf[:128] - salt = rf[-32:] - ok = self.__checkRecoveryPassword(recovery_password, password, salt) - if ok: - self.__setEncryptionKey(ek) - self.configurationReady.emit() - else: - self.__invalidateEncryptionKey() - self.invalidEncryptionKey.emit() + ek = self.__parseImport(rec_file, recovery_password) + + if ek: + self.__setEncryptionKey(ek) + self.configurationReady.emit() + else: + self.__invalidateEncryptionKey() + self.invalidEncryptionKey.emit() except Exception as e: print(str(e)) + def __parseImport(self, rec_file, recovery_password): + local = False + with open(rec_file, "r") as f: + + rf = f.read() + rf = Vermasseln().entschluesseln(rf, local) + ek = rf[128:] + ek = ek[:-32] + password = rf[:128] + salt = rf[-32:] + ok = self.__checkRecoveryPassword(recovery_password, password, salt) + if ok: + return ek + else: + return None + + def __invalidateEncryptionKey(self): # print(f"In {__file__} file, __invalidateEncryptionKey()") self.__config['pyqcrm']['ENCRYPTION_KEY_VALID'] = 'No' @@ -173,13 +188,25 @@ class ConfigLoader(QObject): @Slot(str, str) # todo: non local encryption def importConfig(self, confile, password): - # print(f"In {__file__} file, importConfig()") confile = urlparse(confile) confile = confile.path - if os.name == "nt": confile = confile[1:] - shutil.copyfile(confile, self.config_dir+ '/pyqcrm.toml') + try: + ek = self.__parseImport(confile, password) + if ek: + self.__config = toml.loads(ek) + self.__saveConfig() + self.configurationReady.emit() + else: + self.invalidEncryptionKey.emit() + except Exception as e: + print(str(e)) + + + + + def __configLoad(self): # print(f"In {__file__} file, __configLoad()") @@ -213,8 +240,8 @@ class ConfigLoader(QObject): self.__config['pyqcrm']['ENCRYPTION_KEY'] = enc_key self.__saveConfig() - @Slot(str) - def backupConfig(self, pw): - print(pw) - new_config = self.getConfig() - print(new_config) + @Slot(str, str) + def backupConfig(self, filename, password): + self.__encrypt_key = toml.dumps(self.getConfig()) + self.saveRecoveryKey(filename, password) + diff --git a/lib/Vermasseln.py b/lib/Vermasseln.py index 72e3190..67570cb 100644 --- a/lib/Vermasseln.py +++ b/lib/Vermasseln.py @@ -53,7 +53,6 @@ class Vermasseln: hash_pw = (salt + password).encode("utf-8") h_obj = SHA3_512.new(hash_pw) password = salt + "$" + h_obj.hexdigest() - return password