more cleanup

This commit is contained in:
Jonathan Wren 2022-09-24 15:20:44 -07:00
parent 454be3326e
commit a9c78272ad
7 changed files with 27 additions and 20 deletions

View file

@ -81,7 +81,7 @@ class Journal:
def _get_encryption_method(self):
encryption_method = determine_encryption_method(self.config["encrypt"])
self.encryption_method = encryption_method(self.config)
self.encryption_method = encryption_method(self.name, self.config)
def _decrypt(self, text):
if self.encryption_method is None:

View file

@ -5,7 +5,8 @@ from abc import abstractmethod
class BaseEncryption(ABC):
def __init__(self, config):
def __init__(self, journal_name, config):
self._journal_name = journal_name
self._config = config
@abstractmethod

View file

@ -24,8 +24,10 @@ class BasePasswordEncryption(BaseEncryption):
self._password = None
self._encoding = "utf-8"
# Check keyring first to be ready for decryption
self._password = get_keyring_password(self._config["journal"])
# Check keyring first for password.
# That way we'll have it.
if keyring_pw := get_keyring_password(self._journal_name):
self.password = keyring_pw
@property
def password(self):
@ -37,7 +39,7 @@ class BasePasswordEncryption(BaseEncryption):
def encrypt(self, text):
if self.password is None:
self.password = create_password(self._config["journal"])
self.password = create_password(self._journal_name)
return self._encrypt(text)
def decrypt(self, text):

View file

@ -15,8 +15,8 @@ class Jrnlv1Encryption(BasePasswordEncryption):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
# def _encrypt(self, text: str) -> bytes:
# raise NotImplementedError
def _encrypt(self, text: str) -> bytes:
raise NotImplementedError
def _decrypt(self, text: bytes) -> str | None:
iv, cipher = text[:16], text[16:]

View file

@ -1,18 +1,22 @@
# Copyright © 2012-2022 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html
from jrnl.encryption.NoEncryption import NoEncryption
from importlib import import_module
ENCRYPTION_METHODS = {
# config: classname (as string)
"default": "NoEncryption",
False: "NoEncryption",
True: "Jrnlv2Encryption",
"jrnlv1": "Jrnlv1Encryption",
"jrnlv2": "Jrnlv2Encryption",
}
def determine_encryption_method(config):
encryption_method = NoEncryption
if config is True or config == "jrnlv2":
# Default encryption method
from jrnl.encryption.Jrnlv2Encryption import Jrnlv2Encryption
key = config
if isinstance(config, str):
key = config.lower()
encryption_method = Jrnlv2Encryption
elif config == "jrnlv1":
from jrnl.encryption.Jrnlv1Encryption import Jrnlv1Encryption
my_class = ENCRYPTION_METHODS.get(key, "default")
encryption_method = Jrnlv1Encryption
return encryption_method
return getattr(import_module(f"jrnl.encryption.{my_class}"), my_class)

View file

@ -17,7 +17,7 @@ def get_keyring_password(journal_name: str = "default") -> str | None:
return None
def set_keyring_password(password, journal_name: str = "default"):
def set_keyring_password(password: str, journal_name: str = "default"):
try:
return keyring.set_password("jrnl", journal_name, password)
except keyring.errors.KeyringError as e:

View file

@ -37,7 +37,7 @@ def create_password(journal_name: str) -> str:
if yesno(Message(MsgText.PasswordStoreInKeychain), default=True):
from jrnl.keyring import set_keyring_password
set_keyring_password(journal_name, pw)
set_keyring_password(pw, journal_name)
return pw