jwt中间件验证
This commit is contained in:
parent
750b566f1e
commit
938caef787
@ -63,7 +63,7 @@ class GoogleAdsCustomers
|
||||
$option['refresh_token'] = $thirdUser->access_token;
|
||||
// dump($option);
|
||||
$allRootAccounts = $this->googleAdsAccountService->runGetAccountHierarchy($option);
|
||||
// dump($allAccounts);
|
||||
// dump($allRootAccounts);
|
||||
foreach ($allRootAccounts as $rootAccountId => $accounts) {
|
||||
// dump($rootAccountId, $accounts);
|
||||
foreach ($accounts as $account) {
|
||||
|
@ -1,58 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace app\middleware;
|
||||
|
||||
use Webman\MiddlewareInterface;
|
||||
use Webman\Http\Request;
|
||||
use Webman\Http\Response;
|
||||
use app\rpc\client\AuthRpcClient;
|
||||
use Firebase\JWT\JWT as FJWT;
|
||||
use Firebase\JWT\Key;
|
||||
use Exception;
|
||||
|
||||
class Jwt implements MiddlewareInterface
|
||||
{
|
||||
// 签名密钥
|
||||
private const SIGNING_KEY = '83OP5jf43875jK7';
|
||||
|
||||
public function process(Request $request, callable $handler): Response
|
||||
{
|
||||
// 获取 Authorization 头
|
||||
// $authorization = $request->header('Authorization', '');
|
||||
// if (empty($authorization) || strpos($authorization, 'Bearer ') !== 0) {
|
||||
// return response(['code' => 1, 'msg' => '缺少 Authorization 头或格式无效'], 200);
|
||||
// }
|
||||
$authorization = $request->header('Authorization', '');
|
||||
if (empty($authorization) || strpos($authorization, 'Bearer ') !== 0) {
|
||||
return Json([
|
||||
'code' => 1,
|
||||
'msg' => '缺少 Authorization 头或格式无效',
|
||||
'data' => []
|
||||
]);
|
||||
}
|
||||
|
||||
// 提取 JWT token
|
||||
// $jwtToken = substr($authorization, 7);
|
||||
// dump($jwtToken);
|
||||
// return Json([
|
||||
// 'code' => 0,
|
||||
// 'msg' => 'ok',
|
||||
// 'data' => $jwtToken,
|
||||
// ]);
|
||||
$jwtToken = substr($authorization, 7);
|
||||
|
||||
try {
|
||||
// 调用 AuthRpcClient 进行 JWT 验证
|
||||
// $authRpcClient = new AuthRpcClient();
|
||||
// $response = $authRpcClient->validateJwtToken($jwtToken);
|
||||
//
|
||||
// // 如果验证不通过,返回错误消息
|
||||
// if ($response['result'] !== 'JWT_VERIFY_OK') {
|
||||
// return response([
|
||||
// 'code' => 1,
|
||||
// 'msg' => $this->getErrorMessage($response['result']),
|
||||
// ], 200);
|
||||
// }
|
||||
//
|
||||
// // 如果验证通过,将用户数据 (claims) 存入请求属性,供后续使用
|
||||
// $request = $request->withAttribute('user', $response['claims']);
|
||||
$jwtClaims = ['uid' =>'8d2f93fd-af63-4d46-90ab-69f366a19332' ];
|
||||
$request->jwtClaims = $jwtClaims;
|
||||
// 使用 firebase/php-jwt 解码并验证 JWT
|
||||
// $decoded = FJWT::decode($jwtToken, new Key(self::SIGNING_KEY, 'HS512'), $headers = new stdClass()); // 使用 HMAC-SHA512 算法进行验证
|
||||
$decoded = FJWT::decode($jwtToken, new Key(self::SIGNING_KEY, 'HS512'));
|
||||
|
||||
// // 如果返回了新 token,将其添加到响应头 X-New-Token 中
|
||||
$response = $handler($request);
|
||||
// if (!empty($response['new_token'])) {
|
||||
// $response = $response->withHeader('X-New-Token', $response['new_token']);
|
||||
// }
|
||||
// 将解码后的数据(即 claims)存入请求对象,后续可以访问
|
||||
$request->jwtClaims = (array)$decoded;
|
||||
|
||||
return $response;
|
||||
// 验证 JWT Token
|
||||
// dump((array)$decoded);
|
||||
// return Json([
|
||||
// 'code' => 0,
|
||||
// 'msg' => 'JWT 验证成功',
|
||||
// 'data' => []
|
||||
// ]);
|
||||
|
||||
// 继续处理请求
|
||||
return $handler($request);
|
||||
} catch (Exception $e) {
|
||||
return response(['code' => 1, 'msg' => 'JWT 验证失败: ' . $e->getMessage()], 200);
|
||||
|
||||
return Json([
|
||||
'code' => 1,
|
||||
'msg' => $e->getMessage(),
|
||||
'data' => []
|
||||
]);
|
||||
|
||||
// return response(['code' => 1, 'msg' => 'JWT 验证失败: ' . $e->getMessage()], 200);
|
||||
}
|
||||
}
|
||||
|
||||
@ -73,4 +76,4 @@ class Jwt implements MiddlewareInterface
|
||||
return '未知错误';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ class UpdateGoogleAdsTask
|
||||
}
|
||||
);
|
||||
|
||||
new Crontab('* * */6 * * *', function () {
|
||||
new Crontab('* */20 * * * *', function () {
|
||||
dump(date('Y-m-d H:i:s') . '更新' . GoogleAdsAssetRelations::IMAGEASSET . '开始');
|
||||
Event::emit(GoogleAdsAssetRelations::IMAGEASSET, []);
|
||||
});
|
||||
@ -88,7 +88,7 @@ class UpdateGoogleAdsTask
|
||||
}
|
||||
);
|
||||
|
||||
new Crontab('0 */15 * * * *', function () {
|
||||
new Crontab('0 */3 * * * *', function () {
|
||||
dump(date('Y-m-d H:i:s') . '更新' . GoogleAdsCustomers::CUSTOMERADD . '开始');
|
||||
Event::emit(GoogleAdsCustomers::CUSTOMERADD,[]);
|
||||
}
|
||||
|
@ -42,7 +42,8 @@
|
||||
"webman/think-orm": "^1.1",
|
||||
"phpoffice/phpspreadsheet": "^3.6",
|
||||
"grpc/grpc": "^1.38",
|
||||
"google/protobuf": "^4.29"
|
||||
"google/protobuf": "^4.29",
|
||||
"firebase/php-jwt": "^6.10"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-event": "For better performance. "
|
||||
|
111
composer.lock
generated
111
composer.lock
generated
@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "3b7d410388f353ce39d707fa20dd1931",
|
||||
"content-hash": "2436c19ae2c59b32e3a555bf9791ac20",
|
||||
"packages": [
|
||||
{
|
||||
"name": "brick/math",
|
||||
@ -1961,16 +1961,16 @@
|
||||
},
|
||||
{
|
||||
"name": "nesbot/carbon",
|
||||
"version": "2.72.5",
|
||||
"version": "2.72.6",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/briannesbitt/Carbon.git",
|
||||
"reference": "afd46589c216118ecd48ff2b95d77596af1e57ed"
|
||||
"reference": "1e9d50601e7035a4c61441a208cb5bed73e108c5"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/afd46589c216118ecd48ff2b95d77596af1e57ed",
|
||||
"reference": "afd46589c216118ecd48ff2b95d77596af1e57ed",
|
||||
"url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/1e9d50601e7035a4c61441a208cb5bed73e108c5",
|
||||
"reference": "1e9d50601e7035a4c61441a208cb5bed73e108c5",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@ -1990,7 +1990,7 @@
|
||||
"doctrine/orm": "^2.7 || ^3.0",
|
||||
"friendsofphp/php-cs-fixer": "^3.0",
|
||||
"kylekatarnls/multi-tester": "^2.0",
|
||||
"ondrejmirtes/better-reflection": "*",
|
||||
"ondrejmirtes/better-reflection": "<6",
|
||||
"phpmd/phpmd": "^2.9",
|
||||
"phpstan/extension-installer": "^1.0",
|
||||
"phpstan/phpstan": "^0.12.99 || ^1.7.14",
|
||||
@ -2003,10 +2003,6 @@
|
||||
],
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "3.x-dev",
|
||||
"dev-2.x": "2.x-dev"
|
||||
},
|
||||
"laravel": {
|
||||
"providers": [
|
||||
"Carbon\\Laravel\\ServiceProvider"
|
||||
@ -2016,6 +2012,10 @@
|
||||
"includes": [
|
||||
"extension.neon"
|
||||
]
|
||||
},
|
||||
"branch-alias": {
|
||||
"dev-2.x": "2.x-dev",
|
||||
"dev-master": "3.x-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
@ -2064,7 +2064,7 @@
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2024-06-03T19:18:41+00:00"
|
||||
"time": "2024-12-27T09:28:11+00:00"
|
||||
},
|
||||
{
|
||||
"name": "nikic/fast-route",
|
||||
@ -2291,16 +2291,16 @@
|
||||
},
|
||||
{
|
||||
"name": "phpoffice/phpspreadsheet",
|
||||
"version": "3.6.0",
|
||||
"version": "3.7.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/PHPOffice/PhpSpreadsheet.git",
|
||||
"reference": "bce5db99872f9613121c3ad033c43318a3789396"
|
||||
"reference": "2fc12fdc58d39297c7b8c72d65b37a1a25d65ab5"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/PHPOffice/PhpSpreadsheet/zipball/bce5db99872f9613121c3ad033c43318a3789396",
|
||||
"reference": "bce5db99872f9613121c3ad033c43318a3789396",
|
||||
"url": "https://api.github.com/repos/PHPOffice/PhpSpreadsheet/zipball/2fc12fdc58d39297c7b8c72d65b37a1a25d65ab5",
|
||||
"reference": "2fc12fdc58d39297c7b8c72d65b37a1a25d65ab5",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@ -2389,9 +2389,9 @@
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/PHPOffice/PhpSpreadsheet/issues",
|
||||
"source": "https://github.com/PHPOffice/PhpSpreadsheet/tree/3.6.0"
|
||||
"source": "https://github.com/PHPOffice/PhpSpreadsheet/tree/3.7.0"
|
||||
},
|
||||
"time": "2024-12-08T15:04:12+00:00"
|
||||
"time": "2024-12-27T05:34:47+00:00"
|
||||
},
|
||||
{
|
||||
"name": "phpoption/phpoption",
|
||||
@ -3118,12 +3118,12 @@
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"thanks": {
|
||||
"url": "https://github.com/symfony/contracts",
|
||||
"name": "symfony/contracts"
|
||||
},
|
||||
"branch-alias": {
|
||||
"dev-main": "3.5-dev"
|
||||
},
|
||||
"thanks": {
|
||||
"name": "symfony/contracts",
|
||||
"url": "https://github.com/symfony/contracts"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
@ -3519,12 +3519,12 @@
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"thanks": {
|
||||
"url": "https://github.com/symfony/contracts",
|
||||
"name": "symfony/contracts"
|
||||
},
|
||||
"branch-alias": {
|
||||
"dev-main": "3.5-dev"
|
||||
},
|
||||
"thanks": {
|
||||
"name": "symfony/contracts",
|
||||
"url": "https://github.com/symfony/contracts"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
@ -4137,16 +4137,16 @@
|
||||
},
|
||||
{
|
||||
"name": "workerman/redis-queue",
|
||||
"version": "v1.2.0",
|
||||
"version": "v1.2.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/walkor/redis-queue.git",
|
||||
"reference": "7b6aee70d69e5c9427c0411d85f8398027831b42"
|
||||
"reference": "75dbf7ed2ea228c45dc0df82c0fea35879b715d0"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/walkor/redis-queue/zipball/7b6aee70d69e5c9427c0411d85f8398027831b42",
|
||||
"reference": "7b6aee70d69e5c9427c0411d85f8398027831b42",
|
||||
"url": "https://api.github.com/repos/walkor/redis-queue/zipball/75dbf7ed2ea228c45dc0df82c0fea35879b715d0",
|
||||
"reference": "75dbf7ed2ea228c45dc0df82c0fea35879b715d0",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@ -4168,22 +4168,22 @@
|
||||
"homepage": "http://www.workerman.net",
|
||||
"support": {
|
||||
"issues": "https://github.com/walkor/redis-queue/issues",
|
||||
"source": "https://github.com/walkor/redis-queue/tree/v1.2.0"
|
||||
"source": "https://github.com/walkor/redis-queue/tree/v1.2.1"
|
||||
},
|
||||
"time": "2024-02-28T07:00:03+00:00"
|
||||
"time": "2025-01-02T09:21:45+00:00"
|
||||
},
|
||||
{
|
||||
"name": "workerman/webman-framework",
|
||||
"version": "v1.6.9",
|
||||
"version": "v1.6.13",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/walkor/webman-framework.git",
|
||||
"reference": "c328d94f3aa50a06a59689656a24a580a7b51dc1"
|
||||
"reference": "193cc0a157b56e6bcdc594b202e4d95404dde407"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/walkor/webman-framework/zipball/c328d94f3aa50a06a59689656a24a580a7b51dc1",
|
||||
"reference": "c328d94f3aa50a06a59689656a24a580a7b51dc1",
|
||||
"url": "https://api.github.com/repos/walkor/webman-framework/zipball/193cc0a157b56e6bcdc594b202e4d95404dde407",
|
||||
"reference": "193cc0a157b56e6bcdc594b202e4d95404dde407",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@ -4235,24 +4235,34 @@
|
||||
"source": "https://github.com/walkor/webman-framework",
|
||||
"wiki": "https://doc.workerman.net/"
|
||||
},
|
||||
"time": "2024-12-11T12:41:18+00:00"
|
||||
"time": "2025-01-02T10:40:52+00:00"
|
||||
},
|
||||
{
|
||||
"name": "workerman/workerman",
|
||||
"version": "v4.2.1",
|
||||
"version": "v5.0.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/walkor/workerman.git",
|
||||
"reference": "cafb5a43d93d7d30a16b32a57948581cca993562"
|
||||
"reference": "df67931cc602675e95fb2eace90948fa642a3024"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/walkor/workerman/zipball/cafb5a43d93d7d30a16b32a57948581cca993562",
|
||||
"reference": "cafb5a43d93d7d30a16b32a57948581cca993562",
|
||||
"url": "https://api.github.com/repos/walkor/workerman/zipball/df67931cc602675e95fb2eace90948fa642a3024",
|
||||
"reference": "df67931cc602675e95fb2eace90948fa642a3024",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.0"
|
||||
"ext-json": "*",
|
||||
"php": ">=8.1"
|
||||
},
|
||||
"conflict": {
|
||||
"ext-swow": "<v1.0.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"guzzlehttp/guzzle": "^7.0",
|
||||
"mockery/mockery": "^1.6",
|
||||
"pestphp/pest": "2.x-dev",
|
||||
"phpstan/phpstan": "1.11.x-dev"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-event": "For better performance. "
|
||||
@ -4260,7 +4270,7 @@
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Workerman\\": "./"
|
||||
"Workerman\\": "src"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
@ -4271,22 +4281,24 @@
|
||||
{
|
||||
"name": "walkor",
|
||||
"email": "walkor@workerman.net",
|
||||
"homepage": "http://www.workerman.net",
|
||||
"homepage": "https://www.workerman.net",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"description": "An asynchronous event driven PHP framework for easily building fast, scalable network applications.",
|
||||
"homepage": "http://www.workerman.net",
|
||||
"homepage": "https://www.workerman.net",
|
||||
"keywords": [
|
||||
"asynchronous",
|
||||
"event-loop"
|
||||
"event-loop",
|
||||
"framework",
|
||||
"http"
|
||||
],
|
||||
"support": {
|
||||
"email": "walkor@workerman.net",
|
||||
"forum": "http://wenda.workerman.net/",
|
||||
"forum": "https://www.workerman.net/questions",
|
||||
"issues": "https://github.com/walkor/workerman/issues",
|
||||
"source": "https://github.com/walkor/workerman",
|
||||
"wiki": "http://doc.workerman.net/"
|
||||
"wiki": "https://www.workerman.net/doc/workerman/"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
@ -4298,7 +4310,7 @@
|
||||
"type": "patreon"
|
||||
}
|
||||
],
|
||||
"time": "2024-11-24T11:45:37+00:00"
|
||||
"time": "2024-12-30T15:42:54+00:00"
|
||||
}
|
||||
],
|
||||
"packages-dev": [],
|
||||
@ -4308,7 +4320,8 @@
|
||||
"prefer-stable": true,
|
||||
"prefer-lowest": false,
|
||||
"platform": {
|
||||
"php": ">=8.1"
|
||||
"php": ">=8.1",
|
||||
"ext-grpc": "*"
|
||||
},
|
||||
"platform-dev": {},
|
||||
"plugin-api-version": "2.6.0"
|
||||
|
@ -41,6 +41,7 @@ Route::group('/googleads', function () {
|
||||
// Route::post('/get', [AdController::class, 'getAdStatus']);
|
||||
});
|
||||
})->middleware([
|
||||
app\middleware\Jwt::class,
|
||||
app\middleware\OauthCheck::class,
|
||||
]);
|
||||
|
||||
@ -52,6 +53,7 @@ Route::group('/googleads', function () {
|
||||
// Route::post('/get', [AdController::class, 'getCampaignStatus']);
|
||||
});
|
||||
})->middleware([
|
||||
app\middleware\Jwt::class,
|
||||
app\middleware\OauthCheck::class,
|
||||
]);
|
||||
Route::group('/adgroup', function () {
|
||||
@ -62,11 +64,13 @@ Route::group('/googleads', function () {
|
||||
// Route::post('/get', [AdController::class, 'getGroupStatus']);
|
||||
});
|
||||
})->middleware([
|
||||
app\middleware\Jwt::class,
|
||||
app\middleware\OauthCheck::class,
|
||||
]);
|
||||
Route::group('/asset', function () {
|
||||
Route::post('/list', [AdController::class, 'listAssets']);
|
||||
})->middleware([
|
||||
app\middleware\Jwt::class,
|
||||
app\middleware\OauthCheck::class,
|
||||
]);
|
||||
Route::group('/customer', function () {
|
||||
@ -75,6 +79,7 @@ Route::group('/googleads', function () {
|
||||
Route::post('/list_resource', [CustomerController::class, 'accessibleCustomers']);
|
||||
Route::post('/list_tree', [CustomerController::class, 'accountHierarchy']);
|
||||
})->middleware([
|
||||
app\middleware\Jwt::class,
|
||||
app\middleware\OauthCheck::class,
|
||||
]);
|
||||
|
||||
@ -86,8 +91,8 @@ Route::group('/googleads', function () {
|
||||
Route::post('/refresh_token_test', [OAuthController::class, 'testRefreshToken']);
|
||||
Route::post('/refresh_token_revoke', [OAuthController::class, 'revokeRefreshToken']);
|
||||
})->middleware([
|
||||
app\middleware\Jwt::class,
|
||||
]);
|
||||
app\middleware\Jwt::class,
|
||||
]);
|
||||
});
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user