1.优化ws参数格式auth-token和user-token
This commit is contained in:
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace app\common\library\admin;
|
||||
|
||||
use app\common\service\GameWebSocketAuthHelper;
|
||||
use Webman\Http\Request;
|
||||
|
||||
final class WebSocketConfigHelper
|
||||
@@ -41,32 +42,97 @@ final class WebSocketConfigHelper
|
||||
}
|
||||
|
||||
/**
|
||||
* 在基础 ws_url 上拼接握手鉴权 Query:
|
||||
* - 后台用:auth_token + admin_ws_token(可观测全量主题,无 user_id 过滤)
|
||||
* - H5 用:调用方传 user_token;与 auth_token 一起拼上去
|
||||
* 在基础 ws_url 上拼接握手鉴权 Query(**输出统一为连字符**:auth-token、user-token、admin-ws-token)。
|
||||
*
|
||||
* @param array{auth_token?: string, user_token?: string, admin_ws_token?: string} $tokens
|
||||
* $tokens 的键可使用标准名或旧名下划线别名(auth_token 等),拼到 URL 时一律转为连字符。
|
||||
*
|
||||
* @param array<string, string> $tokens 如 auth-token、user-token、admin-ws-token
|
||||
* @param array<string, string> $extraQuery 如 device_id、lang
|
||||
*/
|
||||
public static function appendTokensToWsUrl(string $wsUrl, array $tokens): string
|
||||
public static function appendTokensToWsUrl(string $wsUrl, array $tokens, array $extraQuery = []): string
|
||||
{
|
||||
$wsUrl = trim($wsUrl);
|
||||
if ($wsUrl === '') {
|
||||
return $wsUrl;
|
||||
}
|
||||
$pairs = [];
|
||||
foreach (['auth_token', 'user_token', 'admin_ws_token'] as $key) {
|
||||
$val = isset($tokens[$key]) && is_string($tokens[$key]) ? trim($tokens[$key]) : '';
|
||||
if ($val !== '') {
|
||||
$pairs[] = $key . '=' . rawurlencode($val);
|
||||
|
||||
$authVal = self::pickTokenFromMap($tokens, [
|
||||
GameWebSocketAuthHelper::QUERY_AUTH_TOKEN,
|
||||
'auth_token',
|
||||
'authToken',
|
||||
]);
|
||||
$userVal = self::pickTokenFromMap($tokens, [
|
||||
GameWebSocketAuthHelper::QUERY_USER_TOKEN,
|
||||
'user_token',
|
||||
'userToken',
|
||||
'token',
|
||||
]);
|
||||
$adminVal = self::pickTokenFromMap($tokens, [
|
||||
GameWebSocketAuthHelper::QUERY_ADMIN_WS_TOKEN,
|
||||
'admin_ws_token',
|
||||
'adminWsToken',
|
||||
]);
|
||||
|
||||
$canonical = [];
|
||||
if ($authVal !== '') {
|
||||
$canonical[GameWebSocketAuthHelper::QUERY_AUTH_TOKEN] = $authVal;
|
||||
}
|
||||
if ($userVal !== '') {
|
||||
$canonical[GameWebSocketAuthHelper::QUERY_USER_TOKEN] = $userVal;
|
||||
}
|
||||
if ($adminVal !== '') {
|
||||
$canonical[GameWebSocketAuthHelper::QUERY_ADMIN_WS_TOKEN] = $adminVal;
|
||||
}
|
||||
|
||||
foreach ($extraQuery as $k => $v) {
|
||||
if (!is_string($k) || $k === '') {
|
||||
continue;
|
||||
}
|
||||
if (!is_scalar($v)) {
|
||||
continue;
|
||||
}
|
||||
$s = trim((string) $v);
|
||||
if ($s !== '') {
|
||||
$canonical[$k] = $s;
|
||||
}
|
||||
}
|
||||
if ($pairs === []) {
|
||||
|
||||
if ($canonical === []) {
|
||||
return $wsUrl;
|
||||
}
|
||||
|
||||
$pairs = [];
|
||||
foreach ($canonical as $key => $val) {
|
||||
$pairs[] = $key . '=' . rawurlencode($val);
|
||||
}
|
||||
$sep = str_contains($wsUrl, '?') ? '&' : '?';
|
||||
|
||||
return $wsUrl . $sep . implode('&', $pairs);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, string> $tokens
|
||||
* @param list<string> $aliases
|
||||
*/
|
||||
private static function pickTokenFromMap(array $tokens, array $aliases): string
|
||||
{
|
||||
foreach ($aliases as $key) {
|
||||
if (!isset($tokens[$key])) {
|
||||
continue;
|
||||
}
|
||||
$val = $tokens[$key];
|
||||
if (!is_string($val)) {
|
||||
continue;
|
||||
}
|
||||
$s = trim($val);
|
||||
if ($s !== '') {
|
||||
return $s;
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
private static function isLoopbackWsUrl(string $url): bool
|
||||
{
|
||||
$host = parse_url($url, PHP_URL_HOST);
|
||||
|
||||
Reference in New Issue
Block a user