定制 mkd/laravel-advanced-otp 二次开发

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

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

mkd/laravel-advanced-otp

Composer 安装命令:

composer require mkd/laravel-advanced-otp

包简介

An advanced, customizable OTP (One-Time Password) verification system for Laravel applications, supporting hashed token and custom validation methods.

README 文档

README

Latest Version on Packagist Total Downloads GitHub Actions

Laravel Advanced OTP is a package designed for flexible OTP (One-Time Password) verification, supporting both hashed token verification and custom validation methods. It allows for easy OTP handling for tasks like email-based authentication.

Features

  • Hashed Token Verification: Secure OTP validation using hashed tokens.
  • Custom Validation: Developers can use their own validation methods (e.g., database or cache-based).
  • Configurable OTP Settings: Custom timeout and OTP length.

Installation

Install the package via Composer:

composer require mkd/laravel-advanced-otp

Create your own OTPMethod

php artisan magic-otp:make LoginOTP

Usage

1. OTP Generation and Email Sending (Hashed Token)

In this example, a hashed token is used to securely send and verify the OTP.

// Generate OTP and send it via email
$otp = \LaravelAdvancedOTP::handle(LoginOTP::class, [
    'secret' => 'secret_key',  // Required to hash and verify OTP
    'email' => 'user_email@example.com',  // Email of the recipient
]);

// Get the hashed token for verification
$token = $otp->getHashedKey();

// Send OTP to user's email
$otp->send('user_email@example.com');

// Return the hashed token for later verification
return response()->json(['token' => $token]);

2. OTP Generation Without Hashed Token

If you want to handle the OTP validation manually (e.g., store it in a database or cache), you can omit the hashed token.

// Generate and send OTP without hashed token
\LaravelAdvancedOTP::handle(LoginOTP::class)->send('user_email@example.com');

3. Verifying OTP (Hashed Token)

Use the hashed token to validate the OTP.

$otp = request('otp');
$hashedToken = request('token');  // Token returned when sending OTP

$signature = [
    'secret' => 'secret_key',  // Same secret used during OTP generation
    'email' => 'user_email@example.com',
];

// Verify the OTP using the hashed token
$otpStatus = \LaravelAdvancedOTP::verify(LoginOTP::class, $otp, $signature, $hashedToken);

if ($otpStatus == OTPStatusEnum::NOT_VERIFIED) {
    // OTP is invalid
}

if ($otpStatus == OTPStatusEnum::VERIFIED) {
    // OTP is valid
}

if ($otpStatus == OTPStatusEnum::EXPIRED) {
    // OTP has expired
}

4. Verifying OTP (Custom Validation)

If you want to handle OTP validation manually, you can use your custom logic for verification.

$otp = request('otp');
$email = request('email');

// Custom validation for OTP
$otpVerified = \LaravelAdvancedOTP::validate(LoginOTP::class, $otp, $email);

if ($otpVerified) {
    // OTP is valid
} else {
    // OTP is invalid or expired
}

Custom OTP Class

To implement your OTP logic, create a class extending MagicOTP. Here is an example:

class LoginOTP extends MagicOTP
{
    protected int $timeout = 120;  // Timeout in seconds
    protected int $otpLength = 5;  // Length of the OTP

    public function send($email)
    {
        $otp = $this->getOTP();
        // Logic to send OTP via email
    }

    public function validate($otp, $email)
    {
        // Logic to validate OTP for the email
    }
}

Configuration

You can adjust the default settings like OTP timeout, length, and more by customizing your OTP class.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email mustafakhaleddev@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

mkd/laravel-advanced-otp 适用场景与选型建议

mkd/laravel-advanced-otp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 10, 最近一次更新时间为 2024 年 10 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「Authentication」 「laravel」 「otp」 「one-time password」 「one-time-password」 「2fa」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 mkd/laravel-advanced-otp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mkd/laravel-advanced-otp 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-10