Files
CardAuth/backend/app/Services/TurnstileService.php
T
root e90ade2ba0 feat: 登录有效期24小时 + Cloudflare Turnstile 人机验证
- jwt_ttl 7200 → 86400 (24h)
- 管理登录/代理登录/代理注册 接入 Turnstile
- 前端 TurnstileWidget 组件, 未完成验证不可提交
- 密钥通过 env TURNSTILE_SITE_KEY/SECRET_KEY 配置
2026-10-11 15:09:54 +08:00

49 lines
1.3 KiB
PHP

<?php
namespace App\Services;
/**
* Cloudflare Turnstile 人机验证
*/
class TurnstileService
{
/**
* 校验 Turnstile token
* 未配置 secret 时跳过校验(便于本地开发)
*/
public static function verify(?string $token, ?string $remoteIp = null): bool
{
$secret = (string) config('app.turnstile.secret_key', '');
if ($secret === '') {
return true;
}
if ($token === null || $token === '') {
return false;
}
$payload = [
'secret' => $secret,
'response' => $token,
];
if ($remoteIp) {
$payload['remoteip'] = $remoteIp;
}
$ch = curl_init('https://challenges.cloudflare.com/turnstile/v0/siteverify');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($payload),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded'],
]);
$body = curl_exec($ch);
curl_close($ch);
if ($body === false) {
return false;
}
$json = json_decode($body, true);
return !empty($json['success']);
}
}