Skip to content

Commit b6b6604

Browse files
committed
Add admin account when running init_sqlite.sh
1 parent 8692707 commit b6b6604

File tree

4 files changed

+61
-15
lines changed

4 files changed

+61
-15
lines changed

init_sqlite.sh

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env bash
22

33
SQLITE=sqlite3
4+
PYTHON=python3
45

56
set -e
67

@@ -10,7 +11,19 @@ then
1011
exit 1
1112
fi
1213

13-
$SQLITE $1 -init schema.txt "insert into config (
14+
if [ -e "$1" ]
15+
then
16+
echo "Database '$1' already exists" >&2
17+
exit 1
18+
fi
19+
20+
read -p 'Admin username: ' username
21+
read -sp 'Admin password: ' password
22+
23+
password=$($PYTHON tool.py password "$password")
24+
time=$($PYTHON -c 'import time; print(time.time_ns())')
25+
26+
$SQLITE "$1" -init schema.txt "insert into config (
1427
version,
1528
name,
1629
description,
@@ -25,4 +38,10 @@ values (
2538
'$(head -c 30 /dev/urandom | base64)',
2639
'$(head -c 30 /dev/urandom | base64)',
2740
0
28-
);"
41+
);
42+
43+
insert into users (name, password, role, join_time)
44+
values (lower('$username'), '$password', 2, $time);
45+
"
46+
47+
echo "Database '$1' created" >&2

main.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import passlib.hash, secrets
99
import time
1010
from datetime import datetime
11-
import captcha
11+
import captcha, password
1212

1313
app = Flask(__name__)
1414
db = DB(os.getenv('DB'))
@@ -103,7 +103,7 @@ def login():
103103
v = db.get_user_password(request.form['username'])
104104
if v is not None:
105105
id, hash = v
106-
if verify_password(request.form['password'], hash):
106+
if password.verify(request.form['password'], hash):
107107
flash('Logged in', 'success')
108108
session['user_id'] = id
109109
return redirect(url_for('index'))
@@ -155,8 +155,8 @@ def user_edit_password():
155155
flash('New password must be at least 8 characters long', 'error')
156156
else:
157157
hash, = db.get_user_password_by_id(user_id)
158-
if verify_password(request.form['old'], hash):
159-
if db.set_user_password(user_id, hash_password(new)):
158+
if password.verify(request.form['old'], hash):
159+
if db.set_user_password(user_id, password.hash(new)):
160160
flash('Updated password', 'success')
161161
else:
162162
flash('Failed to update password', 'error')
@@ -361,7 +361,7 @@ def register():
361361
request.form['answer'],
362362
):
363363
flash('CAPTCHA answer is incorrect', 'error')
364-
elif not db.register_user(username, hash_password(password), time.time_ns()):
364+
elif not db.register_user(username, password.hash(password), time.time_ns()):
365365
flash('Failed to create account (username may already be taken)', 'error')
366366
else:
367367
flash('Account has been created. You can login now.', 'success')
@@ -522,7 +522,7 @@ def admin_new_user():
522522
name, password = request.form['name'], request.form['password']
523523
if name == '' or password == '':
524524
flash('Name and password may not be empty')
525-
elif db.add_user(name, hash_password(password), time.time_ns()):
525+
elif db.add_user(name, password.hash(password), time.time_ns()):
526526
flash('Added user', 'success')
527527
else:
528528
flash('Failed to add user', 'error')
@@ -678,13 +678,6 @@ def minimd(text):
678678
}
679679

680680

681-
def hash_password(password):
682-
return passlib.hash.argon2.hash(password)
683-
684-
def verify_password(password, hash):
685-
return passlib.hash.argon2.verify(password, hash)
686-
687-
688681
def restart():
689682
'''
690683
Shut down *all* workers and spawn new ones.

password.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import passlib.hash
2+
3+
def hash(password):
4+
return passlib.hash.argon2.hash(password)
5+
6+
def verify(password, hash):
7+
return passlib.hash.argon2.verify(password, hash)
8+
9+

tool.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
3+
import sys, password
4+
5+
def arg(i, s):
6+
if i < len(sys.argv):
7+
return sys.argv[i]
8+
print(s)
9+
sys.exit(1)
10+
11+
def arg_last(i, s):
12+
if i == len(sys.argv) - 1:
13+
return sys.argv[i]
14+
print(s)
15+
sys.exit(1)
16+
17+
proc = 'tool.py' if len(sys.argv) < 1 else sys.argv[0]
18+
cmd = arg(1, f'usage: {proc} <command> [...]')
19+
20+
if cmd == 'password':
21+
pwd = arg_last(2, 'usage: {proc} password <pwd>')
22+
print(password.hash(pwd))
23+
else:
24+
print('unknown command ', cmd)
25+
sys.exit(1)

0 commit comments

Comments
 (0)