我处于类似情况,但我在身份验证期间使用密码授予客户端向用户颁发令牌,我需要使用个人访问令牌为用户生成访问令牌,以便他们自己在他们的第 3 方应用程序中使用.您可以通过更新范围来解决问题。如果 2fa 已通过,我还需要验证用户。
在你的
AuthServiceProvider.php
public function boot()
{
$this->registerPolicies();
Passport::routes(function ($router) {
$router->forAccessTokens();
$router->forPersonalAccessTokens();
$router->forTransientTokens(); // register the transient token. skip if all routes are enabled
});
// Add scope to verify the user
// take note that here 2 scope for this due to refresh token scope
Passport::tokensCan([
'2fa-pass' => '2FA Pass',
'2fa-not-pass' => '2FA Pass',
]);
}
下一步是在您发送密码授权类型的身份验证过程中
// I'm using route::dispatch to do a proxy request
// you can use Guzzle if you want
$request->request->add([
'grant_type' => 'password',
'client_id' => 'client-id',
client_secret' => 'client-secret',
'username' => $credentials['email'],
'password' => $credentials['password'],
'scope' => '2fa-not-pass 2fa-pass' // take note that I added the two scope here
]);
$tokenRequest = Request::create('/oauth/token', 'POST');
$response = \Route::dispatch($tokenRequest);
然后在您的 2FA 验证过程中
// your process of verifying the 2FA code
// after that you need to update the scope by doing a refresh token
$request->request->add([
'grant_type' => 'refresh_token',
'refresh_token' => $request->input('refresh_token'),
'client_id' => 'client-id',
'client_secret' => 'client-secret',
'scope' => '2fa-pass' // I removed the 2fa-not-pass in refreshing the token
]);
$tokenRequest = Request::create('/oauth/token', 'POST');
$response = \Route::dispatch($tokenRequest);
注意范围,刷新令牌时,只能获得与原始访问令牌相同或更窄的范围。如果您尝试获取原始访问令牌未提供的范围,则会收到错误消息。
- 帕特里克斯
在这里回答:https://stackoverflow.com/a/45856634/11537130
请注意,这将生成一个具有新范围的新令牌