2017-09-09 15:08:45 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import bcrypt
|
2017-09-12 16:50:45 +03:00
|
|
|
import random
|
2017-09-09 15:08:45 +03:00
|
|
|
import sqlite3
|
|
|
|
|
|
|
|
|
|
|
|
def parse_command_line():
|
2017-09-09 15:18:57 +03:00
|
|
|
"""Parse command line arguments.
|
|
|
|
Add new user:
|
|
|
|
./users.py -u <USERNAME> [-p <PASSWORD>]
|
|
|
|
Delete user:
|
|
|
|
./users.py -u <USERNAME> --delete
|
|
|
|
"""
|
2017-09-09 15:08:45 +03:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("-u", "--user",
|
|
|
|
help="User name")
|
|
|
|
parser.add_argument("-p", "--password",
|
|
|
|
help="User password. If it none password will be generated")
|
|
|
|
parser.add_argument("-b", "--database", default="./auth.db", help="Path to users database")
|
|
|
|
parser.add_argument("--delete", action='store_true', help="Delete user")
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
class User:
|
|
|
|
|
|
|
|
def __init__(self, args):
|
|
|
|
self.args = args
|
|
|
|
self.user = args.user
|
|
|
|
self.password = args.password
|
|
|
|
self.connection = self._init_connection()
|
|
|
|
|
|
|
|
def _init_connection(self):
|
|
|
|
con = sqlite3.connect(self.args.database)
|
|
|
|
cur = con.cursor()
|
|
|
|
return {"cursor": cur, "connect": con}
|
|
|
|
|
|
|
|
def _close_connection(self):
|
|
|
|
self.connection["cursor"].close()
|
|
|
|
self.connection["connect"].close()
|
|
|
|
|
|
|
|
def _commit(self):
|
|
|
|
self.connection["connect"].commit()
|
|
|
|
|
|
|
|
def add(self):
|
|
|
|
if self.password is None:
|
|
|
|
self.password = self._passgen()
|
2018-04-06 13:03:23 +03:00
|
|
|
print "Password has been generated: ", self.password
|
2017-09-09 15:08:45 +03:00
|
|
|
hashed_password = bcrypt.hashpw(self.password.encode('utf-8'), bcrypt.gensalt())
|
2017-09-12 16:50:45 +03:00
|
|
|
try:
|
|
|
|
self.connection["cursor"].execute(
|
|
|
|
"INSERT INTO users (name, password) VALUES (?, ?)",
|
|
|
|
(self.user, hashed_password)
|
|
|
|
)
|
|
|
|
self._commit()
|
|
|
|
except sqlite3.IntegrityError:
|
|
|
|
print 'user already exists'
|
2017-09-09 15:08:45 +03:00
|
|
|
self._close_connection()
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
self.connection["cursor"].execute(
|
|
|
|
"DELETE FROM users WHERE name=?",
|
|
|
|
(self.user,)
|
|
|
|
)
|
|
|
|
self._commit()
|
|
|
|
self._close_connection()
|
|
|
|
|
|
|
|
def _passgen(self):
|
2017-09-12 16:50:45 +03:00
|
|
|
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
|
|
|
|
return ''.join(random.choice(chars) for i in range(10))
|
2017-09-09 15:08:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
args = parse_command_line()
|
|
|
|
user = User(args)
|
|
|
|
if not args.delete:
|
|
|
|
user.add()
|
|
|
|
else:
|
|
|
|
user.delete()
|