jorgemudry/laravel-remote-token-auth
Composer 安装命令:
composer require jorgemudry/laravel-remote-token-auth
包简介
This package provides a hassle-free way to incorporate authentication in your application by integrating with an external api.
README 文档
README
This package provides a hassle-free way to incorporate authentication in your application by integrating with an external service.
It enables the validation of tokens to verify the authenticity of users, ensuring that only valid users gain access to your system.
With this package, you can focus on developing your application's core functionalities while the authentication process is handled seamlessly.
It simplifies the integration process, allowing you to obtain a valid user in no time.
Requirements
- Laravel 8.x or higher
- PHP 8.0 or higher
Installation
You can install the package via composer:
composer require jorgemudry/laravel-remote-token-auth
The package will automatically register its service provider.
You can publish the config file with:
php artisan vendor:publish --provider="JorgeMudry\LaravelRemoteTokenAuth\Providers\LaravelRemoteTokenAuthServiceProvider"
Usage
To require authentication for a specific route, simply add the auth middleware and specify the rta guard. This will ensure that only authenticated users with a valid token can access the route.
Route::get('/users', function (Request $request) { return $request->user(); })->middleware('auth:rta');
Advanced Usage
Custom Adapter Implementation
To create a custom implementation of the class that calls the external API to validate the token, simply implement the AdapterInterface interface on your class. Then, bind the interface to your implementation in the register method of the AuthServiceProvider.
This will allow you to use your own implementation of the class for token validation.
<?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); } }
Using Your Own Authenticated User Class
If you want to use your own class as the logged-in user, you can easily replace the default class with your own. To do this, your custom class must accept an array as a constructor argument and implement the Illuminate\Contracts\Auth\Authenticatable interface (an easy way to this is to extends from the Illuminate\Auth\GenericUser class).
To use your custom class, you will need to update the configuration file to point to your class instead of the default one. This way, when a user logs in, your custom class will be used to represent the user in the application.
<?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, ] ];
Testing
To run the package's test suite, run the following command:
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
If you would like to contribute to the package, please see CONTRIBUTING for information on how to get started.
Security
If you discover any security related issues, please email jorgemudry@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
jorgemudry/laravel-remote-token-auth 适用场景与选型建议
jorgemudry/laravel-remote-token-auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 03 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「auth」 「laravel」 「token」 「stateless」 「external」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jorgemudry/laravel-remote-token-auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jorgemudry/laravel-remote-token-auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jorgemudry/laravel-remote-token-auth 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Multiauth package
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
A PSR-7 compatible library for making CRUD API endpoints
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
Email Toolkit Plugin for CakePHP
Alfabank REST API integration
统计信息
- 总下载量: 25
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-03-01