This commit is contained in:
gr
2026-09-24 09:45:44 +08:00
parent dbf474ddd7
commit bd22e5f476
38 changed files with 19152 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace app\mcp\service;
use think\Request;
/**
* AI 授权令牌:安全随机数生成,只保存 SHA-256;固定前缀便于密钥扫描。
*/
class TokenService
{
public const PREFIX = 'zyt_ai_';
public static function generate(): string
{
return self::PREFIX . bin2hex(random_bytes(32));
}
public static function hash(string $token): string
{
return hash('sha256', $token);
}
public static function displayPrefix(string $token): string
{
return substr($token, 0, 12);
}
/** 从 Authorization: Bearer 头取令牌;格式不对返回空字符串 */
public static function fromRequest(Request $request): string
{
$header = (string) $request->header('authorization', '');
if (!preg_match('/^\s*Bearer\s+(\S+)\s*$/i', $header, $m)) {
return '';
}
$token = $m[1];
return (str_starts_with($token, self::PREFIX) && strlen($token) === strlen(self::PREFIX) + 64) ? $token : '';
}
}