Skip to content

Instantly share code, notes, and snippets.

@fliphess
Last active December 30, 2015 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fliphess/7836479 to your computer and use it in GitHub Desktop.
Save fliphess/7836479 to your computer and use it in GitHub Desktop.
add users + passwords to passdb for tornado basic auth example
#!/usr/bin/env python
# for example itself see: https://gist.github.com/fliphess/7836469
import argparse
import bcrypt
import getpass
import sys
import os.path
import re
def parse_options():
""" Get options and return arguments
"""
parser = argparse.ArgumentParser(description="""
Add an administrator to the password file
Flip Hess 2013 - <flip@fliphess.com>
""")
parser.add_argument('-f', '--file', help='The server settings', required=True, type=str)
parser.add_argument('-u', '--user', help='The host to delete', required=True, type=str)
arguments = vars(parser.parse_args())
return arguments
def main():
args = parse_options()
if not os.path.isfile(args['file']):
print "Fail! File not present!"
sys.exit(1)
with open(args['file'], "r") as fh:
content = fh.readlines()
r = re.compile(r"%s:.*" % args['user'])
for line in content:
m = r.search(line)
if m:
print "User allready in password file! Please remove first!"
sys.exit(1)
print "Enter password:"
password1 = getpass.getpass()
print "Enter again:"
password2 = getpass.getpass()
if not password1 == password2:
print "Passwords do not match!"
sys.exit(1)
hashed = bcrypt.hashpw(password1, bcrypt.gensalt(12))
with open(args['file'], "a+") as fh:
fh.write("%s:%s\n" % (args['user'], hashed))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment