From 080b8d66e4e6461c348be48a9946b2f7a5b8d53c Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sun, 11 Dec 2022 14:55:17 +0100 Subject: [PATCH] Implemented automatic user identification --- Readme.md | 6 +++--- scripts/classes/Cleanup.py | 2 +- scripts/classes/Decryption.py | 16 ++++++++++++++++ scripts/classes/Paths.py | 7 +++++-- scripts/main.py | 15 +++++++++++---- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/Readme.md b/Readme.md index 3330307..f96dc10 100644 --- a/Readme.md +++ b/Readme.md @@ -42,8 +42,8 @@ python scripts/main.py --mode cleanup --file-types encrypted ## decrypt -### decrypt automatic (todo) -To decrypt the data for a defined user type in: +### decrypt automatic +To decrypt the data type in: ```bash python scripts/main.py --mode decrypt @@ -64,7 +64,7 @@ python scripts/main.py --mode decrypt --user "<>" python scripts/main.py --amount 3 --quota 50 --mode encrypt --add-user-information --master-password "{{master_password}}" ``` -### encrypt master-password fuile +### encrypt master-password file ## todo - add data-input attribut diff --git a/scripts/classes/Cleanup.py b/scripts/classes/Cleanup.py index 5eb5829..ed98aed 100644 --- a/scripts/classes/Cleanup.py +++ b/scripts/classes/Cleanup.py @@ -8,7 +8,7 @@ class Cleanup(): def getAllFilePaths(self,file_type): all_file_paths = [ self.paths.getGroupFilesFolderPath(file_type), - self.paths.getUserFilesFolderPath(file_type), + self.paths.getUserFilesPath(file_type), self.paths.getAccumulatedFilePath(file_type) ] if file_type == Paths.TYPE_DECRYPTED: diff --git a/scripts/classes/Decryption.py b/scripts/classes/Decryption.py index 9ac85db..02b5d71 100644 --- a/scripts/classes/Decryption.py +++ b/scripts/classes/Decryption.py @@ -1,6 +1,10 @@ import json +import os from pathlib import Path +class AutomaticIdentificationImpossibleException(Exception): + pass + class Decryption(): def __init__(self,cli,paths): @@ -9,6 +13,18 @@ class Decryption(): self.cli = cli self.paths = paths + def identifyUser(self): + file_type = self.paths.TYPE_ENCRYPTED + file_names = next(os.walk(self.paths.getUserFilesPath(file_type)), (None, None, []))[2] + users = [] + user_file_suffix = self.paths.getUserFileSuffix(file_type) + for file in file_names: + if user_file_suffix in file: + users.append(file.replace(user_file_suffix, '')) + if len(users) < 2: + return users[0] + raise AutomaticIdentificationImpossibleException() + def initializeUser(self,user_id): self.user_id=str(user_id) self.user_file_decrypted_path = self.paths.getUserFilePath(self.user_id,self.paths.TYPE_DECRYPTED) diff --git a/scripts/classes/Paths.py b/scripts/classes/Paths.py index 68b3969..1ac5063 100644 --- a/scripts/classes/Paths.py +++ b/scripts/classes/Paths.py @@ -16,7 +16,7 @@ class Paths(): def getGroupFilesFolderPath(self,folder_type): return self.getDataFolderPath(folder_type) + "group_files/" - def getUserFilesFolderPath(self,folder_type): + def getUserFilesPath(self,folder_type): return self.getDataFolderPath(folder_type) + "user_files/" def getEncryptedMainDataFile(self): @@ -29,9 +29,12 @@ class Paths(): if file_type == Paths.TYPE_ENCRYPTED: return '.gpg' return '' + + def getUserFileSuffix(self,file_type): + return '.json' + self.getFileExtension(file_type) def getUserFilePath(self,user_id,file_type): - return self.getUserFilesFolderPath(file_type)+user_id+'.json' + self.getFileExtension(file_type); + return self.getUserFilesPath(file_type) + user_id + self.getUserFileSuffix(file_type); def getGroupFilePath(self,group_id,file_type): return self.getGroupFilesFolderPath(file_type) + str(group_id) + '.txt' + self.getFileExtension(file_type); diff --git a/scripts/main.py b/scripts/main.py index d01c5a9..f59b347 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -1,7 +1,7 @@ import argparse from classes.Encryption import Encryption from classes.Cleanup import Cleanup -from classes.Decryption import Decryption +from classes.Decryption import Decryption, AutomaticIdentificationImpossibleException from getpass import getpass import traceback from classes.Cli import Cli @@ -61,9 +61,16 @@ try: if args.mode == 'decrypt': decrypt = Decryption(cli,paths) if args.master_password is None: - if args.user is None: - print("Type in the user id:") - decrypt.initializeUser(input()) + if args.user is None: + try: + print("Attempt to identify user.") + user_id = decrypt.identifyUser() + print("The user id is: " + user_id) + except: + print("A automatic user id identification wasn't possible.") + print("Type in the user id:") + user_id = input() + decrypt.initializeUser(user_id) else: decrypt.initializeUser(args.user) if args.user_password is None: