Client Credentials Grant Tokens
The client credentials grant is suitable for machine-to-machine
authentication. For example, you might use this grant in a scheduled
job which is performing maintenance tasks over an API.
Before your application can issue tokens via the client credentials
grant, you will need to create a client credentials grant client. You
may do this using the --client option of the passport:client
command:
php artisan passport:client --client
Next, to use this grant type, you need to add the
CheckClientCredentials middleware to the $routeMiddleware property
of your app/Http/Kernel.php file:
use Laravel\Passport\Http\Middleware\CheckClientCredentials;
protected $routeMiddleware = [
'client' => CheckClientCredentials::class,
];
Then, attach the middleware to a route:
Route::get('/orders', function (Request $request) {
...
})->middleware('client');
To restrict access to the route to specific scopes you may provide a
comma-delimited list of the required scopes when attaching the
client middleware to the route:
Route::get('/orders', function (Request $request) {
...
})->middleware('client:check-status,your-scope');
Retrieving Tokens
To retrieve a token using this grant type, make a request to the
oauth/token endpoint:
$guzzle = new GuzzleHttp\Client;
$response = $guzzle->post('http://your-app.com/oauth/token', [
'form_params' => [
'grant_type' => 'client_credentials',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
'scope' => 'your-scope',
],
]);
return json_decode((string) $response->getBody(), true)['access_token'];