ryangjchandler/bearer
Composer 安装命令:
composer require ryangjchandler/bearer
包简介
Minimalistic token-based authentication for Laravel API endpoints.
README 文档
README
Minimalistic token-based authorization for Laravel API endpoints.
Installation
You can install the package via Composer:
composer require ryangjchandler/bearer
You can publish and run the migrations with:
php artisan vendor:publish --provider="RyanChandler\Bearer\BearerServiceProvider" --tag="bearer-migrations" php artisan migrate
You can publish the config file with:
php artisan vendor:publish --provider="RyanChandler\Bearer\BearerServiceProvider" --tag="bearer-config"
Usage
Creating tokens
To create a new token, you can use the RyanChandler\Bearer\Models\Token model.
use RyanChandler\Bearer\Models\Token; $token = Token::create([ 'token' => Str::random(32), ]);
Alternatively, you can use the RyanChandler\Bearer\Facades\Bearer facade to generate a token.
use RyanChandler\Bearer\Facades\Bearer; $token = Bearer::generate(domains: [], expiresAt: null, description: null);
By default, Bearer uses time-ordered UUIDs for token strings. You can modify this behaviour by passing a Closure to Bearer::generateTokenUsing. This function must return a string for storage to the database.
use RyanChandler\Bearer\Facades\Bearer; Bearer::generateTokenUsing(static function (): string { return (string) Str::orderedUuid(); });
Retrieving a Token instance
To retrieve a Token instance from the token string, you can use the RyanChandler\Bearer\Facades\Bearer facade.
use RyanChandler\Bearer\Facades\Bearer; $token = Bearer::find('my-token-string');
Using a token in a request
Bearer uses the Authorization header of a request to retreive the token instance. You should format it like so:
Authorization: Bearer my-token-string
Verifying tokens
To verify a token, add the RyanChandler\Bearer\Http\Middleware\VerifyBearerToken middleware to your API route.
use RyanChandler\Bearer\Http\Middleware\VerifyBearerToken; Route::get('/endpoint', MyEndpointController::class)->middleware(VerifyBearerToken::class);
Token expiration
If you would like a token to expire at a particular time, you can use the expires_at column.
$token = Bearer::find('my-token-string'); $token->update([ 'expires_at' => now()->addWeek(), ]);
Or just use the class's helper methods.
$token = Bearer::find('my-token-string'); $token->addWeeks(1)->save();
If you try to use the token after this time, it will return an error.
Limit tokens to a particular domain
Token usage can be restricted to a particular domain. Bearer uses the scheme and host from the request to determine if the token is valid or not.
$token = Bearer::find('my-token-string'); $token->update([ 'domains' => [ 'https://laravel.com', ], ]);
If you attempt to use this token from any domain other than https://laravel.com, it will fail and abort.
Note: domain checks include the scheme so be sure to add both cases for HTTP and HTTPS if needed.
Set a token description
You can optionally set a description for the token.
$token = Bearer::find('my-token-string'); $token->update([ 'description' => 'Example description for the token.', ]);
Note: The description field accepts a maximum of 255 characters.
Testing
composer test
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
ryangjchandler/bearer 适用场景与选型建议
ryangjchandler/bearer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31.61k 次下载、GitHub Stars 达 81, 最近一次更新时间为 2021 年 05 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「Bearer」 「ryangjchandler」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ryangjchandler/bearer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ryangjchandler/bearer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ryangjchandler/bearer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A dead-simple comments package for Laravel.
JWT Token for Codeigniter 4
RFC 6750 Compatible Bearer Authentication for Laravel
Build structured navigation menus in Filament.
Yii 2 custom tokens architecture for authorization
Symfony2 OAuth2 Server Bearer Access Token Manager Bundle
统计信息
- 总下载量: 31.61k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 81
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-05-14