-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
572 lines (488 loc) · 17.8 KB
/
main.py
File metadata and controls
572 lines (488 loc) · 17.8 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import hashlib
import hmac
import json
import os
import secrets
import sqlite3
import time
from datetime import datetime, timedelta
from pathlib import Path
import jwt
import uvicorn
from fastapi import Depends, HTTPException, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from fastapi.staticfiles import StaticFiles
from fastapi_offline import FastAPIOffline
from pydantic import BaseModel, Field
from starlette.exceptions import HTTPException as StarletteHTTPException
app = FastAPIOffline(title="MindMaster思维导图大师API", version="1.0.0")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
DATABASE_PATH = os.environ.get("MINDMASTER_DB", "data/mindmaster.db")
JWT_SECRET = os.environ.get("JWT_SECRET", "change-me-in-production")
JWT_ALGORITHM = "HS256"
JWT_EXPIRE_DAYS = 7
PASSWORD_ITERATIONS = 200_000
security = HTTPBearer()
class AuthPayload(BaseModel):
username: str = Field(..., min_length=3, max_length=50)
password: str = Field(..., min_length=6, max_length=128)
class TokenResponse(BaseModel):
token: str
username: str
class SyncPushRequest(BaseModel):
data: dict
client_updated_at: int | None = None
class SyncPullResponse(BaseModel):
data: dict | None
updated_at: int | None
class FileCreateRequest(BaseModel):
name: str = Field(..., min_length=1, max_length=120)
data: dict
class FileRenameRequest(BaseModel):
name: str = Field(..., min_length=1, max_length=120)
class FileUpdateRequest(BaseModel):
data: dict
class FileResponse(BaseModel):
id: int
name: str
created_at: int
updated_at: int
class FileDetailResponse(FileResponse):
data: dict
class PasswordResetRequest(BaseModel):
password: str = Field(..., min_length=6, max_length=128)
class PasswordChangeRequest(BaseModel):
current_password: str = Field(..., min_length=6, max_length=128)
new_password: str = Field(..., min_length=6, max_length=128)
class SPAStaticFiles(StaticFiles):
async def get_response(self, path, scope):
try:
response = await super().get_response(path, scope)
except StarletteHTTPException as exc:
if exc.status_code == 404:
return await super().get_response("index.html", scope)
raise
if response.status_code == 404:
return await super().get_response("index.html", scope)
return response
def get_db():
db_dir = os.path.dirname(DATABASE_PATH)
if db_dir:
os.makedirs(db_dir, exist_ok=True)
conn = sqlite3.connect(DATABASE_PATH)
conn.row_factory = sqlite3.Row
return conn
def init_db():
conn = get_db()
try:
conn.execute(
"""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
created_at INTEGER NOT NULL
)
"""
)
columns = conn.execute("PRAGMA table_info(users)").fetchall()
column_names = {col["name"] for col in columns}
if "is_admin" not in column_names:
conn.execute("ALTER TABLE users ADD COLUMN is_admin INTEGER NOT NULL DEFAULT 0")
conn.execute(
"""
CREATE TABLE IF NOT EXISTS mindmaps (
user_id INTEGER PRIMARY KEY,
data_json TEXT NOT NULL,
updated_at INTEGER NOT NULL,
FOREIGN KEY(user_id) REFERENCES users(id)
)
"""
)
conn.execute(
"""
CREATE TABLE IF NOT EXISTS files (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
name TEXT NOT NULL,
data_json TEXT NOT NULL,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
FOREIGN KEY(user_id) REFERENCES users(id)
)
"""
)
conn.commit()
finally:
conn.close()
def hash_password(password: str) -> str:
salt = secrets.token_bytes(16)
digest = hashlib.pbkdf2_hmac("sha256", password.encode("utf-8"), salt, PASSWORD_ITERATIONS)
return f"{salt.hex()}${digest.hex()}"
def verify_password(password: str, stored: str) -> bool:
try:
salt_hex, digest_hex = stored.split("$", 1)
except ValueError:
return False
salt = bytes.fromhex(salt_hex)
expected = bytes.fromhex(digest_hex)
candidate = hashlib.pbkdf2_hmac("sha256", password.encode("utf-8"), salt, PASSWORD_ITERATIONS)
return hmac.compare_digest(candidate, expected)
def create_token(user_id: int, username: str) -> str:
expires = datetime.utcnow() + timedelta(days=JWT_EXPIRE_DAYS)
payload = {"sub": str(user_id), "username": username, "exp": expires}
return jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM)
def get_current_user(
credentials: HTTPAuthorizationCredentials = Depends(security),
):
token = credentials.credentials
try:
payload = jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGORITHM])
except jwt.ExpiredSignatureError as exc:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Token expired") from exc
except jwt.InvalidTokenError as exc:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token") from exc
user_id = int(payload.get("sub", "0"))
conn = get_db()
try:
row = conn.execute(
"SELECT id, username, is_admin FROM users WHERE id = ?", (user_id,)
).fetchone()
finally:
conn.close()
if not row:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User not found")
return {"id": row["id"], "username": row["username"], "is_admin": row["is_admin"]}
def require_admin(current_user=Depends(get_current_user)):
if not current_user.get("is_admin"):
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Admin only")
return current_user
def ensure_admin_account():
conn = get_db()
try:
row = conn.execute(
"SELECT id, is_admin FROM users WHERE username = ?", ("admin",)
).fetchone()
if row:
if not row["is_admin"]:
conn.execute("UPDATE users SET is_admin = 1 WHERE id = ?", (row["id"],))
conn.commit()
return
password_hash = hash_password("password")
created_at = int(time.time())
conn.execute(
"INSERT INTO users (username, password_hash, created_at, is_admin) VALUES (?, ?, ?, 1)",
("admin", password_hash, created_at),
)
conn.commit()
finally:
conn.close()
@app.on_event("startup")
def startup():
init_db()
ensure_admin_account()
@app.get("/api/health")
def health():
return {"status": "ok", "timestamp": int(time.time())}
@app.post("/api/auth/register", response_model=TokenResponse)
def register(payload: AuthPayload):
conn = get_db()
try:
exists = conn.execute(
"SELECT 1 FROM users WHERE username = ?", (payload.username,)
).fetchone()
if exists:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Username already exists")
password_hash = hash_password(payload.password)
created_at = int(time.time())
cursor = conn.execute(
"INSERT INTO users (username, password_hash, created_at, is_admin) VALUES (?, ?, ?, 0)",
(payload.username, password_hash, created_at),
)
conn.commit()
user_id = cursor.lastrowid
finally:
conn.close()
token = create_token(user_id, payload.username)
return TokenResponse(token=token, username=payload.username)
@app.post("/api/auth/login", response_model=TokenResponse)
def login(payload: AuthPayload):
conn = get_db()
try:
row = conn.execute(
"SELECT id, username, password_hash FROM users WHERE username = ?",
(payload.username,),
).fetchone()
finally:
conn.close()
if not row or not verify_password(payload.password, row["password_hash"]):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid username or password")
token = create_token(row["id"], row["username"])
return TokenResponse(token=token, username=row["username"])
@app.get("/api/auth/me")
def me(current_user=Depends(get_current_user)):
return current_user
@app.post("/api/auth/reset-password")
def reset_password(payload: PasswordChangeRequest, current_user=Depends(get_current_user)):
conn = get_db()
try:
row = conn.execute(
"SELECT password_hash FROM users WHERE id = ?",
(current_user["id"],),
).fetchone()
if not row or not verify_password(payload.current_password, row["password_hash"]):
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Current password is incorrect")
password_hash = hash_password(payload.new_password)
conn.execute(
"UPDATE users SET password_hash = ? WHERE id = ?",
(password_hash, current_user["id"]),
)
conn.commit()
finally:
conn.close()
return {"status": "ok"}
@app.get("/api/files", response_model=list[FileResponse])
def list_files(current_user=Depends(get_current_user)):
conn = get_db()
try:
rows = conn.execute(
"""
SELECT id, name, created_at, updated_at
FROM files
WHERE user_id = ?
ORDER BY updated_at DESC
""",
(current_user["id"],),
).fetchall()
finally:
conn.close()
return [FileResponse(**dict(row)) for row in rows]
@app.post("/api/files", response_model=FileDetailResponse)
def create_file(payload: FileCreateRequest, current_user=Depends(get_current_user)):
conn = get_db()
try:
now = int(time.time())
data_json = json.dumps(payload.data, ensure_ascii=False)
cursor = conn.execute(
"""
INSERT INTO files (user_id, name, data_json, created_at, updated_at)
VALUES (?, ?, ?, ?, ?)
""",
(current_user["id"], payload.name, data_json, now, now),
)
conn.commit()
file_id = cursor.lastrowid
finally:
conn.close()
return FileDetailResponse(
id=file_id,
name=payload.name,
created_at=now,
updated_at=now,
data=payload.data,
)
@app.get("/api/files/{file_id}", response_model=FileDetailResponse)
def get_file(file_id: int, current_user=Depends(get_current_user)):
conn = get_db()
try:
row = conn.execute(
"""
SELECT id, name, data_json, created_at, updated_at
FROM files
WHERE id = ? AND user_id = ?
""",
(file_id, current_user["id"]),
).fetchone()
finally:
conn.close()
if not row:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="File not found")
return FileDetailResponse(
id=row["id"],
name=row["name"],
created_at=row["created_at"],
updated_at=row["updated_at"],
data=json.loads(row["data_json"]),
)
@app.put("/api/files/{file_id}/rename", response_model=FileResponse)
def rename_file(file_id: int, payload: FileRenameRequest, current_user=Depends(get_current_user)):
conn = get_db()
try:
row = conn.execute(
"SELECT id, created_at FROM files WHERE id = ? AND user_id = ?",
(file_id, current_user["id"]),
).fetchone()
if not row:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="File not found")
updated_at = int(time.time())
conn.execute(
"UPDATE files SET name = ?, updated_at = ? WHERE id = ? AND user_id = ?",
(payload.name, updated_at, file_id, current_user["id"]),
)
conn.commit()
finally:
conn.close()
return FileResponse(
id=file_id,
name=payload.name,
created_at=row["created_at"],
updated_at=updated_at,
)
@app.put("/api/files/{file_id}/data", response_model=FileResponse)
def update_file(file_id: int, payload: FileUpdateRequest, current_user=Depends(get_current_user)):
conn = get_db()
try:
row = conn.execute(
"SELECT id, name, created_at FROM files WHERE id = ? AND user_id = ?",
(file_id, current_user["id"]),
).fetchone()
if not row:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="File not found")
updated_at = int(time.time())
data_json = json.dumps(payload.data, ensure_ascii=False)
conn.execute(
"UPDATE files SET data_json = ?, updated_at = ? WHERE id = ? AND user_id = ?",
(data_json, updated_at, file_id, current_user["id"]),
)
conn.commit()
finally:
conn.close()
return FileResponse(
id=file_id,
name=row["name"],
created_at=row["created_at"],
updated_at=updated_at,
)
@app.delete("/api/files/{file_id}")
def delete_file(file_id: int, current_user=Depends(get_current_user)):
conn = get_db()
try:
row = conn.execute(
"SELECT id FROM files WHERE id = ? AND user_id = ?",
(file_id, current_user["id"]),
).fetchone()
if not row:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="File not found")
conn.execute(
"DELETE FROM files WHERE id = ? AND user_id = ?",
(file_id, current_user["id"]),
)
conn.commit()
finally:
conn.close()
return {"status": "ok"}
@app.get("/api/sync/pull", response_model=SyncPullResponse)
def sync_pull(current_user=Depends(get_current_user)):
conn = get_db()
try:
row = conn.execute(
"SELECT data_json, updated_at FROM mindmaps WHERE user_id = ?",
(current_user["id"],),
).fetchone()
finally:
conn.close()
if not row:
return SyncPullResponse(data=None, updated_at=None)
return SyncPullResponse(data=json.loads(row["data_json"]), updated_at=row["updated_at"])
@app.post("/api/sync/push")
def sync_push(payload: SyncPushRequest, current_user=Depends(get_current_user)):
conn = get_db()
try:
row = conn.execute(
"SELECT updated_at FROM mindmaps WHERE user_id = ?",
(current_user["id"],),
).fetchone()
if row and payload.client_updated_at is not None:
if row["updated_at"] > payload.client_updated_at:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail="Remote data is newer, pull first",
)
updated_at = int(time.time())
data_json = json.dumps(payload.data, ensure_ascii=False)
if row:
conn.execute(
"UPDATE mindmaps SET data_json = ?, updated_at = ? WHERE user_id = ?",
(data_json, updated_at, current_user["id"]),
)
else:
conn.execute(
"INSERT INTO mindmaps (user_id, data_json, updated_at) VALUES (?, ?, ?)",
(current_user["id"], data_json, updated_at),
)
conn.commit()
finally:
conn.close()
return {"updated_at": updated_at}
@app.get("/api/admin/users")
def admin_list_users(admin_user=Depends(require_admin)):
conn = get_db()
try:
rows = conn.execute(
"""
SELECT u.id, u.username, u.created_at, u.is_admin,
COUNT(f.id) AS file_count
FROM users u
LEFT JOIN files f ON f.user_id = u.id
GROUP BY u.id
ORDER BY u.created_at DESC
"""
).fetchall()
finally:
conn.close()
return [dict(row) for row in rows]
@app.post("/api/admin/users/{user_id}/reset-password")
def admin_reset_password(
user_id: int, payload: PasswordResetRequest, admin_user=Depends(require_admin)
):
conn = get_db()
try:
row = conn.execute("SELECT id FROM users WHERE id = ?", (user_id,)).fetchone()
if not row:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found")
password_hash = hash_password(payload.password)
conn.execute(
"UPDATE users SET password_hash = ? WHERE id = ?", (password_hash, user_id)
)
conn.commit()
finally:
conn.close()
return {"status": "ok"}
@app.get("/api/admin/files")
def admin_list_files(admin_user=Depends(require_admin)):
conn = get_db()
try:
rows = conn.execute(
"""
SELECT f.id, f.name, f.created_at, f.updated_at, u.username, u.id as user_id
FROM files f
JOIN users u ON u.id = f.user_id
ORDER BY f.updated_at DESC
"""
).fetchall()
finally:
conn.close()
return [dict(row) for row in rows]
@app.delete("/api/admin/files/{file_id}")
def admin_delete_file(file_id: int, admin_user=Depends(require_admin)):
conn = get_db()
try:
row = conn.execute("SELECT id FROM files WHERE id = ?", (file_id,)).fetchone()
if not row:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="File not found")
conn.execute("DELETE FROM files WHERE id = ?", (file_id,))
conn.commit()
finally:
conn.close()
return {"status": "ok"}
STATIC_DIR = Path(__file__).resolve().parent / "frontend" / "dist"
app.mount("/", SPAStaticFiles(directory=str(STATIC_DIR), html=True), name="frontend")
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000)