feat: 添加注册邮箱验证与 SMTP 配置

This commit is contained in:
ucmao
2026-11-19 09:42:46 +08:00
parent cdc5d817e6
commit efd2c25d9f
11 changed files with 511 additions and 17 deletions
+16
View File
@@ -84,3 +84,19 @@ PROXY_MODE=auto
PROXY_POOL_SIZE=2
# 全服务每秒最多调用提取接口次数(1-20,默认 1),不要超过代理商限制。
PROXY_FETCH_PER_SECOND=1
# ------------------------------------------
# 3. 邮件 / 注册邮箱验证 (SMTP,可选)
# 留空 SMTP_HOST 即关闭邮箱验证,注册仍为"用户名 + 密码";配置后注册需邮箱验证码(可在后台「系统设置」关闭)
# ------------------------------------------
SMTP_HOST=
# 端口与加密方式:465 + ssl(默认)、587 + starttls、25 + none
SMTP_PORT=465
SMTP_SECURITY=ssl
SMTP_USER=
# 多数邮箱(QQ / 163 / 阿里企业邮)需填写"授权码"而不是登录密码
SMTP_PASSWORD=
# 发件地址,留空默认使用 SMTP_USER
SMTP_FROM=
SMTP_FROM_NAME=Media Parser
+2
View File
@@ -11,6 +11,7 @@ from src.web.portal import bp as portal_bp
from src.web.admin import bp as admin_bp
from src.db import init_app as init_database
from src.utils.proxy_manager import ProxyManager
from src.mailer import load_smtp_config
from configs.logging_config import get_logger
logger = get_logger(__name__)
@@ -60,6 +61,7 @@ def create_app(config=None):
app.config['API_ONLY'] = os.getenv(
'API_ONLY', ''
).strip().lower() in {'1', 'true', 'yes', 'on'}
app.config.update(load_smtp_config())
if hasattr(app, 'json'):
app.json.sort_keys = False
if config:
+8
View File
@@ -33,6 +33,14 @@ services:
- PROXY_MODE=${PROXY_MODE:-auto}
- PROXY_POOL_SIZE=${PROXY_POOL_SIZE:-2}
- PROXY_FETCH_PER_SECOND=${PROXY_FETCH_PER_SECOND:-1}
# 可选 SMTP:未设置 SMTP_HOST 时注册无需邮箱验证
- SMTP_HOST=${SMTP_HOST:-}
- SMTP_PORT=${SMTP_PORT:-}
- SMTP_SECURITY=${SMTP_SECURITY:-}
- SMTP_USER=${SMTP_USER:-}
- SMTP_PASSWORD=${SMTP_PASSWORD:-}
- SMTP_FROM=${SMTP_FROM:-}
- SMTP_FROM_NAME=${SMTP_FROM_NAME:-}
# 可选平台凭据(若在本地 .env 中配置将自动透传进容器):
- XHS_COOKIE=${XHS_COOKIE:-}
- DOUYIN_COOKIE=${DOUYIN_COOKIE:-}
+149 -8
View File
@@ -1,12 +1,13 @@
import hashlib
import hmac
import re
import secrets
import string
from datetime import datetime, timezone
from functools import wraps
from zoneinfo import ZoneInfo
from flask import Blueprint, current_app, flash, g, redirect, render_template, request, session, url_for
from flask import Blueprint, current_app, flash, g, jsonify, redirect, render_template, request, session, url_for
from werkzeug.security import check_password_hash, generate_password_hash
from src.db import get_db, setting, utcnow
@@ -93,6 +94,62 @@ def registration_quota_exceeded():
return False
EMAIL_PATTERN = re.compile(r"^[^@\s]{1,64}@[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)+$")
EMAIL_CODE_TTL_SECONDS = 600
EMAIL_CODE_MAX_ATTEMPTS = 5
EMAIL_CODE_RESEND_SECONDS = 60
def email_verification_required():
"""仅当已配置 SMTP 且管理员未关闭时才要求邮箱验证;否则沿用原有的用户名注册流程。"""
from src.mailer import smtp_configured
return smtp_configured() and setting("email_verification_enabled", "1") == "1"
def normalize_email(value):
email = (value or "").strip().lower()
if len(email) > 254 or not EMAIL_PATTERN.match(email):
return None
return email
def _hash_email_code(email, code):
pepper = current_app.config["SECRET_KEY"].encode()
return hmac.new(pepper, f"register:{email}:{code}".encode(), hashlib.sha256).hexdigest()
def _email_taken(email):
return get_db().execute(
"SELECT 1 FROM users WHERE email = ? COLLATE NOCASE", (email,)
).fetchone() is not None
def verify_email_code(email, code, purpose="register"):
"""校验验证码;成功后立即作废,失败累计次数,超限作废。返回错误文案或 None。"""
db = get_db()
row = db.execute(
"SELECT * FROM email_verification_codes WHERE email = ? AND purpose = ?", (email, purpose)
).fetchone()
if row is None:
return "请先获取邮箱验证码"
if row["expires_at"] <= utcnow() or row["attempts"] >= EMAIL_CODE_MAX_ATTEMPTS:
db.execute("DELETE FROM email_verification_codes WHERE email = ? AND purpose = ?", (email, purpose))
db.commit()
return "验证码已失效,请重新获取"
if not code or not hmac.compare_digest(row["code_hash"], _hash_email_code(email, code)):
db.execute(
"UPDATE email_verification_codes SET attempts = attempts + 1 WHERE email = ? AND purpose = ?",
(email, purpose),
)
db.commit()
return "验证码错误"
return None
def consume_email_code(db, email, purpose="register"):
db.execute("DELETE FROM email_verification_codes WHERE email = ? AND purpose = ?", (email, purpose))
def csrf_protected(view):
@wraps(view)
def wrapped(*args, **kwargs):
@@ -190,28 +247,109 @@ def setup():
return render_template("auth/setup.html")
@bp.post("/register/send-code")
def register_send_code():
"""注册邮箱验证码:仅在启用邮箱验证时可用,返回 JSON 供注册页异步调用。"""
from src.api.access import consume_rate_limits, get_client_ip
from src.mailer import send_mail
def fail(message, status=400):
return jsonify({"succ": False, "message": message}), status
if setting("registration_enabled", "1") != "1":
return fail("当前未开放注册", 403)
if not email_verification_required():
return fail("当前未启用邮箱验证", 404)
if not validate_csrf():
return fail("页面已过期,请刷新后重试", 400)
email = normalize_email(request.form.get("email"))
if not email:
return fail("请输入有效的邮箱地址")
if _email_taken(email):
return fail("该邮箱已注册,请直接登录")
client_ip = get_client_ip()
email_digest = hashlib.sha256(email.encode()).hexdigest()
limited = consume_rate_limits([
(f"auth:email-code:email:{email_digest}", 1, EMAIL_CODE_RESEND_SECONDS),
(f"auth:email-code:email-day:{email_digest}", 5, 86400),
(f"auth:email-code:ip:{client_ip}", 5, 3600),
(f"auth:email-code:ip-day:{client_ip}", 20, 86400),
])
if limited is not None:
return fail("发送过于频繁,请稍后再试", 429)
code = f"{secrets.randbelow(1000000):06d}"
now = datetime.now(timezone.utc)
db = get_db()
db.execute(
"INSERT INTO email_verification_codes(email,purpose,code_hash,attempts,expires_at,created_at,request_ip) "
"VALUES(?,?,?,0,?,?,?) ON CONFLICT(email,purpose) DO UPDATE SET "
"code_hash=excluded.code_hash, attempts=0, expires_at=excluded.expires_at, "
"created_at=excluded.created_at, request_ip=excluded.request_ip",
(email, "register", _hash_email_code(email, code),
(now + timedelta(seconds=EMAIL_CODE_TTL_SECONDS)).isoformat(timespec="seconds"),
now.isoformat(timespec="seconds"), client_ip),
)
db.commit()
try:
send_mail(
email,
f"【Media Parser】注册验证码 {code}",
f"您好!\n\n您正在注册 Media Parser 账号,验证码为:{code}\n\n"
f"验证码 {EMAIL_CODE_TTL_SECONDS // 60} 分钟内有效,请勿泄露给他人。如非本人操作,请忽略本邮件。\n",
)
except Exception as exc:
current_app.logger.error("[邮件发送失败] %s: %s", type(exc).__name__, exc)
db.execute("DELETE FROM email_verification_codes WHERE email = ? AND purpose = 'register'", (email,))
db.commit()
return fail("验证码发送失败,请稍后再试或联系管理员", 502)
return jsonify({"succ": True, "message": "验证码已发送,请查收邮箱", "resend_after": EMAIL_CODE_RESEND_SECONDS})
@bp.route("/register", methods=("GET", "POST"))
def register():
if setting("registration_enabled", "1") != "1":
flash("当前未开放注册", "error")
return redirect(url_for("auth.login"))
email_required = email_verification_required()
def render_register(status=200):
return render_template(
"auth/register.html",
email_required=email_required,
form_username=request.form.get("username", "").strip(),
form_email=request.form.get("email", "").strip(),
), status
if request.method == "POST":
if not validate_csrf():
flash("页面已过期,请重试", "error")
elif auth_rate_limited("register"):
flash("注册尝试过于频繁,请稍后再试", "error")
return render_template("auth/register.html"), 429
return render_register(429)
elif registration_quota_exceeded():
flash("今日注册名额已满,请明天再试或联系管理员开通", "error")
return render_template("auth/register.html"), 429
return render_register(429)
else:
username = request.form.get("username", "").strip()
password = request.form.get("password", "")
confirm_password = request.form.get("confirm_password", "")
email = normalize_email(request.form.get("email")) if email_required else None
email_error = None
if email_required:
if not email:
email_error = "请输入有效的邮箱地址"
elif _email_taken(email):
email_error = "该邮箱已注册,请直接登录"
if len(username) < 3 or len(password) < 6:
flash("用户名至少 3 位,密码至少 6 位", "error")
elif password != confirm_password:
flash("两次输入的密码不一致", "error")
elif email_error:
flash(email_error, "error")
elif email_required and (code_error := verify_email_code(email, request.form.get("email_code", "").strip())):
flash(code_error, "error")
else:
try:
trial_days = int(setting("default_trial_days", "365"))
@@ -221,12 +359,14 @@ def register():
expires_at = (datetime.now(timezone.utc) + timedelta(days=trial_days)).isoformat(timespec="seconds")
db = get_db()
cur = db.execute(
"INSERT INTO users(username,password_hash,qps_limit,expires_at,credits,created_at,register_ip,last_login_ip,last_login_at) "
"VALUES(?,?,?,?,?,?,?,?,?)",
"INSERT INTO users(username,password_hash,qps_limit,expires_at,credits,created_at,register_ip,last_login_ip,last_login_at,email) "
"VALUES(?,?,?,?,?,?,?,?,?,?)",
(username, generate_password_hash(password), int(setting("default_user_qps", "2")), expires_at, initial_credits, utcnow(),
_client_ip(), _client_ip(), utcnow()),
_client_ip(), _client_ip(), utcnow(), email),
)
user_id = cur.lastrowid
if email:
consume_email_code(db, email)
db.commit()
# 自动登录并签发会话
@@ -255,8 +395,9 @@ def register():
except Exception as exc:
if "UNIQUE" not in str(exc).upper():
raise
flash("用户名已存在", "error")
return render_template("auth/register.html")
get_db().rollback()
flash("该邮箱已注册,请直接登录" if "email" in str(exc).lower() else "用户名已存在", "error")
return render_register()
@bp.route("/login", methods=("GET", "POST"))
+17
View File
@@ -71,6 +71,16 @@ CREATE TABLE IF NOT EXISTS rate_limit_buckets (
count INTEGER NOT NULL,
PRIMARY KEY(subject, bucket_second)
);
CREATE TABLE IF NOT EXISTS email_verification_codes (
email TEXT NOT NULL COLLATE NOCASE,
purpose TEXT NOT NULL,
code_hash TEXT NOT NULL,
attempts INTEGER NOT NULL DEFAULT 0,
expires_at TEXT NOT NULL,
created_at TEXT NOT NULL,
request_ip TEXT,
PRIMARY KEY(email, purpose)
);
CREATE INDEX IF NOT EXISTS idx_logs_created_at ON request_logs(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_logs_user_id ON request_logs(user_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_rate_limit_bucket_time ON rate_limit_buckets(bucket_second);
@@ -119,6 +129,7 @@ _COLUMN_MIGRATIONS = {
"register_ip": "TEXT",
"last_login_ip": "TEXT",
"last_login_at": "TEXT",
"email": "TEXT",
},
"request_logs": {
"client_ip": "TEXT",
@@ -133,6 +144,11 @@ def _migrate_columns(db):
if column not in existing:
db.execute(f"ALTER TABLE {table} ADD COLUMN {column} {column_type}")
db.execute("CREATE INDEX IF NOT EXISTS idx_users_register_ip ON users(register_ip)")
# 邮箱可为空(未配置 SMTP 时注册的账号没有邮箱),仅对已填写的邮箱做唯一约束
db.execute(
"CREATE UNIQUE INDEX IF NOT EXISTS idx_users_email ON users(email COLLATE NOCASE) "
"WHERE email IS NOT NULL"
)
db.execute("CREATE INDEX IF NOT EXISTS idx_logs_client_ip ON request_logs(client_ip, created_at DESC)")
@@ -151,6 +167,7 @@ def init_db():
"default_trial_days": "365",
"default_initial_credits": "100",
"registration_daily_limit": "30",
"email_verification_enabled": "1",
"api_tip_enabled": "1",
"api_tip_author": "ucmao",
"api_tip_website": "https://github.com/ucmao/media-parser",
+66
View File
@@ -0,0 +1,66 @@
"""可选的 SMTP 发信模块:未配置 SMTP_HOST 时整体处于关闭状态,不影响任何现有功能。"""
import os
import smtplib
import ssl
from email.message import EmailMessage
from email.utils import formataddr, make_msgid
from flask import current_app
def load_smtp_config():
"""从环境变量读取 SMTP 配置,供 create_app 写入 app.config。"""
port_raw = (os.getenv("SMTP_PORT") or "").strip()
security = (os.getenv("SMTP_SECURITY") or "").strip().lower()
try:
port = int(port_raw) if port_raw else 0
except ValueError:
port = 0
if security not in {"ssl", "starttls", "none"}:
security = "ssl" if port in (0, 465) else "starttls"
if not port:
port = {"ssl": 465, "starttls": 587, "none": 25}[security]
user = (os.getenv("SMTP_USER") or "").strip()
return {
"SMTP_HOST": (os.getenv("SMTP_HOST") or "").strip(),
"SMTP_PORT": port,
"SMTP_SECURITY": security,
"SMTP_USER": user,
"SMTP_PASSWORD": os.getenv("SMTP_PASSWORD") or "",
"SMTP_FROM": (os.getenv("SMTP_FROM") or "").strip() or user,
"SMTP_FROM_NAME": (os.getenv("SMTP_FROM_NAME") or "").strip() or "Media Parser",
}
def smtp_configured():
config = current_app.config
return bool(config.get("SMTP_HOST") and config.get("SMTP_FROM"))
def send_mail(to_address, subject, body):
"""同步发送纯文本邮件,失败时抛出异常由调用方处理。"""
config = current_app.config
message = EmailMessage()
message["Subject"] = subject
message["From"] = formataddr((config.get("SMTP_FROM_NAME") or "Media Parser", config["SMTP_FROM"]))
message["To"] = to_address
message["Message-ID"] = make_msgid()
message.set_content(body)
host, port, security = config["SMTP_HOST"], config["SMTP_PORT"], config["SMTP_SECURITY"]
context = ssl.create_default_context()
if security == "ssl":
client = smtplib.SMTP_SSL(host, port, timeout=15, context=context)
else:
client = smtplib.SMTP(host, port, timeout=15)
try:
if security == "starttls":
client.starttls(context=context)
if config.get("SMTP_USER"):
client.login(config["SMTP_USER"], config.get("SMTP_PASSWORD") or "")
client.send_message(message)
finally:
try:
client.quit()
except Exception:
pass
+15 -4
View File
@@ -119,10 +119,12 @@ def settings():
"pool_size": manager.pool_size,
"fetch_per_second": round(1 / manager.fetch_interval),
}
from src.mailer import smtp_configured
return render_template(
"admin/settings.html",
settings=settings,
proxy_options=proxy_options,
smtp_configured=smtp_configured(),
active_nav="settings",
)
@@ -152,6 +154,11 @@ def update_settings():
for name in ("global_api_enabled", "homepage_enabled", "demo_enabled", "registration_enabled", "api_tip_enabled"):
set_setting(name, "1" if request.form.get(name) else "0")
set_setting("default_user_qps", _positive_int(request.form.get("default_user_qps"), 2))
# 未配置 SMTP 时开关为 disabled、不随表单提交,此时保留原值,避免被误改为关闭
if "email_verification_field" in request.form:
from src.mailer import smtp_configured
if smtp_configured():
set_setting("email_verification_enabled", "1" if request.form.get("email_verification_enabled") else "0")
if "registration_daily_limit" in request.form:
set_setting("registration_daily_limit", _non_negative_int(request.form.get("registration_daily_limit"), 30, 100000))
@@ -391,14 +398,18 @@ def cookies_canary():
# 3. 客户账号 (Users)
# ----------------------------------------------------------------------
# 客户列表搜索与"全选批量操作"共用同一条件,保证批量操作的范围与列表所见一致
USER_SEARCH_CLAUSE = "(u.username LIKE ? OR u.email LIKE ? OR u.register_ip LIKE ? OR u.last_login_ip LIKE ?)"
@bp.get("/users")
@admin_required
def users():
db = get_db()
users_where, users_params = [], []
if u_q := request.args.get("users_q", "").strip():
users_where.append("(u.username LIKE ? OR u.register_ip LIKE ? OR u.last_login_ip LIKE ?)")
users_params.extend([f"%{u_q}%", f"%{u_q}%", f"%{u_q}%"])
users_where.append(USER_SEARCH_CLAUSE)
users_params.extend([f"%{u_q}%"] * USER_SEARCH_CLAUSE.count("?"))
if u_role := request.args.get("users_role", "").strip():
users_where.append("u.role = ?")
users_params.append(u_role)
@@ -550,8 +561,8 @@ def batch_users():
if select_mode == "all":
users_where, users_params = [], []
if u_q := (request.form.get("users_q") or request.form.get("q") or "").strip():
users_where.append("u.username LIKE ?")
users_params.append(f"%{u_q}%")
users_where.append(USER_SEARCH_CLAUSE)
users_params.extend([f"%{u_q}%"] * USER_SEARCH_CLAUSE.count("?"))
if u_role := (request.form.get("users_role") or request.form.get("role") or "").strip():
users_where.append("u.role = ?")
users_params.append(u_role)
+24
View File
@@ -131,6 +131,27 @@
新注册账号默认初始参数
</div>
<div class="divide-y divide-slate-100 text-sm">
<div class="py-3 first:pt-0 last:pb-0 flex items-center justify-between gap-4">
<div>
<div class="font-medium text-slate-900">注册需邮箱验证</div>
{% if smtp_configured %}
<div class="text-xs text-slate-500 mt-0.5">已配置 SMTP({{ config.get('SMTP_HOST') }}),开启后新用户需填写邮箱并输入验证码完成注册</div>
{% else %}
<div class="text-xs text-amber-600 mt-0.5">未配置 SMTP(环境变量 SMTP_HOST 等),当前仅用户名 + 密码注册;配置后重启即可启用</div>
{% endif %}
</div>
<div class="shrink-0">
<input type="hidden" name="email_verification_field" value="1">
<label class="switch-toggle" title="{{ '切换注册邮箱验证' if smtp_configured else '需先配置 SMTP' }}">
<input type="checkbox" name="email_verification_enabled"
{{ 'checked' if smtp_configured and settings.get('email_verification_enabled', '1') == '1' }}
{{ '' if smtp_configured else 'disabled' }}
onchange="triggerSettingsSubmit(this)">
<span class="switch-slider"></span>
</label>
</div>
</div>
<div class="py-3 first:pt-0 last:pb-0 flex items-center justify-between gap-4">
<div>
<div class="font-medium text-slate-900">每日注册上限</div>
@@ -783,6 +804,9 @@
if (name === 'ip_blacklist') {
return 'IP 黑名单已更新';
}
if (name === 'email_verification_enabled') {
return el.checked ? '已开启注册邮箱验证' : '已关闭注册邮箱验证';
}
if (name === 'registration_daily_limit') {
return '每日注册上限已更新';
}
+10 -4
View File
@@ -19,7 +19,7 @@
<!-- 筛选与搜索工具栏 -->
<form method="get" action="{{ url_for('admin.users') }}" class="table-toolbar table-filter-form mb-5">
<div class="flex flex-wrap items-center gap-2.5">
<input type="text" name="users_q" placeholder="搜索用户名/IP..." value="{{ users_table.query_params.q }}" class="filter-input-text px-3 py-1.5 text-xs rounded-lg border border-slate-200 focus:outline-none focus:border-brand focus:ring-2 focus:ring-brand/20 transition w-48 bg-white">
<input type="text" name="users_q" placeholder="搜索用户名/邮箱/IP..." value="{{ users_table.query_params.q }}" class="filter-input-text px-3 py-1.5 text-xs rounded-lg border border-slate-200 focus:outline-none focus:border-brand focus:ring-2 focus:ring-brand/20 transition w-48 bg-white">
<select name="users_role" onchange="this.form.submit()" class="px-3 py-1.5 text-xs rounded-lg border border-slate-200 focus:outline-none focus:border-brand focus:ring-2 focus:ring-brand/20 transition bg-white text-slate-700">
<option value="">全部角色</option>
<option value="admin" {{ 'selected' if request.args.get('users_role') == 'admin' }}>超级管理员</option>
@@ -92,7 +92,7 @@
<input type="checkbox" class="row-checkbox rounded border-slate-300 text-brand focus:ring-brand/20 cursor-pointer" value="{{ user['id'] }}">
</td>
<td class="px-3 py-3 font-mono text-slate-400 font-medium">#{{ user['id'] }}</td>
<td class="px-3 py-3 whitespace-nowrap font-bold text-slate-900 max-w-[160px] truncate" title="{{ user['username'] }}{% if user['register_ip'] %}&#10;注册 IP:{{ user['register_ip'] }}{% endif %}">{{ user['username'] }}</td>
<td class="px-3 py-3 whitespace-nowrap font-bold text-slate-900 max-w-[160px] truncate" title="{{ user['username'] }}{% if user['email'] %}&#10;邮箱:{{ user['email'] }}{% endif %}{% if user['register_ip'] %}&#10;注册 IP:{{ user['register_ip'] }}{% endif %}">{{ user['username'] }}</td>
<td class="px-3 py-3 whitespace-nowrap text-center">
<span class="px-2 py-0.5 rounded-full text-[11px] font-semibold {{ 'bg-indigo-50 text-brand border border-indigo-200' if user['role']=='admin' else 'bg-slate-100 text-slate-600 border border-slate-200' }}">
{{ '管理员' if user['role']=='admin' else '客户' }}
@@ -133,7 +133,7 @@
<div class="flex items-center justify-end gap-1.5">
{% if user['role'] != 'admin' %}
<button type="button" class="px-2.5 py-1 rounded-lg text-xs font-semibold border border-slate-200 bg-white text-slate-700 hover:bg-slate-50 transition shadow-xs"
onclick="openAdminEditUserModal({{ user['id'] }}, {{ user['username']|tojson|forceescape }}, {{ user['credits'] if user['credits'] is not none else -1 }}, {{ user['qps_limit'] or 2 }}, '{{ format_local_date(user['expires_at']) if user['expires_at'] else '' }}')">
onclick="openAdminEditUserModal({{ user['id'] }}, {{ user['username']|tojson|forceescape }}, {{ user['credits'] if user['credits'] is not none else -1 }}, {{ user['qps_limit'] or 2 }}, '{{ format_local_date(user['expires_at']) if user['expires_at'] else '' }}', {{ (user['email'] or '')|tojson|forceescape }})">
编辑
</button>
<div class="action-dropdown relative inline-block text-left" data-dropdown="user-{{ user['id'] }}">
@@ -294,6 +294,11 @@
<input type="text" id="admin-edit-user-username" disabled class="w-full px-3.5 py-2 text-sm rounded-lg border border-slate-200 bg-slate-50 text-slate-500 font-bold">
</div>
<div class="space-y-1">
<label class="block text-xs font-semibold text-slate-700">邮箱</label>
<input type="text" id="admin-edit-user-email" disabled class="w-full px-3.5 py-2 text-sm rounded-lg border border-slate-200 bg-slate-50 text-slate-500">
</div>
<div class="space-y-1">
<div class="flex items-center justify-between">
<label class="text-xs font-semibold text-slate-700">剩余积分</label>
@@ -404,12 +409,13 @@ function unformatThousands(val) {
return String(val).replace(/,/g, '').trim();
}
function openAdminEditUserModal(userId, username, credits, qps, expires) {
function openAdminEditUserModal(userId, username, credits, qps, expires, email) {
const m = document.getElementById('admin-edit-user-modal-backdrop');
const form = document.getElementById('admin-edit-user-form');
if (!m || !form) return;
form.action = '/admin/users/' + userId;
document.getElementById('admin-edit-user-username').value = username;
document.getElementById('admin-edit-user-email').value = email || '未绑定';
const unlimCreditsCb = document.getElementById('admin-edit-user-unlimited-credits');
const creditsInput = document.getElementById('admin-edit-user-credits');
+46 -1
View File
@@ -25,8 +25,22 @@
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
<div class="space-y-1.5">
<label class="block text-xs font-semibold text-slate-700">用户名</label>
<input name="username" minlength="3" maxlength="40" placeholder="请输入用户名" autocomplete="username" required autofocus class="w-full px-3.5 py-2.5 text-sm rounded-lg border border-slate-200 focus:outline-none focus:border-brand focus:ring-2 focus:ring-brand/20 transition bg-white text-slate-900">
<input name="username" minlength="3" maxlength="40" placeholder="请输入用户名" autocomplete="username" required autofocus value="{{ form_username|default('') }}" class="w-full px-3.5 py-2.5 text-sm rounded-lg border border-slate-200 focus:outline-none focus:border-brand focus:ring-2 focus:ring-brand/20 transition bg-white text-slate-900">
</div>
{% if email_required %}
<div class="space-y-1.5">
<label class="block text-xs font-semibold text-slate-700">邮箱</label>
<input id="register-email" name="email" type="email" maxlength="254" placeholder="请输入常用邮箱" autocomplete="email" required value="{{ form_email|default('') }}" class="w-full px-3.5 py-2.5 text-sm rounded-lg border border-slate-200 focus:outline-none focus:border-brand focus:ring-2 focus:ring-brand/20 transition bg-white text-slate-900">
</div>
<div class="space-y-1.5">
<label class="block text-xs font-semibold text-slate-700">邮箱验证码</label>
<div class="flex gap-2">
<input name="email_code" inputmode="numeric" pattern="[0-9]{6}" maxlength="6" placeholder="6 位数字验证码" autocomplete="one-time-code" required class="flex-1 min-w-0 px-3.5 py-2.5 text-sm rounded-lg border border-slate-200 focus:outline-none focus:border-brand focus:ring-2 focus:ring-brand/20 transition bg-white text-slate-900">
<button type="button" id="send-code-btn" onclick="sendEmailCode(this)" class="shrink-0 w-28 h-[42px] rounded-lg border border-brand text-brand hover:bg-indigo-50 disabled:opacity-50 disabled:cursor-not-allowed text-xs font-bold transition">获取验证码</button>
</div>
<p id="send-code-hint" class="text-[11px] text-slate-500 min-h-[1em]"></p>
</div>
{% endif %}
<div class="space-y-1.5">
<label class="block text-xs font-semibold text-slate-700">设置密码</label>
<div class="password-group relative">
@@ -67,6 +81,37 @@
</main>
<script>
{% if email_required %}
async function sendEmailCode(btn) {
const email = document.getElementById('register-email').value.trim();
const hint = document.getElementById('send-code-hint');
const setHint = (text, ok) => {
hint.textContent = text;
hint.className = 'text-[11px] min-h-[1em] ' + (ok ? 'text-emerald-600' : 'text-red-600');
};
if (!email) { setHint('请先填写邮箱', false); return; }
btn.disabled = true;
const body = new FormData();
body.append('csrf_token', document.querySelector('input[name="csrf_token"]').value);
body.append('email', email);
let wait = 0;
try {
const resp = await fetch('{{ url_for("auth.register_send_code") }}', { method: 'POST', body, credentials: 'same-origin' });
const data = await resp.json().catch(() => ({}));
setHint(data.message || (resp.ok ? '验证码已发送' : '发送失败,请稍后再试'), resp.ok && data.succ);
if (resp.ok && data.succ) wait = data.resend_after || 60;
else if (resp.status === 429) wait = 60;
} catch (e) {
setHint('网络异常,请稍后再试', false);
}
if (!wait) { btn.disabled = false; return; }
const timer = setInterval(() => {
if (wait <= 0) { clearInterval(timer); btn.disabled = false; btn.textContent = '重新获取'; return; }
btn.textContent = wait + 's 后重试';
wait -= 1;
}, 1000);
}
{% endif %}
function togglePasswordVisibility(btn) {
const group = btn.closest('.password-group');
if (!group) return;
+158
View File
@@ -0,0 +1,158 @@
import os
import re
import sqlite3
import tempfile
import unittest
from unittest.mock import patch
from app import create_app
from src.db import get_db, set_setting
SMTP_CONFIG = {"SMTP_HOST": "smtp.example.test", "SMTP_PORT": 465, "SMTP_SECURITY": "ssl",
"SMTP_USER": "bot@example.test", "SMTP_PASSWORD": "x", "SMTP_FROM": "bot@example.test"}
class EmailRegistrationTest(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
self.db_path = os.path.join(self.temp_dir.name, "test.db")
def tearDown(self):
self.temp_dir.cleanup()
def make_app(self, smtp=False, path=None):
config = {"TESTING": True, "SECRET_KEY": "test-secret", "DATABASE": path or self.db_path,
"SMTP_HOST": "", "SMTP_FROM": ""}
if smtp:
config.update(SMTP_CONFIG)
self.app = create_app(config)
self.client = self.app.test_client()
return self.app
def csrf(self):
with self.client.session_transaction() as session:
session["csrf_token"] = "test-csrf"
return "test-csrf"
def register(self, username, email=None, code=None, ip="198.51.100.1"):
data = {"csrf_token": self.csrf(), "username": username,
"password": "password123", "confirm_password": "password123"}
if email is not None:
data["email"] = email
if code is not None:
data["email_code"] = code
return self.client.post("/auth/register", data=data, environ_base={"REMOTE_ADDR": ip})
def send_code(self, email, ip="198.51.100.1"):
sent = {}
def fake_send(to, subject, body):
sent["to"] = to
sent["code"] = re.search(r"\d{6}", body).group(0)
with patch("src.mailer.send_mail", side_effect=fake_send):
resp = self.client.post("/auth/register/send-code", environ_base={"REMOTE_ADDR": ip},
data={"csrf_token": self.csrf(), "email": email})
return resp, sent.get("code")
def user(self, username):
with self.app.app_context():
return get_db().execute("SELECT * FROM users WHERE username=?", (username,)).fetchone()
def test_without_smtp_registration_is_unchanged(self):
self.make_app(smtp=False)
page = self.client.get("/auth/register").get_data(as_text=True)
self.assertNotIn('name="email"', page)
self.assertEqual(self.register("plain_user").status_code, 302)
self.assertIsNone(self.user("plain_user")["email"])
resp = self.client.post("/auth/register/send-code", data={"csrf_token": self.csrf(), "email": "a@b.cn"})
self.assertEqual(resp.status_code, 404)
def test_with_smtp_requires_valid_code(self):
self.make_app(smtp=True)
self.assertIn('name="email_code"', self.client.get("/auth/register").get_data(as_text=True))
resp = self.register("no_code", email="no_code@example.com", code="")
self.assertEqual(resp.status_code, 200)
self.assertIsNone(self.user("no_code"))
resp, code = self.send_code("Mail_User@Example.com")
self.assertEqual(resp.status_code, 200, resp.get_json())
wrong = "000000" if code != "000000" else "111111"
self.assertEqual(self.register("mail_user", email="mail_user@example.com", code=wrong).status_code, 200)
self.assertIsNone(self.user("mail_user"))
self.assertEqual(self.register("mail_user", email="mail_user@example.com", code=code).status_code, 302)
self.assertEqual(self.user("mail_user")["email"], "mail_user@example.com")
with self.app.app_context():
self.assertIsNone(get_db().execute("SELECT 1 FROM email_verification_codes").fetchone())
# 同一邮箱不能再次获取验证码或注册
resp, _ = self.send_code("mail_user@example.com", ip="198.51.100.2")
self.assertEqual(resp.status_code, 400)
def test_code_locks_after_max_attempts(self):
self.make_app(smtp=True)
_, code = self.send_code("lock@example.com")
wrong = "000000" if code != "000000" else "111111"
for i in range(5):
self.register(f"lock_{i}", email="lock@example.com", code=wrong, ip=f"198.51.100.{10 + i}")
self.assertEqual(self.register("lock_ok", email="lock@example.com", code=code, ip="198.51.100.30").status_code, 200)
self.assertIsNone(self.user("lock_ok"))
def test_send_failure_reports_error_and_discards_code(self):
self.make_app(smtp=True)
with patch("src.mailer.send_mail", side_effect=OSError("boom")):
resp = self.client.post("/auth/register/send-code", data={"csrf_token": self.csrf(), "email": "f@example.com"})
self.assertEqual(resp.status_code, 502)
with self.app.app_context():
self.assertIsNone(get_db().execute("SELECT 1 FROM email_verification_codes").fetchone())
def test_admin_can_disable_email_verification(self):
self.make_app(smtp=True)
with self.app.app_context():
set_setting("email_verification_enabled", "0")
get_db().commit()
self.assertEqual(self.register("toggle_off").status_code, 302)
def test_admin_user_list_searches_and_shows_email(self):
self.make_app(smtp=False)
self.client.post("/auth/setup", data={"csrf_token": self.csrf(), "username": "admin",
"password": "password123", "confirm_password": "password123"})
self.client.post("/auth/login", data={"csrf_token": self.csrf(), "username": "admin", "password": "password123"})
with self.app.app_context():
db = get_db()
db.execute("INSERT INTO users(username,password_hash,created_at,email) VALUES('has_mail','x','2026-01-01','found@example.com')")
db.execute("INSERT INTO users(username,password_hash,created_at) VALUES('no_mail','x','2026-01-01')")
db.commit()
page = self.client.get("/admin/users?users_q=found@").get_data(as_text=True)
self.assertIn("has_mail", page)
self.assertNotIn("no_mail", page)
self.assertIn("found@example.com", page)
self.assertIn('id="admin-edit-user-email"', page)
def test_legacy_database_gets_nullable_unique_email(self):
path = os.path.join(self.temp_dir.name, "legacy.db")
conn = sqlite3.connect(path)
conn.executescript(
"CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL UNIQUE COLLATE NOCASE,"
" password_hash TEXT NOT NULL, role TEXT NOT NULL DEFAULT 'user', active INTEGER NOT NULL DEFAULT 1,"
" expires_at TEXT, qps_limit INTEGER NOT NULL DEFAULT 2, credits INTEGER NOT NULL DEFAULT 100, created_at TEXT NOT NULL);"
"INSERT INTO users(username,password_hash,created_at) VALUES('old_a','x','2026-01-01T00:00:00+00:00');"
"INSERT INTO users(username,password_hash,created_at) VALUES('old_b','x','2026-01-01T00:00:00+00:00');"
)
conn.commit()
conn.close()
self.make_app(smtp=False, path=path)
self.make_app(smtp=False, path=path) # 二次启动迁移需幂等
conn = sqlite3.connect(path)
self.assertIn("email", {row[1] for row in conn.execute("PRAGMA table_info(users)")})
self.assertEqual(conn.execute("SELECT COUNT(*) FROM users WHERE email IS NULL").fetchone()[0], 2)
conn.execute("UPDATE users SET email='dup@example.com' WHERE username='old_a'")
with self.assertRaises(sqlite3.IntegrityError):
conn.execute("UPDATE users SET email='DUP@example.com' WHERE username='old_b'")
conn.close()
if __name__ == "__main__":
unittest.main()