Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

423
Visualizações
Why should Client Creadentials be associated with a user in Laravel Passport?

I want to use Client Credentials to authenticate client applications to access the API.

My problem is with creating client credentials. Using php artisan passport:client requires me to enter a user_id to associate the client to that user. I don't get it. Why the client application has to be associated to a user?! Or Is there another way?

passport:client command only supports creating Password Grant Clients and Personal Grant Client. I don't think that any of them is what I need.

What I really need is to create client credentials that will only be used by the client application to authorize itself to access some APIs. How to do that?

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I assume you want to use machine-to-machine authentication (no user interactions)

I would recommend to read through the docs a couple of times to get the hang of it.

I do not believe there is an specific way to create an only client credentials client, What i do is to create an personal client then change the field for personal client in the database personal_access_client 1 => 0

You could use the personal client option, as seen from the --help option

Usage:
  passport:client [options]

Options:
      --personal        Create a personal access token client
      --password        Create a password grant client
      --name[=NAME]     The name of the client
  -h, --help            Display this help message
...

php artisan passport:client --personal

output

Personal access client created successfully.
Client ID: 1
Client Secret: LbjQNxK5SQZ3pPrEBUwbkE8vaRkg8jh25Qh43HYy

You would need to use another middleware other then the default one because there is no user present when using this method

  • Define client credentials alias middleware in kernel
  • Add middleware to route
  • Send request

Define client credentials middleware to the http kernel

Class \App\Http\Kernel:

 protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'client_credentials' => \Laravel\Passport\Http\Middleware\CheckClientCredentials::class,
        //ommited
    ];

Define middleware on route

Route::get('/test', 'ApiTestController@test')->middleware('client_credentials');

Class \App\Http\Controllers\ApiTestController:

public function test() {
        return response()->json(['data' => 'hey'] );
}

From php artisan route:list

GET|HEAD  | api/test | App\Http\Controllers\ApiTestController@test   | api,client_credentials  |

Send request

Following the specified request in the documentation on client-credentials-grant-tokens

I use Postman for simplicity, easily send test request with Postman (www.getpostman.com)

Set authorization to OAuth 2.0, image: Postman authentication

Set access token URL, client id, client secret and grant type to 'Client Credentials', image: Postman OAuth Fields

Postman creates an token and appends it to URL or Header, in this case header

Accept:application/json
Authorization:Bearer eyJ0eXAiOi...KCjK0

Response:

{
  "data": "hey"
}
about 4 years ago · Santiago Trujillo Relatório

0

These answers are a little old.

You can certainly add client credentials.

php artisan passport:client --client

protected $signature = 'passport:client
        {--personal : Create a personal access token client}
        {--password : Create a password grant client}
        {--client : Create a client credentials grant client}
        {--name= : The name of the client}
        {--provider= : The name of the user provider}
        {--redirect_uri= : The URI to redirect to after authorization }
        {--user_id= : The user ID the client should be assigned to }
        {--public : Create a public client (Auth code grant type only) }';
about 4 years ago · Santiago Trujillo Relatório

0

I was gonna comment but I don't have enough reputation yet =p

You could give the command a name parameter that won't require any user input. As far as how you would get that client secret over to your client without manual intervention is where the real wizardry would come in.

php artisan passport:client --personal --name={someName}

That command would still give you:


Personal access client created successfully.
Client ID: 1
Client Secret: LbjQNxK5SQZ3pPrEBUwbkE8vaRkg8jh25Qh43HYy

as expected.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda