code16/machina
Composer 安装命令:
composer require code16/machina
包简介
Machine to machine authentication for Laravel
README 文档
README
This package is a wrapper around tymons\jwt-auth, aimed at providing a simple & flexible machine-to-machine authentication for Laravel 5.5+.
Installation
composer require code16/machina
Configuration
If you want to customize some default options like the prefix used for /login and /refresh endpoints by the package, you can publish it to your application folder :
php artisan config:publish code16/machina
Then run this command, which will add a JWT_SECRET entry in your .env file:
php artisan jwt:secret
Defining machine guard
In config/auth.php :
'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'machina' => [ 'driver' => 'machina', 'provider' => Api\ClientRepository::class, ], ],
Creating a ClientRepository class
This package does not come with an opinionated way of retrieving clients, but instead provides a very simple way to adapt it to your application, by providing a class implementing Code16\Machina\ClientRepositoryInterface.
Example :
namespace App; use Code16\Machina\ClientRepositoryInterface; class ClientRepository implements ClientRepositoryInterface { public function findByKey($key) { return User::find($key); } public function findByCredentials($client, $secret) { return User::where('id', $client)->where('secret', $secret)->first(); } }
Note that here we used the standard App\User model DB to identify our client, but you can use whichever model / fields you like.
Protecting routes
Route::get('protected', 'ApiController@index')->middleware('auth:machina');
Authenticating and retrieving token
Send a POST request the /auth/login endpoint with client and secret as parameters :
{
client : "1",
secret : "x7jfajleug64hggi"
}
If the credentials are correct, the API will return a JWT token that can be used to access protected routes.
Accessing protected routes
There is two ways of passing the token along the request :
-
Passing the token in the
authorizationheader with the following string format :Bearer <token> -
Passing the token as a query parameter :
https://app.dev/protected?token=<token>
Implementing client applications
For your client applications, you can use our companion package, machina client.
code16/machina 适用场景与选型建议
code16/machina 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.25k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 03 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 code16/machina 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 code16/machina 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.25k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 12
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-03-09