-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_user.py
More file actions
52 lines (45 loc) · 1.6 KB
/
make_user.py
File metadata and controls
52 lines (45 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import argparse
import sqlite3
from pathlib import Path
from typing import Dict
from argon2 import PasswordHasher
DB_FILE = "modernrelay.db"
USER_AND_PASSWORD: Dict[str, str] = {
"test": "test",
"test2": "test2"
}
class ParseKwargs(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, dict())
for value in values:
key, value = value.split('=')
getattr(namespace, self.dest)[key[1:-1]] = value[1:-1]
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Creates users and stores them in a sqlite database.')
parser.add_argument('-dbname', help='The name of the database file')
parser.add_argument('-users', nargs='*', action=ParseKwargs, help='users in \'user\'=\'password\' format')
args = parser.parse_args()
if args.dbname:
dbfp = Path(args.dbname).absolute()
else:
dbfp = Path(DB_FILE).absolute()
if dbfp.exists():
dbfp.unlink()
if args.dbname:
conn = sqlite3.connect(args.dbname)
else:
conn = sqlite3.connect(DB_FILE)
curs = conn.cursor()
curs.execute("CREATE TABLE userauth (username text, hashpass text)")
ph = PasswordHasher()
insert_up = "INSERT INTO userauth VALUES (?, ?)"
if args.users:
USER_AND_PASSWORD = args.users
for u, p in USER_AND_PASSWORD.items():
print(f"[-] Inserting {u} into the database")
h = ph.hash(p)
curs.execute(insert_up, (u, h))
conn.commit()
conn.close()
assert dbfp.exists()
print(f"[+] Database created at {dbfp}")