-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.py
38 lines (29 loc) · 1019 Bytes
/
util.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
import string
from passlib.hash import sha512_crypt
from random import SystemRandom
import database
from flask_login import login_user
_random = SystemRandom()
def random_string(size=10, chars=string.ascii_letters + string.digits):
return "".join(_random.choice(chars) for x in range(size))
def create_user(**kwargs):
username = kwargs.get("username")
password = kwargs.get("password")
email = kwargs.get("email")
hash = sha512_crypt.encrypt(password)
user = database.User(username=username, hash=hash, email=email)
if database.User.objects().count() == 0:
user.admin = True
user.save()
login_user(user)
def authenticate_user(username, password):
user = database.User.objects(username__iexact=username).first()
if user is None:
return None
if sha512_crypt.verify(password, user.hash):
return user
else:
return None
def lookup_user(username):
user = database.User.objects(username__iexact=username).first()
return user