定制 kwidoo/passport-multiauth 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kwidoo/passport-multiauth

Composer 安装命令:

composer require kwidoo/passport-multiauth

包简介

Extends Laravel Passport Password Grant with OTP

README 文档

README

Latest Version on Packagist Total Downloads GitHub Actions

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

  1. Require via Composer:

    composer require kwidoo/passport-multiauth

    This automatically installs the package and its dependencies (except for the optional Twilio SDK mentioned above).

  2. Publish Config (optional):

    php artisan vendor:publish --provider="Kwidoo\MultiAuth\MultiAuthServiceProvider" --tag=config

    This copies passport-multiauth.php into your config folder, allowing you to adjust OTP strategies, timeouts, and more.

  3. 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 otps table to store OTP codes and status.
    • Views include basic Blade templates for OTP success/error states.
  4. Run Migrations:

    php artisan migrate

    This will create any necessary tables (e.g., otps table for storing email OTPs).

  5. Configure Twilio (Optional): If using Twilio, define these in your .env or in a config/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

  1. Request an OTP This package includes OTPController and a route file (routes.php) with a POST /oauth/otp endpoint by default. Package will respect changes in config/passport.php file. 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., TwilioService or EmailVerifier) to generate/send OTP.

  2. 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 method is "password" or missing, it defaults to Laravel Passport’s normal password-based grant.
  3. 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: Usually OTPStrategy, which handles how the credentials are validated using the class.
    • resolver: Defines how to find your user record after OTP validation (e.g., by email, phone).
  4. 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.model key 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

  1. User enters their phone number on your app.
  2. Your app calls POST /oauth/otp with { "method":"twilio", "username":"+1234567890" }.
  3. TwilioService (in the background) sends an SMS code via Twilio.
  4. User receives the code and enters it in your app.
  5. Your app calls POST /oauth/token with:
    {
      "grant_type": "password",
      "client_id": "...",
      "client_secret": "...",
      "username": "+1234567890",
      "password": "the-otp-code",
      "method": "twilio"
    }
  6. MultiAuthGrant checks that the OTP is correct, then resolves the user from the database, and issues a Passport token.

Testing

Running Tests Locally

  1. Install dependencies & dev tools (like Orchestra Testbench):
    composer install
  2. Run tests:
    composer test
    or
    vendor/bin/phpunit
  3. Generate coverage:
    composer test-coverage
    This outputs an HTML coverage report in a coverage folder.

Contributing

  • Feel free to open issues or submit pull requests on GitHub.
  • All contributions are welcome and appreciated.

License

MIT

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 kwidoo/passport-multiauth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

与 kwidoo/passport-multiauth 相关的其它包

同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 33
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-27