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?

推荐答案

我假设您想要使用机器对机器身份验证(无用户交互)

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

  • 在内核中定义客户端凭据别名为中间件
  • Add middleware to route
  • Send request

定义http内核的客户端凭据中间件

类别\App\Http\Kernel:

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

在路由上定义中间件

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

Class \App\Http\Controllers\ApiTestController:

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

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

为了简单起见,我使用Postman,很容易用Postman发送测试请求(www.getpostman.com)

Set authorization to OAuth 2.0, image: Postman authentication

将访问令牌URL、客户端id、客户端机密和授权类型设置为"客户端凭据",图像:Postman OAuth Fields

postman 创建一个令牌,并将其附加到URL或标头,在本例中为标头

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

答复:

{
  "data": "hey"
}

Laravel相关问答推荐

在 Laravel 中排序的集合在 Vue 中突然不再排序

嵌套数组请求的 Laravel 验证,其中如果整数之一为零,则另一个应大于零

Laravel AppServiceProvider 中的代码在通过 DeployHQ 部署时停止 Composer 和构建

未定义防护 [api] 的授权驱动程序 [api]

如何保存/重定向 Laravel Artisan 命令的输出?

如何在 vue 模板中引用 laravel csrf 字段

在 laravel 4.2 中,用户 'root'@'localhost' 的 Laravel 访问被拒绝(使用密码:YES)

Laravel 5.4 中的自定义助手类

Laravel + SQLite = SQLSTATE[HY000]一般错误:8次try 写入只读数据库

如何创建具有关系的 Eloquent 模型?

如何在 Laravel 5.5 中为选定的请求类设置自定义响应

php Laravel-遇到格式不正确的数值(在字符串上)

Cookie::forget 不工作 laravel 5.1

Web 服务器如何处理请求?

laravel 4:更新行时如何从列的当前值中减一?

如何将 Facebook PHP SDK 与 Laravel 5.4 集成?

脚本 php artisan clear-compiled 处理 pre-update-cmd 事件返回错误(Laravel 4.1 升级)

laravel - 无法在控制器构造函数中获取会话

如何访问 Laravel 集合中的第 n 个项目?

错误:找不到模块 'webpack/lib/rules/DescriptionDataMatcherRulePlugin' 需要堆栈: