danilopolani/laravel-fusionauth-jwt
Composer 安装命令:
composer require danilopolani/laravel-fusionauth-jwt
包简介
Laravel Auth guard for FusionAuth JWT
README 文档
README
Implement an Auth guard for FusionAuth JWTs in Laravel.
It ships with also a middleware to check against the user role.
Installation
You can install the package via composer:
composer require danilopolani/laravel-fusionauth-jwt
Then publish its config file:
php artisan vendor:publish --tag=fusionauth-jwt-config
Configuration
There are a few notable configuration options for the package.
| Key | Type | Description |
|---|---|---|
domain |
String | Your FusionAuth domain, e.g. auth.myapp.com or sandbox.fusionauth.io. |
client_id |
String | The Client ID of the current application. |
client_secret |
String | The Client Secret of the current application. |
issuers |
Array | A list of authorized issuers for the incoming JWT. |
audience |
String | Null | The ID/Name of the authorized audience. If null, the Client ID will be used. |
supported_algs |
Array | The supported algorithms of the JWT. Supported: RS256 and HS256. |
default_role |
String | Null | The default role to be checked if you're using the CheckRole middleware. |
Usage
To start protecting your APIs you need to add the Guard and the Auth Provider to your config/auth.php configuration file:
'guards' => [ // ... 'fusionauth' => [ 'driver' => 'fusionauth', 'provider' => 'fusionauth', ], ], 'providers' => [ // ... 'fusionauth' => [ 'driver' => 'fusionauth', ], ],
Then you can use the auth:fusionauth guard to protect your endpoints; you can apply it to a group or a single route:
// app\Http\Kernel.php protected $middlewareGroups = [ 'api' => [ 'auth:fusionauth', // ... ], ]; // or routes/api.php Route::get('users', [UserController::class, 'index']) ->middleware('auth:fusionauth');
Now requests for those endpoints will check if the given JWT (given as Bearer token) is valid.
To retrieve the current logged in user - or to check if it's logged in - you can use the usual Auth facade methods, specifying the fusionauth guard:
Auth::guard('fusionauth')->check(); /** @var \DaniloPolani\FusionAuthJwt\FusionAuthJwtUser $user */ $user = Auth::guard('fusionauth')->user();
Role middleware
The package ships with a handy middleware to check for user role (stored in the roles key).
You can apply it on a middleware group inside the Kernel.php or to specific routes:
// app\Http\Kernel.php protected $middlewareGroups = [ 'api' => [ 'auth:fusionauth', \DaniloPolani\FusionAuthJwt\Http\Middleware\CheckRole::class, // ... ], ]; // or routes/api.php Route::get('users', [UserController::class, 'index']) ->middleware(['auth:fusionauth', 'fusionauth.role']);
By default the middleware will check that the current user has the default_role specified in the configuration file, but you can use as well a specific role, different from the default:
// routes/api.php Route::get('users', [UserController::class, 'index']) ->middleware(['auth:fusionauth', 'fusionauth.role:admin']);
For more complex cases we suggest you to take a look on how the CheckRole middleware is written (using the RoleManager class) and write your own.
Usage in tests
When you need to test your endpoints in Laravel, you can take advantage of the actingAs method to set the current logged in user.
You can pass any property you want to the FusionAuthJwtUser class, like email, user etc. Take a look at this example where we specify the user roles:
use DaniloPolani\FusionAuthJwt\FusionAuthJwtUser; $this ->actingAs( new FusionAuthJwtUser([ 'roles' => ['user', 'admin'], ]), 'fusionauth', ) ->get('/api/users') ->assertOk();
If you need to set the authenticated user outside HTTP testing (therefore you can't use actingAs()), you can use the setUser() method of the Auth facade:
use DaniloPolani\FusionAuthJwt\FusionAuthJwtUser; use Illuminate\Support\Facades\Auth; Auth::guard('fusionauth')->setUser( new FusionAuthJwtUser([ 'roles' => ['user', 'admin'], ]) );
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email danilo.polani@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.
danilopolani/laravel-fusionauth-jwt 适用场景与选型建议
danilopolani/laravel-fusionauth-jwt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20.29k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2021 年 07 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「danilopolani」 「laravel-fusionauth-jwt」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 danilopolani/laravel-fusionauth-jwt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 danilopolani/laravel-fusionauth-jwt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 danilopolani/laravel-fusionauth-jwt 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
统计信息
- 总下载量: 20.29k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-07-30