|
|
@@ -5,6 +5,7 @@ import hashlib
|
|
|
import hmac
|
|
|
import json
|
|
|
import os
|
|
|
+import re
|
|
|
import secrets
|
|
|
import time
|
|
|
|
|
|
@@ -134,7 +135,13 @@ def user_from_request(request: Request) -> dict:
|
|
|
|
|
|
|
|
|
def require_user(request: Request) -> dict:
|
|
|
- return user_from_request(request)
|
|
|
+ user = user_from_request(request)
|
|
|
+ if user["role"] == "user" and not user.get("email"):
|
|
|
+ raise HTTPException(
|
|
|
+ status_code=403,
|
|
|
+ detail={"code": "bind-email", "message": "请先绑定邮箱后继续使用"},
|
|
|
+ )
|
|
|
+ return user
|
|
|
|
|
|
|
|
|
def require_admin(request: Request) -> dict:
|
|
|
@@ -188,3 +195,63 @@ def check_sms_code(phone: str, code: str) -> bool:
|
|
|
return False
|
|
|
cur.execute("UPDATE sms_codes SET used = 1 WHERE id = %s", (row["id"],))
|
|
|
return True
|
|
|
+
|
|
|
+
|
|
|
+# ---------- 邮箱 ----------
|
|
|
+
|
|
|
+EMAIL_RE = re.compile(r"^[^\s@]+@[^\s@]+\.[^\s@]+$")
|
|
|
+EMAIL_DEMO = DEMO_MODE # SMTP 未配置时同样降级为演示模式(见 issue_email_code)
|
|
|
+
|
|
|
+
|
|
|
+def normalize_email(email: str) -> str:
|
|
|
+ e = str(email or "").strip().lower()
|
|
|
+ if len(e) > 120 or not EMAIL_RE.match(e):
|
|
|
+ raise HTTPException(status_code=400, detail="请输入正确的邮箱地址")
|
|
|
+ return e
|
|
|
+
|
|
|
+
|
|
|
+def issue_email_code(email: str) -> dict:
|
|
|
+ """发邮箱验证码。演示/未配置 SMTP 时验证码随响应返回。"""
|
|
|
+ import email_service
|
|
|
+
|
|
|
+ now = int(time.time())
|
|
|
+ with db() as cur:
|
|
|
+ cur.execute(
|
|
|
+ "SELECT sent_at FROM email_codes WHERE email = %s ORDER BY id DESC LIMIT 1", (email,)
|
|
|
+ )
|
|
|
+ recent = cur.fetchone()
|
|
|
+ if recent and now - recent["sent_at"] < SMS_RESEND_INTERVAL:
|
|
|
+ raise HTTPException(status_code=429, detail="发送太频繁,请稍后再试")
|
|
|
+ code = "%06d" % secrets.randbelow(1000000)
|
|
|
+ cur.execute(
|
|
|
+ "INSERT INTO email_codes (email, code, sent_at, expires_at, used) VALUES (%s,%s,%s,%s,0)",
|
|
|
+ (email, code, now, now + SMS_TTL),
|
|
|
+ )
|
|
|
+ cur.execute("DELETE FROM email_codes WHERE expires_at < %s", (now - SMS_TTL,))
|
|
|
+ demo = DEMO_MODE or not email_service.smtp_configured()
|
|
|
+ result = {"ok": True, "demo": demo}
|
|
|
+ if demo:
|
|
|
+ result["code"] = code
|
|
|
+ return result
|
|
|
+ status, err = email_service.send_email(
|
|
|
+ email, "【袋研社 POUCHLAB】登录验证码",
|
|
|
+ "你的登录验证码:%s(10 分钟内有效)。如非本人操作请忽略。" % code,
|
|
|
+ )
|
|
|
+ if status != "sent":
|
|
|
+ raise HTTPException(status_code=500, detail="邮件发送失败,请稍后再试")
|
|
|
+ return result
|
|
|
+
|
|
|
+
|
|
|
+def check_email_code(email: str, code: str) -> bool:
|
|
|
+ now = int(time.time())
|
|
|
+ with db() as cur:
|
|
|
+ cur.execute(
|
|
|
+ "SELECT id FROM email_codes WHERE email = %s AND code = %s AND used = 0 AND expires_at > %s"
|
|
|
+ " ORDER BY id DESC LIMIT 1",
|
|
|
+ (email, code, now),
|
|
|
+ )
|
|
|
+ row = cur.fetchone()
|
|
|
+ if not row:
|
|
|
+ return False
|
|
|
+ cur.execute("UPDATE email_codes SET used = 1 WHERE id = %s", (row["id"],))
|
|
|
+ return True
|