Skip to content

Commit

Permalink
Merge pull request #20 from psbleep/master
Browse files Browse the repository at this point in the history
Generate passwords.
  • Loading branch information
Kwpolska committed Aug 28, 2018
2 parents 74c9390 + f093e72 commit 6c3f3ab
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Chris Warrick <chris@chriswarrick.com>
Patrick Schneeweis <psbleep@protonmail.com>
88 changes: 85 additions & 3 deletions upass/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""

import os
import re
import sys
import upass
import urwid
Expand All @@ -25,13 +26,13 @@
d display
s display
c copy
g generate
r refresh
/ search
h help
q quit
upass requires pass installed and in $PATH: http://www.passwordstore.org/"""


# Case folding
if sys.version_info[0] == 2:
try:
Expand Down Expand Up @@ -110,9 +111,9 @@ def keypress(self, size, key):
newfocus = 0
elif key == 'end':
newfocus = maxindex
elif key == 'k' and self._app.mode != 'search':
elif key == 'k' and self._app.mode not in ('search', 'generate'):
newfocus = currentfocus - 1
elif key == 'j' and self._app.mode != 'search':
elif key == 'j' and self._app.mode not in ('search', 'generate'):
newfocus = currentfocus + 1
if newfocus is not None:
if newfocus < 0:
Expand Down Expand Up @@ -182,6 +183,7 @@ def __init__(self):
column_data = [
urwid.Button('DiSplay', self.display_selected),
urwid.Button('Copy', self.copy_selected),
urwid.Button('Generate', self.generate_password),
urwid.Button('Refresh', self.refresh_and_reload),
urwid.Button('/search', self.search),
urwid.Button('Help', self.help),
Expand All @@ -196,6 +198,7 @@ def __init__(self):
ini_commands = {
'display': self.display_selected,
'copy': self.copy_selected,
'generate': self.generate_password,
'refresh': self.refresh_and_reload,
'search': self.search,
'help': self.help,
Expand Down Expand Up @@ -274,6 +277,83 @@ def copy_selected(self, originator):
if pathg in self.passwords:
self.call_pass(originator, (path, True, False))

def generate_password(self, originator, args=None):
self.mode = 'generate'
if args:
path, length, symbols, force = args
else:
path, length, symbols, force = '', 16, True, False
if not path and self.current != '.':
path = self.current + '/'
self.set_header('GENERATE PASSWORD')
self._clear_box()
self.generate_password_path = urwid.Edit(("highlight", "Password Name: "), path)
self.generate_password_length = urwid.IntEdit("Length: ", default=length)
self.generate_password_symbols = urwid.CheckBox("Symbols", state=symbols)
self.generate_password_force = urwid.CheckBox("Overwrite existing passwords with that name", state=force)
self.box.body.append(self.generate_password_path)
self.box.body.append(self.generate_password_length)
self.box.body.append(self.generate_password_symbols)
self.box.body.append(self.generate_password_force)
self.box.body.append(ActionButton('GENERATE', self.call_generate))
self.box.body.append(BackButton('BACK', self.load_dispatch, self.current, self))

def call_generate(self, originator):
path = self.generate_password_path.edit_text
length = str(self.generate_password_length.value())
symbols = self.generate_password_symbols.state
force = self.generate_password_force.state
self._clear_box()
self.box.body.append(urwid.AttrMap(
urwid.Text(
'Generating "{0}": (length: {1}, symbols: {2}, overwrite: {3})'.format(path, length, symbols, force)
), 'highlight')
)
self.loop.draw_screen()
result_msg, error_msg = '', ''
if not path or path.endswith('/'):
error_msg = 'Invalid key path "{0}". Please enter a valid password name.'.format(path)
elif self._store_key_exists(path) and not force:
error_msg = ('Password with name "{0}" already exists. '
'Please select the option to overwrite the existing password to generate a new password '
'for this entry.'.format(path))
else:
gargs = ['pass', 'generate']
if not symbols:
gargs.append('-n')
if force:
gargs.append('-f')
gargs.extend([path, length])
p = subprocess.Popen(gargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
try:
stdout, stderr = p.communicate(timeout=5)
if p.returncode == 0:
self.refresh()
if stdout:
# Strip ANSI escape codes for text styling and colors from stdout.
ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
result_msg = ansi_escape.sub('', stdout.decode('utf-8'))
if stderr:
error_msg = stderr.decode('utf-8')
except subprocess.TimeoutExpired:
error_msg = 'Command timed out: {0}'.format(' '.join(gargs))
self._clear_box()
self.mode = 'generate_results'
if error_msg:
self.box.body.append(urwid.Text(('error', 'ERROR')))
self.box.body.append(urwid.Text(('error', error_msg)))
if result_msg:
self.box.body.append(urwid.Text(result_msg))
if self._store_key_exists(path):
self.box.body.append(ActionButton('LOAD PASSWORD', self.pass_load, path))
self.box.body.append(ActionButton('GENERATE NEW PASSWORD', self.generate_password,
(path, length, symbols, force)))
self.box.body.append(BackButton('BACK TO DIRECTORY', self.dir_load,
os.path.dirname(path) or '.', self))

def _store_key_exists(self, path):
return os.path.exists(os.path.join(self.home, path + '.gpg'))

def search(self, originator):
"""Display the search box."""
if self.mode == 'search':
Expand Down Expand Up @@ -382,6 +462,7 @@ def pass_load(self, originator, path):
(path, True, False)))
self.box.body.append(ActionButton('COPY EVERYTHING', self.call_pass,
(path, True, True)))
self.box.body.append(ActionButton('GENERATE NEW PASSWORD', self.generate_password, (path, 16, True, True)))
self.box.body.append(urwid.Text("More copy options are available after displaying the password."))

def call_pass(self, originator, args):
Expand Down Expand Up @@ -495,6 +576,7 @@ def main():
"""The main function of upass."""
return App().run()


if __name__ == '__main__':
try:
sys.exit(main())
Expand Down
1 change: 1 addition & 0 deletions upass/data/upass.ini.skel
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
help=? h
display=d s
copy=c
generate=g
refresh=r
search=/
quit=q f10
Expand Down

0 comments on commit 6c3f3ab

Please sign in to comment.