-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
188 lines (123 loc) · 4.75 KB
/
Copy pathauth.py
File metadata and controls
188 lines (123 loc) · 4.75 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
187
188
import hashlib
import socket
import uuid
import platform
import db
from config_loader import ENABLE_REGISTRATION_LIMIT
from utils import verify_password, validate_username, validate_password, generate_token, timestamp
current_user = None
current_token = None
def get_current_user():
global current_user
return current_user
def is_logged():
return current_user is not None
def is_admin():
if current_user:
if current_user.get("is_admin", 0) == 1:
return True
return False
def register(username, password):
valid, error = validate_username(username)
if not valid:
return False, error
valid, error = validate_password(password)
if not valid:
return False, error
hostname = socket.gethostname()
mac_int = uuid.getnode()
mac_bytes = mac_int.to_bytes(6, byteorder='big')
mac_parts = []
for byte in mac_bytes:
mac_parts.append(f"{byte:02x}")
mac_address = ":".join(mac_parts)
system = platform.system()
identifier = f"{hostname}:{mac_address}:{system}"
machine_id = hashlib.sha256(identifier.encode()).hexdigest()
if ENABLE_REGISTRATION_LIMIT:
success, result = db.create_user(username, password, machine_id=machine_id)
else:
success, result = db.create_user(username, password)
if success:
return True, result
else:
return False, result
def login(username, password):
global current_user, current_token
user = db.get_user_by_username(username)
if user is None:
return False, "User not found."
if user.get("is_banned") == 1:
reason = user.get("ban_reason")
return False, f"Your account has been banned. Reason: {reason}"
time = timestamp()
if user["locked_until"] > time:
remain = (user["locked_until"] - time) // 60
return False, f"Your account is locked due to multiple failed login attempts. Please try again in {remain} minutes."
if verify_password(password, user["password_hash"], user["password_salt"]):
token = generate_token()
expires = time + 86400
db.create_session(user["id"], token, expires)
current_user = user
current_token = token
db.update_user(user["id"], login_attempts=0)
return True, f"Welcome back, @{username}!"
else:
attempts = user["login_attempts"] + 1
if attempts >= 5:
lock_duration = time + 15 * 60 # 15 minutes
db.update_user(user["id"], login_attempts=0, locked_until=lock_duration)
return False, "Too many failed login attempts. Your account has been locked for 15 minutes."
else:
db.update_user(user["id"], login_attempts=attempts)
remaining_attempts = 5 - attempts
return False, f"Incorrect password. You have {remaining_attempts} more attempt(s)."
def logout():
global current_user, current_token
if current_token:
db.delete_session(current_token)
current_user = None
current_token = None
return True, "You have been logged out."
def delete_account(password):
global current_user
if not is_logged():
return False, "No user is currently logged in."
if not verify_password(password, current_user["password_hash"], current_user["password_salt"]):
return False, "Incorrect password. Account deletion aborted."
db.delete_user(current_user["id"])
logout()
return True, "Your account has been deleted."
def change_password(old_password, new_password):
global current_user
if not is_logged():
return False, "No user is currently logged in."
if not verify_password(old_password, current_user["password_hash"], current_user["password_salt"]):
return False, "Current password is incorrect."
valid, error = validate_password(new_password)
if not valid:
return False, error
db.change_user_password(current_user["id"], new_password)
db.delete_user_sessions(current_user["id"])
login(current_user["username"], new_password)
return True, "Password changed successfully."
def validate_session():
global current_user, current_token
if not current_token:
return False
session = db.get_session(current_token)
if session is None:
current_user = None
current_token = None
return False
now_timestamp = timestamp()
if session["expires"] < now_timestamp:
db.delete_session(current_token)
current_user = None
current_token = None
return False
current_user = db.get_user_by_id(session["user_id"])
if current_user and current_user.get("is_banned") == 1:
logout()
return False
return True