feat: 用户白名单免验证码登录

- Settings 新增 SMS_CODE_WHITELIST 配置(逗号分隔手机号)
- login_with_sms 中白名单手机号跳过验证码校验
- 方便内部测试和演示账号使用
This commit is contained in:
小鱼开发
2026-05-21 16:32:09 +08:00
parent 44ec2dceb7
commit 2cece72abe
3 changed files with 21 additions and 2 deletions
+2
View File
@@ -71,6 +71,8 @@ SMS_APP_ID=your-sms-app-id
SMS_SECRET_KEY=your-16-24-32-byte-aes-key
SMS_BASE_URL=https://bjksmtn.b2m.cn/inter/sendSingleSMS
# SMS_EXTENDED_CODE= # 扩展码(选填)
# 免验证码登录白名单(逗号分隔),名单内的手机号登录时跳过验证码校验
# SMS_CODE_WHITELIST=13800138000,13900139000
# === 日志配置 ===
# 生产环境建议 INFO
+15
View File
@@ -134,6 +134,10 @@ class Settings(BaseSettings):
SMS_EXTENDED_CODE: str | None = Field(
default=None, description="B2M 短信平台扩展码(选填)"
)
SMS_CODE_WHITELIST: str = Field(
default="",
description="免验证码登录白名单(逗号分隔的手机号,如 13800138000,13900139000",
)
@@ -175,6 +179,17 @@ class Settings(BaseSettings):
"""是否使用 Redis"""
return bool(self.REDIS_HOST)
@property
def sms_code_whitelist_set(self) -> set[str]:
"""免验证码登录白名单(去重、去空格)"""
if not self.SMS_CODE_WHITELIST:
return set()
return {
mobile.strip()
for mobile in self.SMS_CODE_WHITELIST.split(",")
if mobile.strip()
}
@lru_cache
def get_settings() -> Settings:
+4 -2
View File
@@ -18,6 +18,7 @@ from typing import Any
import httpx
from sqlalchemy.ext.asyncio import AsyncSession
from app.config import get_settings
from app.core.redis_client import get_redis_client
from app.core.security import (
create_access_token,
@@ -188,8 +189,9 @@ async def login_with_sms(
5. 创建/覆盖设备记录
6. 签发双 Token
"""
# 1. 校验验证码
if not await verify_sms_code(mobile, code):
# 1. 校验验证码(白名单内的手机号跳过校验)
settings = get_settings()
if mobile not in settings.sms_code_whitelist_set and not await verify_sms_code(mobile, code):
raise ValueError("验证码错误或已过期")
# 2. 查询用户(不再自动注册)