Last active
December 30, 2015 13:38
-
-
Save fliphess/7836479 to your computer and use it in GitHub Desktop.
add users + passwords to passdb for tornado basic auth example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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