mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-13 09:58:31 +02:00
more cleanup
This commit is contained in:
parent
454be3326e
commit
a9c78272ad
7 changed files with 27 additions and 20 deletions
|
@ -81,7 +81,7 @@ class Journal:
|
||||||
|
|
||||||
def _get_encryption_method(self):
|
def _get_encryption_method(self):
|
||||||
encryption_method = determine_encryption_method(self.config["encrypt"])
|
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):
|
def _decrypt(self, text):
|
||||||
if self.encryption_method is None:
|
if self.encryption_method is None:
|
||||||
|
|
|
@ -5,7 +5,8 @@ from abc import abstractmethod
|
||||||
|
|
||||||
|
|
||||||
class BaseEncryption(ABC):
|
class BaseEncryption(ABC):
|
||||||
def __init__(self, config):
|
def __init__(self, journal_name, config):
|
||||||
|
self._journal_name = journal_name
|
||||||
self._config = config
|
self._config = config
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
|
|
|
@ -24,8 +24,10 @@ class BasePasswordEncryption(BaseEncryption):
|
||||||
self._password = None
|
self._password = None
|
||||||
self._encoding = "utf-8"
|
self._encoding = "utf-8"
|
||||||
|
|
||||||
# Check keyring first to be ready for decryption
|
# Check keyring first for password.
|
||||||
self._password = get_keyring_password(self._config["journal"])
|
# That way we'll have it.
|
||||||
|
if keyring_pw := get_keyring_password(self._journal_name):
|
||||||
|
self.password = keyring_pw
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def password(self):
|
def password(self):
|
||||||
|
@ -37,7 +39,7 @@ class BasePasswordEncryption(BaseEncryption):
|
||||||
|
|
||||||
def encrypt(self, text):
|
def encrypt(self, text):
|
||||||
if self.password is None:
|
if self.password is None:
|
||||||
self.password = create_password(self._config["journal"])
|
self.password = create_password(self._journal_name)
|
||||||
return self._encrypt(text)
|
return self._encrypt(text)
|
||||||
|
|
||||||
def decrypt(self, text):
|
def decrypt(self, text):
|
||||||
|
|
|
@ -15,8 +15,8 @@ class Jrnlv1Encryption(BasePasswordEncryption):
|
||||||
def __init__(self, *args, **kwargs) -> None:
|
def __init__(self, *args, **kwargs) -> None:
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
# def _encrypt(self, text: str) -> bytes:
|
def _encrypt(self, text: str) -> bytes:
|
||||||
# raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def _decrypt(self, text: bytes) -> str | None:
|
def _decrypt(self, text: bytes) -> str | None:
|
||||||
iv, cipher = text[:16], text[16:]
|
iv, cipher = text[:16], text[16:]
|
||||||
|
|
|
@ -1,18 +1,22 @@
|
||||||
# Copyright © 2012-2022 jrnl contributors
|
# Copyright © 2012-2022 jrnl contributors
|
||||||
# License: https://www.gnu.org/licenses/gpl-3.0.html
|
# 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):
|
def determine_encryption_method(config):
|
||||||
encryption_method = NoEncryption
|
key = config
|
||||||
if config is True or config == "jrnlv2":
|
if isinstance(config, str):
|
||||||
# Default encryption method
|
key = config.lower()
|
||||||
from jrnl.encryption.Jrnlv2Encryption import Jrnlv2Encryption
|
|
||||||
|
|
||||||
encryption_method = Jrnlv2Encryption
|
my_class = ENCRYPTION_METHODS.get(key, "default")
|
||||||
elif config == "jrnlv1":
|
|
||||||
from jrnl.encryption.Jrnlv1Encryption import Jrnlv1Encryption
|
|
||||||
|
|
||||||
encryption_method = Jrnlv1Encryption
|
return getattr(import_module(f"jrnl.encryption.{my_class}"), my_class)
|
||||||
|
|
||||||
return encryption_method
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ def get_keyring_password(journal_name: str = "default") -> str | None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def set_keyring_password(password, journal_name: str = "default"):
|
def set_keyring_password(password: str, journal_name: str = "default"):
|
||||||
try:
|
try:
|
||||||
return keyring.set_password("jrnl", journal_name, password)
|
return keyring.set_password("jrnl", journal_name, password)
|
||||||
except keyring.errors.KeyringError as e:
|
except keyring.errors.KeyringError as e:
|
||||||
|
|
|
@ -37,7 +37,7 @@ def create_password(journal_name: str) -> str:
|
||||||
if yesno(Message(MsgText.PasswordStoreInKeychain), default=True):
|
if yesno(Message(MsgText.PasswordStoreInKeychain), default=True):
|
||||||
from jrnl.keyring import set_keyring_password
|
from jrnl.keyring import set_keyring_password
|
||||||
|
|
||||||
set_keyring_password(journal_name, pw)
|
set_keyring_password(pw, journal_name)
|
||||||
|
|
||||||
return pw
|
return pw
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue