feat: 开放API授权相关

This commit is contained in:
2025-05-20 14:40:39 +08:00
parent a7b752636d
commit 1d723b31a5
16 changed files with 577 additions and 1 deletions

View File

@@ -0,0 +1,31 @@
<?php
declare (strict_types = 1);
namespace app\openapi\middleware;
use OAuth2\OAuth2;
use OAuth2\OAuth2ServerException;
use oauth\OAuthStorage;
class Auth
{
/**
* 处理请求
*
* @param \think\Request $request
* @param \Closure $next
* @return Response
*/
public function handle($request, \Closure $next)
{
try {
$oauth = new OAuth2(new OAuthStorage);
$token = $oauth->getBearerToken();
$oauth->verifyAccessToken($token);
} catch (OAuth2ServerException $e) {
return error('Unauthorized', $e->sendHttpResponse(), 401);
}
return $next($request);
}
}