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
@@ -53,9 +53,10 @@ class AuthController extends Controller
|
||||
$this->error('请输入用户名或邮箱');
|
||||
}
|
||||
|
||||
// Cloudflare Turnstile 人机验证
|
||||
// Cloudflare Turnstile(按系统配置开关与入口白名单)
|
||||
$portalKey = $portal === 'agent' ? 'agent_login' : 'admin_login';
|
||||
$turnstileToken = trim((string) ($input['turnstile_token'] ?? ''));
|
||||
if (!TurnstileService::verify($turnstileToken !== '' ? $turnstileToken : null, clientIp())) {
|
||||
if (!TurnstileService::verifyFor($portalKey, $turnstileToken !== '' ? $turnstileToken : null, clientIp())) {
|
||||
$this->error('人机验证失败,请完成验证后重试');
|
||||
}
|
||||
|
||||
@@ -132,9 +133,9 @@ class AuthController extends Controller
|
||||
$this->error('两次输入的密码不一致');
|
||||
}
|
||||
|
||||
// Cloudflare Turnstile 人机验证
|
||||
// Cloudflare Turnstile(按系统配置开关与入口白名单)
|
||||
$turnstileToken = trim((string) ($input['turnstile_token'] ?? ''));
|
||||
if (!TurnstileService::verify($turnstileToken !== '' ? $turnstileToken : null, clientIp())) {
|
||||
if (!TurnstileService::verifyFor('agent_register', $turnstileToken !== '' ? $turnstileToken : null, clientIp())) {
|
||||
$this->error('人机验证失败,请完成验证后重试');
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Core\Database;
|
||||
use App\Core\Validator;
|
||||
use App\Services\InstallationService;
|
||||
use App\Services\VersionService;
|
||||
use App\Services\TurnstileService;
|
||||
|
||||
/**
|
||||
* 公开接口控制器
|
||||
@@ -746,6 +747,21 @@ class PublicController extends Controller
|
||||
$this->success(['valid' => false, 'message' => '卡密状态异常'], 'ok');
|
||||
}
|
||||
|
||||
/**
|
||||
* 人机验证公开配置(前端登录页判断是否展示组件)
|
||||
* GET /api/public/turnstile-config
|
||||
* 注意:客户端 verify/activate/check-update 等接口从不校验 Turnstile
|
||||
*/
|
||||
public function turnstileConfig(): void
|
||||
{
|
||||
$c = \App\Services\TurnstileService::config();
|
||||
$this->success([
|
||||
'enabled' => $c['enabled'] === 1 && $c['site_key'] !== '',
|
||||
'site_key' => $c['site_key'],
|
||||
'endpoints' => $c['endpoints'],
|
||||
], 'ok');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取公告列表(公开/客户端)
|
||||
* GET /api/public/announcements?target=client&project_id=1&limit=10
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -47,6 +47,9 @@ $router->group('api/public', function ($router) {
|
||||
// 公告推送(客户端/商城)
|
||||
$router->get('announcements', [PublicController::class, 'announcements']);
|
||||
|
||||
// 人机验证公开配置(登录页用;客户端激活/验证接口从不校验)
|
||||
$router->get('turnstile-config', [PublicController::class, 'turnstileConfig']);
|
||||
|
||||
// 支付方式(前台展示用)
|
||||
$router->get('payment/options', [PublicController::class, 'paymentOptions']);
|
||||
|
||||
|
||||
Reference in new issue
Block a user