-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.py
More file actions
186 lines (162 loc) · 5.3 KB
/
users.py
File metadata and controls
186 lines (162 loc) · 5.3 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import json
import os
from hashlib import sha512
import uuid
import base64
import re
# deafult user: username: admin, password: admin
users = {
'admin': {
'start_salt': '5RJpr-v3T2K4wudpCG4POw==',
'end_salt': 'YAEmL_UiTX2hp_1aq6IUAA==',
'password': '2e094648a2c3fc168a585361d082599cf5cb2a8dac2cdcafd0f02edbd1adcd28d1d5f11be982e339897e57c9d9f1a063217faeb3c7e67ced683e2bbade3875dd',
'type': 'admin',
'is_logged' : False
}
}
try:
with open("users_data.json") as file:
users = json.load(file)
except:
pass
types = ["user", "admin"]
file_name = "users_data.json"
invaild_names = ['guest', '', 'sign up', 'server']
def is_admin(username):
return users[username]["type"] == "admin"
def get_users():
global users
if os.path.isfile(file_name):
with open(file_name, "r") as file:
users = json.load(file)
elif os.path.isdir(file_name):
raise("%s is directory, expect it to be file..." % (file_name))
else:
with open(file_name, "w") as file:
file.write(json.dumps(users))
return users
def has_special(word):
special_char = False
regexp = re.compile('[^0-9a-zA-Z]+')
if regexp.search(word):
special_char = True
return special_char
def save():
with open(file_name, "w") as file:
json.dump(users, file, indent=2)
def create_user(creator_name, username, password, usertype="user"):
# check whether the user has permissions to create users
try:
if not is_admin(creator_name):
return False, "6"
except:
if creator_name != "sign up":
return False, "4"
if has_special(username):
return False, "0"
# check if this name already exists
if username in users:
return False, "1"
if username in invaild_names:
return False, "2"
if usertype not in types:
return False, "5"
users[username] = {}
start_salt = base64.urlsafe_b64encode(uuid.uuid4().bytes)
end_salt = base64.urlsafe_b64encode(uuid.uuid4().bytes)
users[username]["start_salt"] = start_salt.decode()
users[username]["end_salt"] = end_salt.decode()
salted_password = start_salt + password.encode() + end_salt
hashed_password = sha512(salted_password)
users[username]["password"] = hashed_password.hexdigest()
users[username]["type"] = usertype
users[username]["is_logged"] = False
save()
return True, "3"
def login(username, password):
if username not in users:
return False, "0"
if users[username]["is_logged"]:
return False, "1"
user = users[username]
salted_password = (user["start_salt"] + password + user["end_salt"]).encode()
if user["password"] == sha512(salted_password).hexdigest():
users[username]["is_logged"] = True
return True, "3"
return False, "0"
def logout(username):
if username in users:
users[username]["is_logged"] = False
def close():
for user in users:
logout(user)
save()
def change_type(user, username, new_type):
global users
if not is_admin(user):
# return False, "permission denied"
return False, "1"
if not user_exist(username):
# return False, "user does not exist"
return False, "2"
if new_type not in types:
# return False, "invaild type!"
return False, "3"
users[username]["type"] = new_type
save()
# return True, "user type changed successfully"
return True, "4"
def change_name(user, old_username, new_username):
if not is_admin(user):
if user != old_username:
return False, "1", old_username # "permission denied"
if not user_exist(old_username):
return False, "2", old_username # "user does not exist"
if new_username in invaild_names:
return False, "3", old_username # "can't change to that name!"
users[new_username] = users.pop(old_username)
save()
return True, "4", new_username # "username changed successfully"
def mute(user, username):
if not is_admin(user):
# return False, "permission denied"
return False, "1"
if not user_exist(username):
# return False, "user does not exist"
return False, "2"
if users[username]["is_logged"]:
# return True, "user muted successfully!"
return True, "3"
# return False, "user is not connected"
return False, "4"
def unmute(user, username):
if not is_admin(user):
# return False, "permission denied"
return False, "0"
if not user_exist(username):
# return False, "user does not exist"
return False, "1"
if users[username]["is_logged"]:
# return True, "user unmuted successfully"
return True, "2"
# return False, "user is not connected"
return False, "3"
def kick(user, username):
if not is_admin(user):
# return False, "permission denied"
return False, "1"
if not user_exist(username):
# return False, "user does not exist"
return False, "2"
if users[username]["is_logged"]:
logout(username)
# return True, "user kicked successfully"
return True, "3"
# return False, "user is not connected"
return False, "4"
def exit():
for user in users:
logout(user)
save()
def user_exist(username):
return username in users