Repository navigation
Expand file tree
/
Copy pathaccounts.py
More file actions
173 lines (134 loc) · 4.58 KB
/
Copy pathaccounts.py
File metadata and controls
173 lines (134 loc) · 4.58 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
import hashlib
import hmac
import json
import os
import secrets
from threading import Lock
from logger import print
print("accounts module loaded")
class AccountStore:
def __init__(self, storage_path="accounts.wdb"):
print("Accounts module storage initialized")
self._lock = Lock()
self.storage_path = storage_path
self._accounts = {}
# Used for authentication attempts with unknown accounts.
self._dummy_salt = secrets.token_bytes(16)
self._dummy_hash = self._hash_password(
"fakeg-dummy-password",
self._dummy_salt,
)
self.load()
@staticmethod
def _hash_password(password, salt):
return hashlib.pbkdf2_hmac(
"sha256",
password.encode("utf-8"),
salt,
180_000,
)
def load(self):
"""Load accounts from disk on startup."""
if not os.path.exists(self.storage_path):
print("No account storage found; starting empty")
return
with open(self.storage_path, "r", encoding="utf-8") as f:
data = json.load(f)
accounts = {}
for email, account in data.items():
email = email.strip().lower()
accounts[email] = {
"salt": bytes.fromhex(account["salt"]),
"password_hash": bytes.fromhex(
account["password_hash"]
),
}
self._accounts = accounts
print(f"Loaded {len(self._accounts)} accounts")
def write(self):
"""Write account data to disk."""
data = {
email: {
"salt": account["salt"].hex(),
"password_hash": account["password_hash"].hex(),
}
for email, account in self._accounts.items()
}
directory = os.path.dirname(
os.path.abspath(self.storage_path)
)
os.makedirs(directory, exist_ok=True)
temp_path = self.storage_path + ".tmp"
try:
with open(temp_path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4)
f.flush()
os.fsync(f.fileno())
os.replace(temp_path, self.storage_path)
finally:
if os.path.exists(temp_path):
os.remove(temp_path)
print(f"Wrote {len(self._accounts)} accounts")
def add_account(self, email, password):
email = email.strip().lower()
if not email or not password:
raise ValueError("Email and password must not be empty")
salt = secrets.token_bytes(16)
password_hash = self._hash_password(password, salt)
self._accounts[email] = {
"salt": salt,
"password_hash": password_hash,
}
self.write()
def register_account(self, email, password):
with self._lock:
if email in self._accounts:
return False
# Only create the account after the duplicate check.
salt = secrets.token_bytes(16)
password_hash = self._hash_password(password, salt)
self._accounts[email] = {
"salt": salt,
"password_hash": password_hash,
}
self.write()
return True
def authenticate(self, email, password):
email = email.strip().lower()
account = self._accounts.get(email)
# Perform a password hash even for unknown accounts, so the
# simplest username-enumeration timing difference is reduced.
if account is None:
candidate = self._hash_password(
password,
self._dummy_salt,
)
hmac.compare_digest(candidate, self._dummy_hash)
r = False
else:
candidate = self._hash_password(
password,
account["salt"],
)
r = hmac.compare_digest(
candidate,
account["password_hash"],
)
return r
@classmethod
def for_local_development(cls):
accounts = cls()
email = os.environ.get(
"FAKEG_DEMO_EMAIL",
"demo@example.test",
)
password = os.environ.get(
"FAKEG_DEMO_PASSWORD",
"fakeg-demo-password",
)
email = email.strip().lower()
# Seed the demo account only when it doesn't already exist.
# This preserves the existing password across restarts.
if email not in accounts._accounts:
accounts.add_account(email, password)
return accounts