-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuser_store.py
More file actions
269 lines (217 loc) · 10.1 KB
/
user_store.py
File metadata and controls
269 lines (217 loc) · 10.1 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""User account storage with encrypted token support.
This module stores accounts keyed by internal account_id and maintains
separate identity indexes for Nostr pubkeys and BTC Map usernames.
"""
import fcntl
import json
import os
import uuid
from datetime import datetime
from typing import Any, Dict, Optional
from cryptography.fernet import Fernet, InvalidToken
def _utc_now_iso() -> str:
return datetime.utcnow().isoformat() + 'Z'
class UserStore:
"""Handles account persistence with encrypted token storage."""
def __init__(self, storage_path: str = 'users.json', cipher_key: Optional[str] = None):
self.storage_path = storage_path
key = cipher_key or os.environ.get('TOKEN_CIPHER_KEY')
if not key:
raise ValueError(
'TOKEN_CIPHER_KEY environment variable is required. '
'Generate one with: python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"'
)
self.cipher = Fernet(key.encode())
def _load_store(self) -> Dict[str, Any]:
"""Load store under a lock, auto-migrating legacy shape if needed."""
if not os.path.exists(self.storage_path):
return self._empty_store()
with open(self.storage_path, 'r+') as f:
fcntl.flock(f.fileno(), fcntl.LOCK_EX)
try:
content = f.read().strip()
raw = json.loads(content) if content else {}
if self._is_v2(raw):
return raw
migrated = self._migrate_legacy(raw)
f.seek(0)
f.truncate()
json.dump(migrated, f, indent=2)
return migrated
finally:
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
def _save_store(self, store: Dict[str, Any]) -> None:
"""Save full store under exclusive lock."""
with open(self.storage_path, 'w') as f:
fcntl.flock(f.fileno(), fcntl.LOCK_EX)
try:
json.dump(store, f, indent=2)
finally:
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
def _encrypt_token(self, token: str) -> str:
return self.cipher.encrypt(token.encode()).decode()
def _decrypt_token(self, encrypted_token: str) -> Optional[str]:
try:
return self.cipher.decrypt(encrypted_token.encode()).decode()
except InvalidToken:
return None
def _empty_store(self) -> Dict[str, Any]:
return {
'accounts': {},
'index_nostr': {},
'index_btcmap': {},
'schema_version': 2,
'migrated_at': _utc_now_iso(),
}
def _is_v2(self, data: Dict[str, Any]) -> bool:
return isinstance(data, dict) and 'accounts' in data and 'index_nostr' in data and 'index_btcmap' in data
def _migrate_legacy(self, legacy_data: Dict[str, Any]) -> Dict[str, Any]:
"""Migrate legacy identity-keyed records to account model."""
new_store = self._empty_store()
if not legacy_data:
return new_store
for legacy_key, legacy_user in legacy_data.items():
if not isinstance(legacy_user, dict):
continue
account_id = str(uuid.uuid4())
is_btcmap = isinstance(legacy_key, str) and legacy_key.startswith('btcmap:')
btcmap_username = legacy_key.split(':', 1)[1] if is_btcmap else legacy_user.get('btcmap_username')
nostr_pubkey = None if is_btcmap else legacy_key
account = {
'account_id': account_id,
'auth_type': legacy_user.get('auth_type', 'btcmap' if is_btcmap else 'nostr'),
'nostr_pubkey': nostr_pubkey,
'btcmap_username': btcmap_username,
'last_login': legacy_user.get('last_login'),
'last_login_method': legacy_user.get('last_login_method'),
'created_at': legacy_user.get('created_at', _utc_now_iso()),
'updated_at': legacy_user.get('updated_at', _utc_now_iso()),
}
if legacy_user.get('rpc_token_encrypted'):
account['rpc_token_encrypted'] = legacy_user['rpc_token_encrypted']
elif legacy_user.get('rpc_token'):
account['rpc_token_encrypted'] = self._encrypt_token(legacy_user['rpc_token'])
new_store['accounts'][account_id] = account
if nostr_pubkey:
new_store['index_nostr'][nostr_pubkey] = account_id
if btcmap_username:
new_store['index_btcmap'][btcmap_username.lower()] = account_id
new_store['migrated_at'] = _utc_now_iso()
return new_store
def with_store_update(self, updater):
"""Run read-modify-write atomically under one exclusive file lock."""
if not os.path.exists(self.storage_path):
with open(self.storage_path, 'w') as f:
json.dump(self._empty_store(), f, indent=2)
with open(self.storage_path, 'r+') as f:
fcntl.flock(f.fileno(), fcntl.LOCK_EX)
try:
content = f.read().strip()
raw = json.loads(content) if content else {}
store = raw if self._is_v2(raw) else self._migrate_legacy(raw)
result = updater(store)
f.seek(0)
f.truncate()
json.dump(store, f, indent=2)
return result
finally:
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
def _account_with_decrypted_token(self, account: Dict[str, Any]) -> Dict[str, Any]:
data = dict(account)
encrypted = data.get('rpc_token_encrypted')
data['rpc_token'] = self._decrypt_token(encrypted) if encrypted else None
return data
def get_account(self, account_id: str) -> Optional[Dict[str, Any]]:
store = self._load_store()
account = store['accounts'].get(account_id)
if not account:
return None
return self._account_with_decrypted_token(account)
def create_account(self, auth_type: str = 'nostr') -> Dict[str, Any]:
def updater(store):
account_id = str(uuid.uuid4())
account = {
'account_id': account_id,
'auth_type': auth_type,
'nostr_pubkey': None,
'btcmap_username': None,
'last_login': None,
'last_login_method': None,
'created_at': _utc_now_iso(),
'updated_at': _utc_now_iso(),
}
store['accounts'][account_id] = account
return self._account_with_decrypted_token(account)
return self.with_store_update(updater)
def find_account_by_nostr(self, nostr_pubkey: str) -> Optional[Dict[str, Any]]:
store = self._load_store()
account_id = store['index_nostr'].get(nostr_pubkey)
if not account_id:
return None
account = store['accounts'].get(account_id)
return self._account_with_decrypted_token(account) if account else None
def find_account_by_btcmap(self, username: str) -> Optional[Dict[str, Any]]:
store = self._load_store()
account_id = store['index_btcmap'].get(username.lower())
if not account_id:
return None
account = store['accounts'].get(account_id)
return self._account_with_decrypted_token(account) if account else None
def link_nostr(self, account_id: str, nostr_pubkey: str) -> None:
def updater(store):
mapped = store['index_nostr'].get(nostr_pubkey)
if mapped and mapped != account_id:
raise ValueError('Nostr pubkey is already linked to another account')
account = store['accounts'].get(account_id)
if not account:
raise ValueError('Account not found')
previous = account.get('nostr_pubkey')
if previous and previous != nostr_pubkey:
store['index_nostr'].pop(previous, None)
account['nostr_pubkey'] = nostr_pubkey
account['updated_at'] = _utc_now_iso()
store['index_nostr'][nostr_pubkey] = account_id
self.with_store_update(updater)
def link_btcmap(self, account_id: str, username: str) -> None:
uname = username.strip()
lower = uname.lower()
def updater(store):
mapped = store['index_btcmap'].get(lower)
if mapped and mapped != account_id:
raise ValueError('BTC Map username is already linked to another account')
account = store['accounts'].get(account_id)
if not account:
raise ValueError('Account not found')
previous = account.get('btcmap_username')
if previous and previous.lower() != lower:
store['index_btcmap'].pop(previous.lower(), None)
account['btcmap_username'] = uname
account['updated_at'] = _utc_now_iso()
store['index_btcmap'][lower] = account_id
self.with_store_update(updater)
def set_account_token(self, account_id: str, token: Optional[str]) -> None:
def updater(store):
account = store['accounts'].get(account_id)
if not account:
raise ValueError('Account not found')
if token:
account['rpc_token_encrypted'] = self._encrypt_token(token)
else:
account.pop('rpc_token_encrypted', None)
account['updated_at'] = _utc_now_iso()
self.with_store_update(updater)
def update_account_metadata(self, account_id: str, **updates: Any) -> None:
def updater(store):
account = store['accounts'].get(account_id)
if not account:
raise ValueError('Account not found')
for key, value in updates.items():
account[key] = value
account['updated_at'] = _utc_now_iso()
self.with_store_update(updater)
_user_store: Optional[UserStore] = None
def get_user_store() -> UserStore:
global _user_store
if _user_store is None:
_user_store = UserStore()
return _user_store