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

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

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

mkd/laravel-advanced-otp

最新稳定版本:v1.0.1

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.

统计信息

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

GitHub 信息

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

其他信息

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