Laravel/Passport 客户端授权令牌模式下如何获取当前令牌所属的client
Laravel10
项目中使用了laravel/passport
授权认证,配置正常后开始使用。
由于使用的是客户端授权令牌模式,因此,客户端请求/oauth/token
,模拟如下:
use Illuminate\Support\Facades\Http;
$response = Http::asForm()->post('http://passport-app.test/oauth/token', [
'grant_type' => 'client_credentials',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
'scope' => 'your-scope',
]);
return $response->json()['access_token'];
客户端拿到access_token
后,使用Bearer token
携带该access_token
请求某个由client
中间件值守的路由:
Route::get('/test', function (){
$psr = (new PsrHttpFactory(
new Psr17Factory,
new Psr17Factory,
new Psr17Factory,
new Psr17Factory
))->createRequest(\request());
$server = app()->make(ResourceServer::class);
$psr = $server->validateAuthenticatedRequest($psr);
$oauth_client_id = $psr->getAttribute('oauth_client_id');
$client = Client::query()->find($oauth_client_id);
dd($client);
})->middleware('client');
通过这种方法可以拿到客户端信息,但是总觉得是我不了解laravel/passport
的功能,有没有更简单的办法?
正在努力学习的小逗比 [ dobeen.net ]
已解决,GPT的解决方案:
$oauth_client_id = $request->attributes->get('oauth_client_id');