From 62150d261a5e6700d9026a391b965a22d6742cee Mon Sep 17 00:00:00 2001 From: MiAn Date: Tue, 24 Nov 2026 15:46:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(auth):=20=E4=BF=AE=E5=A4=8D=E5=B8=88?= =?UTF-8?q?=E5=82=85=E7=AB=AF=E4=B8=8A=E4=BC=A0=E8=BA=AB=E4=BB=BD=E8=AF=81?= =?UTF-8?q?=E6=8A=A5=E8=AF=B7=E5=85=88=E7=99=BB=E5=BD=95=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E4=B8=8EPHP8=E5=85=BC=E5=AE=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复 addons/cos/library/Auth.php 仅校验用户端登录态导致师傅端Token被误拒的问题,支持MasterAuth多端鉴权 - 增强 api/controller/Common.php upload 方法获取 token 支持 header 读取 - 优化 fast/Random.php uuid 方法采用RFC4122原生random_bytes实现,规避PHP8.4+下Ramsey\\Uuid可空参数弃用异常 --- server/addons/cos/library/Auth.php | 20 ++++++++++++++++++-- server/application/api/controller/Common.php | 2 +- server/extend/fast/Random.php | 9 ++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/server/addons/cos/library/Auth.php b/server/addons/cos/library/Auth.php index 3aaf5f3..640406b 100644 --- a/server/addons/cos/library/Auth.php +++ b/server/addons/cos/library/Auth.php @@ -27,11 +27,27 @@ class Auth if (in_array($module, $noNeedLogin) || ($module == 'admin' && \app\admin\library\Auth::instance()->id) || ($module != 'admin' && \app\common\library\Auth::instance()->id) + || ($module != 'admin' && \app\common\library\MasterAuth::instance()->id()) || $isModuleLogin) { return true; - } else { - return false; } + + // 若当前尚未初始化会话(如 API 模式),尝试从请求头或参数中解析 Token 校验用户端或师傅端 + if ($module == 'api' || $module != 'admin') { + $token = request()->server('HTTP_TOKEN', request()->header('token', request()->request('token', \think\Cookie::get('token', '')))); + if ($token) { + $userAuth = \app\common\library\Auth::instance(); + if ($userAuth->init($token) && $userAuth->id) { + return true; + } + $masterAuth = \app\common\library\MasterAuth::instance(); + if ($masterAuth->init($token) && $masterAuth->id() > 0) { + return true; + } + } + } + + return false; } } diff --git a/server/application/api/controller/Common.php b/server/application/api/controller/Common.php index 3be0eea..f74e45c 100755 --- a/server/application/api/controller/Common.php +++ b/server/application/api/controller/Common.php @@ -69,7 +69,7 @@ class Common extends Api public function upload() { if (!$this->auth->isLogin()) { - $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', '')); + $token = $this->request->server('HTTP_TOKEN', $this->request->header('token', $this->request->request('token', ''))); $masterAuth = \app\common\library\MasterAuth::instance(); if (!$token || !$masterAuth->init($token)) { $this->error(__('Please login first'), null, 401); diff --git a/server/extend/fast/Random.php b/server/extend/fast/Random.php index 34088b1..936c22a 100755 --- a/server/extend/fast/Random.php +++ b/server/extend/fast/Random.php @@ -97,6 +97,13 @@ class Random */ public static function uuid(): string { - return Uuid::uuid4()->toString(); + try { + $data = random_bytes(16); + $data[6] = chr(ord($data[6]) & 0x0f | 0x40); + $data[8] = chr(ord($data[8]) & 0x3f | 0x80); + return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); + } catch (\Throwable $e) { + return Uuid::uuid4()->toString(); + } } }