Skip to content

Commit

Permalink
Add pasword confirmation dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
pspeter committed Oct 31, 2019
1 parent 34f8f85 commit dadbcbf
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 12 deletions.
17 changes: 13 additions & 4 deletions features/encryption.feature
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

Scenario: Encrypting a journal
Given we use the config "basic.yaml"
When we run "jrnl --encrypt" and enter "swordfish" and "n"
When we run "jrnl --encrypt" and enter
"""
swordfish
swordfish
n
"""
Then we should see the message "Journal encrypted"
and the config for journal "default" should have "encrypt" set to "bool:True"
When we run "jrnl -n 1" and enter "swordfish"
Expand All @@ -23,9 +28,13 @@

Scenario: Storing a password in Keychain
Given we use the config "multiple.yaml"
When we run "jrnl simple --encrypt" and enter "sabertooth" and "y"
When we run "jrnl simple --encrypt" and enter
"""
sabertooth
sabertooth
y
"""
When we set the keychain password of "simple" to "sabertooth"
Then the config for journal "simple" should have "encrypt" set to "bool:True"
When we run "jrnl simple -n 1"
Then we should not see the message "Password"
and the output should contain "2013-06-10 15:40 Life is good"
Then the output should contain "2013-06-10 15:40 Life is good"
7 changes: 6 additions & 1 deletion features/multiple_journals.feature
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@ Feature: Multiple journals

Scenario: Don't crash if no file exists for a configured encrypted journal
Given we use the config "multiple.yaml"
When we run "jrnl new_encrypted Adding first entry" and enter "these three eyes" and "y"
When we run "jrnl new_encrypted Adding first entry" and enter
"""
these three eyes
these three eyes
n
"""
Then we should see the message "Journal 'new_encrypted' created"
7 changes: 4 additions & 3 deletions features/steps/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ def set_password(self, servicename, username, password):
def get_password(self, servicename, username):
return self.keys[servicename].get(username)

def delete_password(self, servicename, username, password):
def delete_password(self, servicename, username):
self.keys[servicename][username] = None


# set the keyring for keyring lib
keyring.set_keyring(TestKeyring())

Expand Down Expand Up @@ -86,11 +87,11 @@ def prompt_return(prompt=""):
@when('we run "{command}" and enter "{inputs1}" and "{inputs2}"')
def run_with_input(context, command, inputs1="", inputs2=""):
# create an iterator through all inputs. These inputs will be fed one by one
# to the mocked calls for 'input()', 'util.getpass()' and 'sys.stdin.read()'
# to the mocked calls for 'input()', 'getpass.getpass()' and 'sys.stdin.read()'
text = iter((inputs1, inputs2)) if inputs1 else iter(context.text.split("\n"))
args = ushlex(command)[1:]
with patch("builtins.input", side_effect=_mock_input(text)) as mock_input:
with patch("jrnl.util.getpass", side_effect=_mock_getpass(text)) as mock_getpass:
with patch("jrnl.util.gp.getpass", side_effect=_mock_getpass(text)) as mock_getpass:
with patch("sys.stdin.read", side_effect=text) as mock_read:
try:
cli.run(args or [])
Expand Down
2 changes: 1 addition & 1 deletion jrnl/EncryptedJournal.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def open(self, filename=None):
filename = filename or self.config['journal']

if not os.path.exists(filename):
password = util.getpass("Enter password for new journal: ")
password = util.create_password()
if password:
if util.yesno("Do you want to store the password in your keychain?", default=True):
util.set_keychain(self.name, password)
Expand Down
2 changes: 1 addition & 1 deletion jrnl/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def encrypt(journal, filename=None):
""" Encrypt into new file. If filename is not set, we encrypt the journal file itself. """
from . import EncryptedJournal

journal.config['password'] = util.getpass("Enter new password: ")
journal.config['password'] = util.create_password()
journal.config['encrypt'] = True

new_journal = EncryptedJournal.EncryptedJournal(None, **journal.config)
Expand Down
10 changes: 8 additions & 2 deletions jrnl/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,18 @@ class UserAbort(Exception):
pass


getpass = gp.getpass
def create_password():
while True:
pw = gp.getpass("Enter password for new journal: ")
if pw == gp.getpass("Enter password again: "):
return pw

gp.getpass("Passwords did not match, please try again")


def get_password(validator, keychain=None, max_attempts=3):
pwd_from_keychain = keychain and get_keychain(keychain)
password = pwd_from_keychain or getpass()
password = pwd_from_keychain or gp.getpass()
result = validator(password)
# Password is bad:
if result is None and pwd_from_keychain:
Expand Down

0 comments on commit dadbcbf

Please sign in to comment.