feat: 更新验证码请求模型,添加 Google reCAPTCHA 和 Cloudflare Turnstile 验证功能
refactor: 修改用户状态字段类型,优化用户模型 fix: 修复启动服务的错误提示信息 refactor: 统一认证依赖,替换为 AuthRequired docs: 添加用户会话刷新接口
This commit is contained in:
5
service/captcha/__init__.py
Normal file
5
service/captcha/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
class CaptchaRequestBase(BaseModel):
|
||||
token: str
|
||||
secret: str
|
||||
@@ -1,6 +1,8 @@
|
||||
import aiohttp
|
||||
|
||||
async def verify_captcha(token: str, secret_key: str) -> bool:
|
||||
from . import CaptchaRequestBase
|
||||
|
||||
async def verify_captcha(request: CaptchaRequestBase) -> bool:
|
||||
"""
|
||||
验证 Google reCAPTCHA v2/v3 的 token 是否有效。
|
||||
|
||||
@@ -13,10 +15,7 @@ async def verify_captcha(token: str, secret_key: str) -> bool:
|
||||
:rtype: bool
|
||||
"""
|
||||
verify_url = "https://www.google.com/recaptcha/api/siteverify"
|
||||
payload = {
|
||||
'secret': secret_key,
|
||||
'response': token
|
||||
}
|
||||
payload = request.model_dump()
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(verify_url, data=payload) as response:
|
||||
|
||||
26
service/captcha/turnstile.py
Normal file
26
service/captcha/turnstile.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import aiohttp
|
||||
|
||||
from . import CaptchaRequestBase
|
||||
|
||||
async def verify_captcha(request: CaptchaRequestBase) -> bool:
|
||||
"""
|
||||
验证 Turnstile 的 token 是否有效。
|
||||
|
||||
:param token: 用户提交的 Turnstile token
|
||||
:type token: str
|
||||
:param secret_key: Turnstile 的密钥
|
||||
:type secret_key: str
|
||||
|
||||
:return: 如果验证成功返回 True,否则返回 False
|
||||
:rtype: bool
|
||||
"""
|
||||
verify_url = "https://challenges.cloudflare.com/turnstile/v0/siteverify"
|
||||
payload = request.model_dump()
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(verify_url, data=payload) as response:
|
||||
if response.status != 200:
|
||||
return False
|
||||
|
||||
result = await response.json()
|
||||
return result.get('success', False)
|
||||
Reference in New Issue
Block a user