feat: 人机验证可开关 + 登录注册入口白名单
- 系统设置新增「人机验证」页: 总开关 + 按入口勾选 - 客户端 verify/activate/check-update 永不校验 Turnstile - 公开 GET /api/public/turnstile-config 供登录页按需展示组件
This commit is contained in:
1 parent
217c083544
commit
7c85ecdd2d
9 files changed
+262
-21
No files matched your search
@@ -1,13 +1,99 @@
|
||||
<?php
|
||||
namespace App\Services;
|
||||
|
||||
use App\Core\Database;
|
||||
|
||||
/**
|
||||
* Cloudflare Turnstile 人机验证
|
||||
* 可通过系统配置开关,并按登录/注册入口白名单启用
|
||||
*/
|
||||
class TurnstileService
|
||||
{
|
||||
/** 可选启用的入口标识 */
|
||||
public const ENDPOINTS = [
|
||||
'admin_login' => '管理员登录',
|
||||
'agent_login' => '代理登录',
|
||||
'agent_register'=> '代理注册',
|
||||
];
|
||||
|
||||
/**
|
||||
* 读取配置缓存
|
||||
* @return array{enabled:int, endpoints:array<int,string>, secret:string, site_key:string}
|
||||
*/
|
||||
public static function config(): array
|
||||
{
|
||||
static $cache = null;
|
||||
if ($cache !== null) {
|
||||
return $cache;
|
||||
}
|
||||
|
||||
$enabled = true;
|
||||
$endpoints = array_keys(self::ENDPOINTS);
|
||||
try {
|
||||
$db = Database::getInstance();
|
||||
$rows = $db->fetchAll(
|
||||
"SELECT `key`, `value` FROM {$db->table('configs')} WHERE `key` IN ('turnstile_enabled','turnstile_endpoints')"
|
||||
);
|
||||
$map = [];
|
||||
foreach ($rows as $r) {
|
||||
$map[$r['key']] = $r['value'];
|
||||
}
|
||||
if (isset($map['turnstile_enabled'])) {
|
||||
$enabled = (string) $map['turnstile_enabled'] === '1';
|
||||
}
|
||||
if (isset($map['turnstile_endpoints']) && trim($map['turnstile_endpoints']) !== '') {
|
||||
$parts = array_filter(array_map('trim', explode(',', $map['turnstile_endpoints'])));
|
||||
$endpoints = array_values(array_intersect(array_keys(self::ENDPOINTS), $parts));
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
// 配置表不可用时保持默认:启用且仅登录注册
|
||||
}
|
||||
|
||||
$cache = [
|
||||
'enabled' => $enabled ? 1 : 0,
|
||||
'endpoints' => $endpoints,
|
||||
'secret' => (string) config('app.turnstile.secret_key', ''),
|
||||
'site_key' => (string) config('app.turnstile.site_key', ''),
|
||||
];
|
||||
return $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* 全局是否启用人机验证
|
||||
*/
|
||||
public static function isEnabled(): bool
|
||||
{
|
||||
$c = self::config();
|
||||
return $c['enabled'] === 1 && $c['secret'] !== '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定入口是否需要人机验证
|
||||
* @param string $endpoint admin_login|agent_login|agent_register
|
||||
*/
|
||||
public static function requiredFor(string $endpoint): bool
|
||||
{
|
||||
if (!self::isEnabled()) {
|
||||
return false;
|
||||
}
|
||||
$c = self::config();
|
||||
return in_array($endpoint, $c['endpoints'], true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验 Turnstile token
|
||||
* @param string $endpoint 入口标识
|
||||
*/
|
||||
public static function verifyFor(string $endpoint, ?string $token, ?string $remoteIp = null): bool
|
||||
{
|
||||
if (!self::requiredFor($endpoint)) {
|
||||
return true;
|
||||
}
|
||||
return self::verify($token, $remoteIp);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验 Turnstile token(强制)
|
||||
* 未配置 secret 时跳过校验(便于本地开发)
|
||||
*/
|
||||
public static function verify(?string $token, ?string $remoteIp = null): bool
|
||||
|
||||
Reference in new issue
Block a user