Skip to content

Commit

Permalink
Refactor in preparation for #75 Configurable password prompt mechanisms
Browse files Browse the repository at this point in the history
  • Loading branch information
clach04 committed Sep 9, 2023
1 parent 1ba52ea commit e9eb68d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 5 additions & 1 deletion puren_tonbo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,11 @@ def debug_get_password():

#################

def getpassfunc(prompt=None):
# TODO replace with plugin classes
# TODO see dirname param in gen_caching_get_password()
def getpassfunc(prompt=None, preference_list=None):
preference_list = preference_list or ['any']
# text
if prompt:
if sys.platform == 'win32': # and isinstance(prompt, unicode): # FIXME better windows check and unicode check
#if windows, deal with unicode problem in console
Expand Down
7 changes: 5 additions & 2 deletions puren_tonbo/tools/ptcipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ def main(argv=None):
parser.add_option("-e", "--encrypt", action="store_false", dest="decrypt",
help="encrypt in_filename")
parser.add_option("--list-formats", help="Which encryption/file formats are available", action="store_true")
parser.add_option("--no-prompt", help="do not prompt for password", action="store_true")
parser.add_option("--password-prompt", "--password_prompt", help="Comma seperated list of prompt mechanism to use, options; any,text", default="any")
parser.add_option("--no-prompt", "--no_prompt", help="do not prompt for password", action="store_true")
parser.add_option("--cipher", help="Which encryption mechanism to use (file extension used as hint)")
parser.add_option("-p", "--password", help="password, if omitted but OS env PT_PASSWORD is set use that, if missing prompt")
parser.add_option("-P", "--password_file", help="file name where password is to be read from, trailing blanks are ignored")
Expand Down Expand Up @@ -127,12 +128,14 @@ def usage():
#print('DEBUG tmp_out_filename %r' % tmp_out_filename) # TODO replace with logging.debug call

if options.no_prompt:
options.password_prompt = None
default_password_value = '' # empty password, cause a bad password error
else:
options.password_prompt = options.password_prompt.split(',') # TODO validation options? for now rely on puren_tonbo.getpassfunc()
default_password_value = None
password = options.password or password_file or os.environ.get('PT_PASSWORD') or puren_tonbo.keyring_get_password() or default_password_value
if password is None:
password = getpass.getpass("Password:")
password = puren_tonbo.getpassfunc("Password:", preference_list=options.password_prompt)
if not isinstance(password, bytes):
password = password.encode('us-ascii')

Expand Down

0 comments on commit e9eb68d

Please sign in to comment.