recoveryfile

This commit is contained in:
2024-12-09 16:35:54 +01:00
parent 1900100b54
commit 5f08435816
8 changed files with 183 additions and 349 deletions

View File

@@ -24,7 +24,8 @@ class ConfigLoader(QObject):
dbConnectionError = Signal(str, bool)
adminUserError = Signal(str, bool)
usernameNotAvailable = Signal()
configurationReady = Signal()
configurationReady = Signal(bool)
backupEncryptionKey = Signal()
def __init__(self):
super().__init__()
@@ -36,6 +37,51 @@ class ConfigLoader(QObject):
else:
config_dir.mkdir(0o750, True, True)
@Slot(str, str)
def saveRecoveryKey(self, recovery_file, recovery_password):
rp = self.__setRecoveryPassword(recovery_password)
rf = rp[1] + self.__encrypt_key + rp[0]
rf = Vermasseln().oscarVermasseln(rf)
rec_file = urlparse(recovery_file)
rec_file = rec_file.path
if os.name == "nt":
rec_file = rec_file [1:]
try:
with open(rec_file, "w") as f:
f.write(rf)
self.configurationReady.emit(True)
except Exception as e:
print(str(e))
@Slot(str, str)
def getRecoveryKey(self, recovery_file, recovery_password):
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)
ek = rf[128:]
ek = ek[:-32]
password = rf[:128]
salt = rf[-32:]
ok = self.__checkRecoveryPassword(recovery_password, password, salt)
if ok:
self.configurationReady.emit(True)
else:
self.configurationReady.emit(False)
except Exception as e:
print(str(e))
def __checkRecoveryPassword(self, recovery_password, password, salt):
rp = self.__setRecoveryPassword(recovery_password, salt)
return rp[1] == password
@Slot(str, str)
def importConfig(self, confile, password):
@@ -52,6 +98,8 @@ class ConfigLoader(QObject):
if not admin:
self.usernameNotAvailable.emit()
else:
self.backupEncryptionKey.emit()
return admin
@Slot(dict, result= bool)
@@ -66,7 +114,7 @@ class ConfigLoader(QObject):
self.__saveConfig()
conf = self.__checkAdminUser()
if conf:
self.configurationReady.emit()
self.configurationReady.emit(True)
@@ -75,6 +123,7 @@ class ConfigLoader(QObject):
with open (self.config_dir + '/pyqcrm.toml', 'r') as f:
config = f.read()
self.__config = toml.loads(Vermasseln().entschluesseln(config))
self.configurationReady.emit(True)
except FileNotFoundError:
print("Konnte die Konfiguration nicht laden.")
except TypeError:
@@ -87,8 +136,8 @@ class ConfigLoader(QObject):
return self.__config
def __initializeConfig(self):
encrypt_key = b64encode(get_random_bytes(32)).decode("utf-8")
conf = f"[pyqcrm]\nVERSION = \"{self.__version}\"\nENCRYPTION_KEY = \"{encrypt_key}\"\n\n"
self.__encrypt_key = b64encode(get_random_bytes(32)).decode("utf-8")
conf = f"[pyqcrm]\nVERSION = \"{self.__version}\"\nENCRYPTION_KEY = \"{self.__encrypt_key}\"\n\n"
return conf
@@ -114,12 +163,13 @@ class ConfigLoader(QObject):
self.adminUserError.emit("Admin vorhanden", True)
return True
@Slot(str)
def setEncyrptKey(self, key):
self.__config['pyqcrm']['ENCRYPTION_KEY'] = key
self.__saveConfig()
def __setRecoveryPassword(self, key, salt = None):
key = Vermasseln.userPasswordHash(key, salt)
return key.split("$")
def __saveConfig(self):
try:
with open (self.config_dir + '/pyqcrm.toml', 'w') as f:
config = Vermasseln().oscarVermasseln(toml.dumps(self.__config))