-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_admin.py
65 lines (57 loc) · 2 KB
/
create_admin.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
# Creates a root account with a random password. Can also be used to reset the root password.
# If the database does not exist it is created too
import bcrypt
import sqlite3
import secrets
import sys
import os.path
from os import path
import collections
from config import *
if not path.exists(DATABASE):
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
query = open('schema.sql', 'r')
c.executescript(query.read())
conn.commit()
query.close()
print("Initialized database")
else:
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
admin_username = 'nick'
"""
Returns a named tuple containing a clear-text password and the corresponding bcrypt hash.
"""
Pass = collections.namedtuple('Pass', ['text', 'hash'])
def random_password(length=16):
p = secrets.token_urlsafe(length).encode('utf-8')
hashed = bcrypt.hashpw(p, bcrypt.gensalt())
return Pass(p.decode(), hashed)
#Check if the root account already exists
c.execute("SELECT * FROM users WHERE username=?", (admin_username,))
if(c.fetchone() != None):
# root user exists
print("Admin user '" + admin_username + "' already exists.")
input = input("Would you like to overwrite this user? (yes/no): ")
if(input != 'yes'):
print("Goodbye")
conn.close()
sys.exit()
else:
#Update the db
new_pass = random_password()
c.execute("UPDATE users SET password=? WHERE username=?", (new_pass.hash,admin_username))
conn.commit()
print("PLEASE SAVE THE FOLLOWING INFORMATION!")
print("Username: "+admin_username)
print("Password: " + new_pass.text)
else:
# root user does not exist
password = random_password()
c.execute('INSERT INTO users (username, password, display_name, permissions_level) VALUES (?, ?, ?, "1");', (admin_username, password.hash, admin_username))
conn.commit()
print("PLEASE SAVE THE FOLLOWING INFORMATION!")
print("Username: " + admin_username)
print("Password: " + password.text)
conn.close()