mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-13 01:48:31 +02:00
keep logic for password attemps inside the class that uses it
This commit is contained in:
parent
196cb71beb
commit
d066a3a8de
2 changed files with 16 additions and 12 deletions
|
@ -1,7 +1,11 @@
|
||||||
# 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.BaseEncryption import BaseEncryption
|
from jrnl.encryption.BaseEncryption import BaseEncryption
|
||||||
|
from jrnl.exception import JrnlException
|
||||||
from jrnl.keyring import get_keyring_password
|
from jrnl.keyring import get_keyring_password
|
||||||
|
from jrnl.messages import Message
|
||||||
|
from jrnl.messages import MsgStyle
|
||||||
|
from jrnl.messages import MsgText
|
||||||
from jrnl.prompt import create_password
|
from jrnl.prompt import create_password
|
||||||
from jrnl.prompt import prompt_password
|
from jrnl.prompt import prompt_password
|
||||||
|
|
||||||
|
@ -34,12 +38,18 @@ class BasePasswordEncryption(BaseEncryption):
|
||||||
def decrypt(self, text: bytes) -> str:
|
def decrypt(self, text: bytes) -> str:
|
||||||
if not self.password:
|
if not self.password:
|
||||||
self._prompt_password()
|
self._prompt_password()
|
||||||
|
|
||||||
while (result := self._decrypt(text)) is None:
|
while (result := self._decrypt(text)) is None:
|
||||||
self._prompt_password()
|
self._prompt_password()
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def _prompt_password(self) -> None:
|
def _prompt_password(self) -> None:
|
||||||
self._attempts, self.password = prompt_password(
|
if self._attempts >= self._max_attempts:
|
||||||
self._attempts, self._max_attempts
|
raise JrnlException(
|
||||||
)
|
Message(MsgText.PasswordMaxTriesExceeded, MsgStyle.ERROR)
|
||||||
|
)
|
||||||
|
|
||||||
|
first_try = self._attempts == 0
|
||||||
|
self.password = prompt_password(first_try=first_try)
|
||||||
|
self._attempts += 1
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
# 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.exception import JrnlException
|
|
||||||
from jrnl.messages import Message
|
from jrnl.messages import Message
|
||||||
from jrnl.messages import MsgStyle
|
from jrnl.messages import MsgStyle
|
||||||
from jrnl.messages import MsgText
|
from jrnl.messages import MsgText
|
||||||
|
@ -43,22 +42,17 @@ def create_password(journal_name: str) -> str:
|
||||||
return pw
|
return pw
|
||||||
|
|
||||||
|
|
||||||
def prompt_password(attempts: int, max_attempts: int) -> tuple[int, str]:
|
def prompt_password(first_try: bool = True) -> str:
|
||||||
if attempts >= max_attempts:
|
if not first_try:
|
||||||
raise JrnlException(Message(MsgText.PasswordMaxTriesExceeded, MsgStyle.ERROR))
|
|
||||||
|
|
||||||
if attempts > 0:
|
|
||||||
print_msg(Message(MsgText.WrongPasswordTryAgain, MsgStyle.WARNING))
|
print_msg(Message(MsgText.WrongPasswordTryAgain, MsgStyle.WARNING))
|
||||||
|
|
||||||
attempts += 1
|
|
||||||
return (
|
return (
|
||||||
attempts,
|
|
||||||
print_msg(
|
print_msg(
|
||||||
Message(MsgText.Password, MsgStyle.PROMPT),
|
Message(MsgText.Password, MsgStyle.PROMPT),
|
||||||
get_input=True,
|
get_input=True,
|
||||||
hide_input=True,
|
hide_input=True,
|
||||||
)
|
)
|
||||||
or "",
|
or ""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue