I'm in a similar situation but I used password grant client to issue token to user during authentication and I need to used the personal access token for user to generate an access token for their self to be used in their 3rd party applications. You can do a workaround by updating the scope.I also need to verify the user if the 2fa has been pass.
in your
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',
]);
}
next step is in your Authentication process where you send the password grant type
// 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);
then in your 2FA Verification process
// 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);
Take note about the scopes, when you refresh the token, you can only obtain identical or narrower scopes than the original access token. If you attempt to get a scope that was not provided by the original access token, you will get an error.
- patricus
answer here: https://stackoverflow.com/a/45856634/11537130
take note that this will be generate a new token with new scope