werk365/jwtauthroles
Composer 安装命令:
composer require werk365/jwtauthroles
包简介
Made to use fusionauth users in Laravel using JWT. Possible to either use pem keys directly or use the jwks endpoint.
关键字:
README 文档
README
Made to use JWTs from an external identity provider in Laravel. Tested with Fusionauth, but should be quite general purpose.
With this package you can validate the incoming JWT, and create an authenticated user that has to roles specified in the JWT for further (route based) authentication using a role middleware that is included.
.
Take a look at contributing.md to see a to do list.
Installation
Via Composer
$ composer require werk365/jwtauthroles
Publish config and migration
$ php artisan vendor:publish --provider="Werk365\JwtAuthRoles\JwtAuthRolesServiceProvider"
Migrations are only needed if you want to either cache the JWKs or store the user, this can be configured in the config. It's possible to use this package without storing anything related to it in the database at all.
Run migration
$ php artisan migrate
Usage
In your AuthServiceProvider modify boot()
use Illuminate\Support\Facades\Auth; use Werk365\JwtAuthRoles\JwtAuthRoles; public function boot() { $this->registerPolicies(); Auth::viaRequest('jwt', function ($request) { return JwtAuthRoles::authUser($request); }); }
Then either change one of your guards in config/auth.php to use the jwt driver and jwt_users provider, or add a new guard
use Werk365\JwtAuthRoles\Models\JwtUser; 'guards' => [ // ... 'jwt' => [ 'driver' => 'jwt', 'provider' => 'jwt_users', 'hash' => false, ], ], // ... 'providers' => [ // ... 'jwt_users' => [ 'driver' => 'eloquent', 'model' => JwtUser::class, ], ],
Now you can use the JWT guard in your routes, for example on a group:
Route::group(['middleware' => ['auth:jwt']], function () { // Routes can go here });
You can also use the RolesMiddelware to do role-based authentication on a route like this:
// single role Route::get('/exammple', function(){ return "example"; })->middleware('role:example'); // multiple roles Route::get('/exammples', function(){ return "examples"; })->middleware('role:example|second|third|etc');
To make the authenticated user actually useful, the JwtUser model extends the User model. This means that you can define any relations in the User model, and then use them for the authenticated user.
For example, add the following relationship in the default User model:
public function documents() { return $this->hasMany('App\Models\Document', 'user', 'uuid'); }
This assumes you have a Documents model where the uuid provided by your identity provider is stored in a 'user' column, this can be anything you want of course, but the local key should always be uuid.
This can then be used as follows to retrieve all documents belonging to this user:
return Auth::user()->documents;
Finally, configure the config to your needs. The default published config will validate the JWT, but not use the database. It looks like this:
<?php return [ // If enabled, stores every user in the database 'useDB' => env('FA_USE_DB', false), // Only if useDB = true // Column name in the users table where uuid should be stored.' 'userId' => env('FA_USR_ID', 'uuid'), // Only if useDB = true 'autoCreateUser' => env('FA_CREATE_USR', false), 'alg' => env('FA_ALG', 'RS256'), // Allows you to skip validation, this is potentially dangerous, // only use for testing or if the jwt has been validated by something like an api gateway 'validateJwt' => env('FA_VALIDATE', true), // Only if validateJwt = true 'cache' => [ 'enabled' => env('FA_CACHE_ENABLED', false), 'type' => env('FA_CACHE_TYPE', 'database'), ], // Only if validateJwt = true 'jwkUri' => env('JWKS_URL', 'http://localhost:9011/.well-known/jwks.json'), // Only if validateJwt = true 'pemUri' => env('PEM_URL', 'http://localhost:9011/api/jwt/public-key'), // Only if validateJwt = true // Configure to use PEM endpoint (default) or JWK 'useJwk' => env('USE_JWK', false), ];
Laravel version
Currently this package supports Laravel 8. Since we use the default User model, it expects it to be in the app\Models\User namespace. To make this package work with previous versions of Laravel, you'll only have to make a model in this namespace, besides that the package should work with any recent version.
Change log
Please see the changelog for more information on what has changed recently.
Testing
Testing is not yet implemented
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email hergen.dillema@gmail.com instead of using the issue tracker.
Credits
License
license. Please see the license file for more information.
werk365/jwtauthroles 适用场景与选型建议
werk365/jwtauthroles 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 419 次下载、GitHub Stars 达 15, 最近一次更新时间为 2020 年 07 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「jwtauthroles」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 werk365/jwtauthroles 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 werk365/jwtauthroles 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 werk365/jwtauthroles 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Alfabank REST API integration
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
Branded, diagnostic error pages (500, 403, 404, 419, 503) for Filament — native Filament UI, dark mode and translations out of the box.
Turn any PDF into a Pingen-ready A4 letter (generated address cover page + A4 normalisation + safe margins) and send it through the Pingen print & mail API. Laravel-first.
统计信息
- 总下载量: 419
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-07-09