From 8a4a5991d4dd4fbba52a47b1f4ee12f982580d03 Mon Sep 17 00:00:00 2001 From: digood-dev Date: Thu, 26 Mar 2026 16:27:58 +0800 Subject: [PATCH] Update LogtoClient.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复用户资料中姓名为中文名时,响应的base64文本格式标准不一的问题 --- src/LogtoClient.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/LogtoClient.php b/src/LogtoClient.php index 615545e..0407679 100644 --- a/src/LogtoClient.php +++ b/src/LogtoClient.php @@ -93,7 +93,23 @@ function getIdToken(): ?string */ function getIdTokenClaims(): IdTokenClaims { - return new IdTokenClaims(...json_decode(base64_decode(explode('.', $this->getIdToken())[1]), true)); + $playload = explode('.', $this->getIdToken())[1] ?? null; + if (empty($playload)) throw new \Exception("Invalid Playload data"); + + // 解决编码标准不一致的问题: (RFC 4648 §5) 与标准 Base64 (RFC 4648 §4) + + // 1. 替换字符 (- -> +, _ -> /) + $data = strtr($playload, '-_', '+/'); + + // 2. 补全填充 (=) + $mod = strlen($data) % 4; + if ($mod > 0) { + $data .= str_repeat('=', 4 - $mod); + } + + $playloadDecode = base64_decode($data, true); + + return new IdTokenClaims(...json_decode($playloadDecode, true)); } /**