-
Notifications
You must be signed in to change notification settings - Fork 6
/
usermanager.py
32 lines (25 loc) · 1013 Bytes
/
usermanager.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
import bcrypt
import json
class UserManager:
def __init__(self, db):
self.db = db
with open(db, "r") as f:
self.users = json.loads(f.read().strip() or "{}")
def add(self, username, password):
if not (username and password):
raise ValueError("username and password are required")
if not (isinstance(username, str) and isinstance(password, str)):
raise TypeError("username and password have to be strings")
self.users[username] = bcrypt.hashpw(
password.encode("utf-8"), bcrypt.gensalt()
).decode("ascii")
with open(self.db, "w") as f:
f.write(json.dumps(self.users))
def user_exists(self, username):
return username in self.users.keys()
def check_password(self, username, password):
if not self.user_exists(username):
return False
return bcrypt.checkpw(
password.encode("utf-8"), self.users[username].encode("ascii")
)