kwidoo/passport-multiauth
Composer 安装命令:
composer require kwidoo/passport-multiauth
包简介
Extends Laravel Passport Password Grant with OTP
README 文档
README
Laravel Passport Multi-Auth
A Laravel package that extends Laravel Passport’s password grant to support multiple authentication methods (e.g., SMS/Email OTP). You can define custom OTP strategies and user resolvers in a single config file, making your OTP flows more flexible and secure.
Requirements
- Laravel Passport (already required by this package).
- PHP ^8.0
- (Optional) Twilio SDK if you plan to use Twilio for SMS OTP:
composer require twilio/sdk
Note: For twilio in production you should install Twilio SDK.
Installation
-
Require via Composer:
composer require kwidoo/passport-multiauth
This automatically installs the package and its dependencies (except for the optional Twilio SDK mentioned above).
-
Publish Config (optional):
php artisan vendor:publish --provider="Kwidoo\MultiAuth\MultiAuthServiceProvider" --tag=configThis copies
passport-multiauth.phpinto yourconfigfolder, allowing you to adjust OTP strategies, timeouts, and more. -
Publish Migrations and Views (if desired):
php artisan vendor:publish --provider="Kwidoo\MultiAuth\MultiAuthServiceProvider" --tag=migrations php artisan vendor:publish --provider="Kwidoo\MultiAuth\MultiAuthServiceProvider" --tag=views
- Migrations will create (by default) an
otpstable to store OTP codes and status. - Views include basic Blade templates for OTP success/error states.
- Migrations will create (by default) an
-
Run Migrations:
php artisan migrate
This will create any necessary tables (e.g.,
otpstable for storing email OTPs). -
Configure Twilio (Optional): If using Twilio, define these in your
.envor in aconfig/twilio.php:TWILIO_SID=your_twilio_sid TWILIO_AUTH_TOKEN=your_twilio_auth_token TWILIO_VERIFY_SID=your_twilio_verify_service_id
Note, you should have a Twilio account and will need to create Verification SID there. This code doesn't use regular SMS by default, but Twilio Verify API. For more details, see the Twilio Verify API.
Usage
-
Request an OTP This package includes
OTPControllerand a route file (routes.php) with aPOST /oauth/otpendpoint by default. Package will respect changes inconfig/passport.phpfile. Example request:POST /oauth/otp { "method": "twilio", "username": "+1234567890" }
or:
POST /oauth/otp { "method": "email", "username": "user@example.com" }
This triggers the corresponding service (e.g.,
TwilioServiceorEmailVerifier) to generate/send OTP. -
Obtain Access Token After receiving the OTP, call the standard Passport token endpoint (often
POST /oauth/token) with parameters:{ "grant_type": "password", "client_id": "your-passport-client-id", "client_secret": "your-passport-client-secret", "username": "+1234567890", "password": "123456", // The OTP from Twilio "method": "twilio" }- If
method="twilio", the package will validate the OTP via Twilio. - If
method="email", it will use the email-based OTP. - If
methodis"password"or missing, it defaults to Laravel Passport’s normal password-based grant.
- If
-
Configuration File (
config/passport-multiauth.php) You can customize each strategy like so:'strategies' => [ 'twilio' => [ 'class' => \Kwidoo\MultiAuth\Services\TwilioService::class, 'strategy' => \Kwidoo\MultiAuth\Services\OTPStrategy::class, 'resolver' => \Kwidoo\MultiAuth\Resolvers\GeneralUserResolver::class, ], 'email' => [ 'class' => \Kwidoo\MultiAuth\Services\EmailVerifier::class, 'strategy' => \Kwidoo\MultiAuth\Services\OTPStrategy::class, 'resolver' => \Kwidoo\MultiAuth\Resolvers\GeneralUserResolver::class, ], // ... or custom strategies ],
class: The service that sends/validates the OTP (Twilio, Email, etc.).strategy: UsuallyOTPStrategy, which handles how the credentials are validated using theclass.resolver: Defines how to find your user record after OTP validation (e.g., by email, phone).
-
OTP model (
Kwidoo\MultiAuth\Models\OTP)-
This model is used on with Email strategy to store OTPs. Twilio doesn't require to store OTPs localy
-
You can customize one time password model by changing the
otp.modelkey in the configuration file. -
Default one time password is 6 digits long and expires in 5 minutes. You can change these values in the configuration file.
-
Example Flow
- User enters their phone number on your app.
- Your app calls
POST /oauth/otpwith{ "method":"twilio", "username":"+1234567890" }. - TwilioService (in the background) sends an SMS code via Twilio.
- User receives the code and enters it in your app.
- Your app calls
POST /oauth/tokenwith:{ "grant_type": "password", "client_id": "...", "client_secret": "...", "username": "+1234567890", "password": "the-otp-code", "method": "twilio" } - MultiAuthGrant checks that the OTP is correct, then resolves the user from the database, and issues a Passport token.
Testing
Running Tests Locally
- Install dependencies & dev tools (like Orchestra Testbench):
composer install
- Run tests:
composer testorvendor/bin/phpunit
- Generate coverage:
composer test-coverage
This outputs an HTML coverage report in acoveragefolder.
Contributing
- Feel free to open issues or submit pull requests on GitHub.
- All contributions are welcome and appreciated.
License
kwidoo/passport-multiauth 适用场景与选型建议
kwidoo/passport-multiauth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 02 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「kwidoo」 「passport-multiauth」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kwidoo/passport-multiauth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kwidoo/passport-multiauth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kwidoo/passport-multiauth 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 33
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-27