Neewd
Neewd

Reputation: 93

Laravel : Client_Credentials don't work

In my Laravel website, I have to make a cron jobs who will retrieve some data, then update my database. From the docs of Laravel I thought to the machine-to-machine authentication from Laravel Passport.

So I jumped into this, installation and so on. https://laravel.com/docs/5.6/passport#client-credentials-grant-tokens

I followed every single step from the docs, and from the example I found in the internet, I always got the following message

{
    "message": "Unauthenticated."
}

I added the client_credentials middleware in my Kernel.php

protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    ...
    'authAdmin' => \App\Http\Middleware\AuthAdmin::class,
    'client' => \Laravel\Passport\Http\Middleware\CheckClientCredentials::class,
];

Then for the basics, I created a ApiTestController with a test methods who gonna be triggered for my route.

I have put into my api.php routes this following lines :

/*
  Public API Routes
*/
Route::group(['prefix' => 'v1'], function() {
  ... 
  Route::get('/test', 'ApiTestController@test')->middleware('client');
});

Basically my method just return "it works".

I have created a passport client via the passport:client artisan command, then use it through postman to get my Bearer token.

But it doesn't work :(

Someone can help me ?

Upvotes: 2

Views: 1556

Answers (1)

David H
David H

Reputation: 11

I realise this reply is very late however I've experienced the same issue in Passport 5.7.

To fix this issue I had to add the code below to my .htaccess. This is because I was testing in postman using https rather than http however http would return unauthenticated.

After adding the code you should be able to test your http api using an http url in postman.

Maybe it helps someone.

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

Upvotes: 1

Related Questions