def create_file(file_path, password): """ Create a new file, static method. Uses the FileStorage Helper to coordinate the creation of the file with its addition to the Storage File. :param file_path: Path of the Password File to create. :type file_path: str :param password: Password to assign to the Password File to create. :type password: str :return: False if the operation failed, the FileHandler object created otherwise. :rtype: Union(bool, FileHandler) """ fsh = FileStorageHelper() fsh.load_db() key, salt = EncryptionManager().generate_new_key(password=password) pass_file = PasswordFile(file_path=file_path, salt=salt) if not pass_file.is_complete(): return False if not fsh.add_file(pass_file): return False # initialize an empty file file_handler = FileHandler(file_path=file_path, password=password, salt=salt) file_handler.write_in_file(json.dumps([])) fsh.save_db() return file_handler
def delete_file(ctx, master_password): """Endpoint to delete a new file.""" try: path = ctx.obj.get('path') salt = FileStorageHelper.get_salt_from_path(path) FileHandler(file_path=path, password=master_password, salt=salt).delete() ok_msg("File successfully deleted.") except FileNotFoundError: err_msg("No such file '{}'".format(ctx.obj.get('path'))) exit() except InvalidToken: err_msg("Wrong password.") exit()
def delete(self): """ Delete a file. :return: None """ fsh = FileStorageHelper() fsh.load_db() self.get_all_entries() # check the master password self.delete_all_entries() # make it empty fsh.delete_file( PasswordFile(file_path=self.file_path, salt=self.salt) # removes the entry from the storage file ) fsh.save_db() # then save the db again. os.remove(self.file_path) # removes file from the OS.
def delete(ctx, name): """Get delete a password in an encrypted file.""" try: path = ctx.obj.get("path") salt = FileStorageHelper.get_salt_from_path(path) fh = FileHandler(file_path=path, password=ctx.obj.get("master_password"), salt=salt) fh.delete_entry(entry_name=name) ok_msg("Deletion successful") exit() except InvalidToken: err_msg("Master password incorrect. Please retry.") exit() except Exception as e: err_msg(e.__str__()) exit()
def add(ctx, name, username, password_): """Get add a new password in an encrypted file.""" try: path = ctx.obj.get("path") salt = FileStorageHelper.get_salt_from_path(path) fh = FileHandler(file_path=path, password=ctx.obj.get("master_password"), salt=salt) fh.add_entry( PasswordEntry(username=username, password=password_, name=name)) ok_msg("Addition of a new password successful") except IndexError as e: err_msg(e) exit() except Exception as e: err_msg(e) exit()
def get_all(ctx): """Get all password in an encrypted file.""" path = ctx.obj.get("path") salt = FileStorageHelper.get_salt_from_path(path) try: fh = FileHandler(file_path=path, password=ctx.obj.get("master_password"), salt=salt) entries = fh.get_all_entries() [ pprint.pprint(json.loads(pwd.to_json()), compact=False, indent=2) for pwd in entries ] except InvalidToken: err_msg("Wrong password.") except FileNotFoundError: err_msg("File not found") except IndexError: err_msg("Path not found in the storage file.")
def get(ctx, name, absolute): """Get one or several passwords in an encrypted file.""" try: path = ctx.obj.get("path") salt = FileStorageHelper.get_salt_from_path(path) fh = FileHandler(file_path=path, password=ctx.obj.get("master_password"), salt=salt) if absolute: entries = [fh.get_entry(name)] else: entries = fh.get_entry_starting_by(name) [ pprint.pprint(json.loads(pwd.to_json()), compact=False, indent=2) for pwd in entries ] except Exception as e: err_msg(e)
def list_files(): """Endpoint to list all encrypted files in the OS.""" fsh = FileStorageHelper() fsh.load_db() pprint.pprint(fsh.get_all_files(), indent=4)