jorgemudry / laravel-remote-token-auth
本包通过集成外部API,提供了一种无烦恼的方式将身份验证集成到您的应用程序中。
Requires
- php: ^8.0
- guzzlehttp/guzzle: ^7.5
- illuminate/support: ^8.0|^9.0|^10.0
Requires (Dev)
- laravel/pint: ^1.5|^1.6
- orchestra/testbench: ^6.0|^7.0|^8.0
- pestphp/pest: ^1.22
- pestphp/pest-plugin-laravel: ^1.4
- pestphp/pest-plugin-mock: ^1.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.0
README
本包通过集成外部服务,提供了一种无烦恼的方式将身份验证集成到您的应用程序中。
它能够验证令牌以验证用户的真实性,确保只有有效的用户才能访问您的系统。
使用本包,您可以将注意力集中在开发应用程序的核心功能上,同时身份验证过程将无缝处理。
它简化了集成过程,让您能够迅速获取有效用户。
要求
- Laravel 8.x 或更高版本
- PHP 8.0 或更高版本
安装
您可以通过 composer 安装此包
composer require jorgemudry/laravel-remote-token-auth
包将自动注册其服务提供者。
您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="JorgeMudry\LaravelRemoteTokenAuth\Providers\LaravelRemoteTokenAuthServiceProvider"
用法
要为特定路由要求身份验证,只需添加 auth 中间件并指定 rta 守护者。这将确保只有具有有效令牌的认证用户才能访问该路由。
Route::get('/users', function (Request $request) { return $request->user(); })->middleware('auth:rta');
高级用法
自定义适配器实现
要创建调用外部API验证令牌的类的自定义实现,只需在您的类上实现 AdapterInterface 接口。然后,在 AuthServiceProvider 的 register 方法中将接口绑定到您的实现。
这将允许您使用自己的类实现进行令牌验证。
<?php namespace App\Adapters; use JorgeMudry\LaravelRemoteTokenAuth\Contracts\AdapterInterface; use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Http\Request; class MyAdapter implements AdapterInterface { public function authorize(Request $request): Authenticatable { // your custom implementation logic } }
<?php namespace App\Providers; use App\Adapters\MyAdapter; use JorgeMudry\LaravelRemoteTokenAuth\Contracts\AdapterInterface; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; class AuthServiceProvider extends ServiceProvider { /** * The model to policy mappings for the application. * * @var array<class-string, class-string> */ protected $policies = [ // 'App\Models\Model' => 'App\Policies\ModelPolicy', ]; /** * Register any authentication / authorization services. */ public function boot(): void { } /** * Register any application services. */ public function register(): void { $this->app->bind(AdapterInterface::class, MyAdapter::class); } }
使用您自己的 认证用户
类
如果您想使用您自己的类作为已登录用户,可以轻松地将其替换为默认类。为此,您的自定义类必须接受一个数组作为构造函数参数并实现 Illuminate\Contracts\Auth\Authenticatable
接口(一种简单的方法是扩展 Illuminate\Auth\GenericUser
类)。
要使用您自定义的类,您需要更新配置文件,使其指向您的类而不是默认类。这样,当用户登录时,您的自定义类将用于在应用程序中表示用户。
<?php namespace App\Auth; use Illuminate\Auth\GenericUser; class AuthenticatedUser extends GenericUser { /** * Create a new generic User object. * * @param array<string, mixed> $attributes * @return void */ public function __construct(array $attributes) { parent::__construct($attributes); } }
<?php declare(strict_types=1); use App\Auth\AuthenticatedUser; return [ 'endpoint' => 'https://remote-token-validation.service/token', 'response' => [ 'user_path' => 'data', 'user_class' => AuthenticatedUser::class, ] ];
测试
要运行包的测试套件,请运行以下命令
composer test
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG
贡献
如果您想为此包做出贡献,请参阅 CONTRIBUTING 了解如何开始。
安全
如果您发现任何与安全相关的问题,请通过电子邮件发送到 jorgemudry@gmail.com 而不是使用问题跟踪器。
致谢
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件