nomadnt/lumen-passport
Composer 安装命令:
composer require nomadnt/lumen-passport
包简介
Lumen porting of Laravel Passport
关键字:
README 文档
README
Lumen Passport
Lumen porting of Laravel Passport. The idea come from https://github.com/dusterio/lumen-passport but try to make it transparent with original laravel passport
Dependencies
- PHP >= 8.0
- Lumen >= 11.0
- Laravel Passport >= 12.0
Installation
First of all let's install Lumen Framework if you haven't already.
composer create-project --prefer-dist laravel/lumen lumen-app && cd lumen-app
Then install Lumen Passport (it will fetch Laravel Passport along):
composer require nomadnt/lumen-passport
Configuration
Generate your APP_KEY and update .env with single command
sed -i "s|\(APP_KEY=\)\(.*\)|\1$(openssl rand -base64 24)|" .env
Configure your database connection (ie to use SQLite) This is how your .env file should looking after the changes
APP_NAME=Lumen APP_ENV=local APP_KEY=<my-super-strong-api-key> APP_DEBUG=true APP_URL=http://localhost:8000 APP_TIMEZONE=UTC LOG_CHANNEL=stack LOG_SLACK_WEBHOOK_URL= DB_CONNECTION=sqlite CACHE_DRIVER=file QUEUE_CONNECTION=sync
Copy the Lumen configuration folder to your project
cp -a vendor/laravel/lumen-framework/config config
Update guards and provider section of your config/auth.php to match Passport requirements
<?php return [ ... 'guards' => [ 'api' => ['driver' => 'passport', 'provider' => 'users'] ], ... 'providers' => [ 'users' => ['driver' => 'eloquent', 'model' => \App\Models\User::class] ] ... ];
You need to change a little the bootstrap/app.php file doing the following:
<?php ... // enable facades $app->withFacades(); // enable eloquent $app->withEloquent(); ... $app->configure('app'); // initialize auth configuration $app->configure('auth'); ... // enable auth and throttle middleware $app->routeMiddleware([ 'auth' => App\Http\Middleware\Authenticate::class, 'throttle' => Nomadnt\LumenPassport\Middleware\ThrottleRequests::class ]); ... // register required service providers // $app->register(App\Providers\AppServiceProvider::class); $app->register(App\Providers\AuthServiceProvider::class); $app->register(Nomadnt\LumenPassport\PassportServiceProvider::class); // $app->register(App\Providers\EventServiceProvider::class); ...
Create database.sqlite
touch database/database.sqlite
Lauch the migrations
php artisan migrate
Install Laravel passport
# Install encryption keys and other necessary stuff for Passport
php artisan passport:install
The previous command should give back to you an output similar to this:
Encryption keys generated successfully. Personal access client created successfully. Client ID: 1 Client secret: BxSueZnqimNTE0r98a0Egysq0qnonwkWDUl0KmE5 Password grant client created successfully. Client ID: 2 Client secret: VFWuiJXTJhjb46Y04llOQqSd3kP3goqDLvVIkcIu
User model
Make sure your user model uses Passport's HasApiTokens trait, eg.:
<?php namespace App; use Illuminate\Auth\Authenticatable; use Laravel\Passport\HasApiTokens; use Laravel\Lumen\Auth\Authorizable; use Illuminate\Database\Eloquent\Model; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; class User extends Model implements AuthenticatableContract, AuthorizableContract { use HasApiTokens, Authenticatable, Authorizable; // rest of the model }
Access Token Events
Prune and/or Revoke tokens
If you want to revoke or purge tokens on event based you have to create related Listeners and
register on your app/Http/Providers/EventServiceProvider.php istead of using deprecated properties
Passport::$revokeOtherTokens = true; and Passport::$pruneRevokedTokens = true;
First you need to make sure that EventServiceProvider is registered on your bootstrap/app.php
<?php ... // $app->register(App\Providers\AppServiceProvider::class); $app->register(App\Providers\AuthServiceProvider::class); $app->register(Nomadnt\LumenPassport\PassportServiceProvider::class); $app->register(App\Providers\EventServiceProvider::class); ...
Then you need to listen for AccessTokenCreated event and register your required listeners
<?php namespace App\Providers; use Laravel\Lumen\Providers\EventServiceProvider as ServiceProvider; class EventServiceProvider extends ServiceProvider{ /** * The event listener mappings for the application. * * @var array */ protected $listen = [ 'Laravel\Passport\Events\AccessTokenCreated' => [ 'App\Listeners\RevokeOtherTokens', 'App\Listeners\PruneRevokedTokens', ] ]; }
Create the app/Listeners/RevokeOtherTokens.php file and put the following content
<?php namespace App\Listeners; use Laravel\Passport\Events\AccessTokenCreated; use Laravel\Passport\Token; class RevokeOtherTokens { /** * Create the event listener. * * @return void */ public function __construct() { // } /** * Handle the event. * * @param \App\Events\OrderShipped $event * @return void */ public function handle(AccessTokenCreated $event) { Token::where(function($query) use($event){ $query->where('user_id', $event->userId); $query->where('id', '<>', $event->tokenId); })->revoke(); } }
Create the app/Listeners/PruneRevokedTokens.php file and put the following content
<?php namespace App\Listeners; use Laravel\Passport\Events\AccessTokenCreated; use Laravel\Passport\Token; class PruneRevokedTokens { /** * Create the event listener. * * @return void */ public function __construct() { // } /** * Handle the event. * * @param \App\Events\AccessTokenCreated $event * @return void */ public function handle(AccessTokenCreated $event) { Token::where(function($query) use($event){ $query->where('user_id', $event->userId); $query->where('id', '<>', $event->tokenId); $query->where('revoked', true); })->delete(); } }
nomadnt/lumen-passport 适用场景与选型建议
nomadnt/lumen-passport 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.39k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2018 年 11 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「lumen」 「laravel passport」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nomadnt/lumen-passport 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nomadnt/lumen-passport 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nomadnt/lumen-passport 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Testing Suite For Lumen like Laravel does.
Stackdriver handler for Monolog (codeinternetapplications/monolog-stackdriver Fork).
Class to generate a standard structure for api json responses
Amqp classes
Audit changes of your Eloquent models in Laravel/Lumen
Laravel, Lumen and Native php elasticseach query builder to build complex queries using an elegant syntax
统计信息
- 总下载量: 7.39k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 6
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-11-11