njoguamos/laravel-otp 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

njoguamos/laravel-otp

Composer 安装命令:

composer require njoguamos/laravel-otp

包简介

A composer package for generating and verifying One Time Passwords (OTP) in Laravel 11+.

关键字:

README 文档

README

Latest Version on Packagist run-tests Fix PHP code style issues Total Downloads

A composer package for generating and verifying One Time Passwords (OTP) in Laravel 11+.

Installation

Version Supported Laravel
1.x 11.x, 12.x
2.x 12.x, 13.x

You can install the package via composer:

composer require njoguamos/laravel-otp

You can publish and run the migrations with:

php artisan vendor:publish --tag="otp-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="otp-config"
This is the contents of the published config file:
return [

    /*
    |--------------------------------------------------------------------------
    |  OTP Length
    |--------------------------------------------------------------------------
    |
    | This is the length of the generated OTP token. By default it is set to
    | 6 digits. The length of the OTP must be at least 4 digits to ensure
    | that the OTP is not easily guessable
    |
    */

    'length' => env(key: 'OPT_LENGTH', default: 6),

    /*
    |--------------------------------------------------------------------------
    | OTP Validity time by minutes
    |--------------------------------------------------------------------------
    |
    | This is the validity time of the generated OTP token. By default it is
    | set to 10 minutes. This means that the OTP will be valid for 10 minutes
    | after it is generated. You can change this value to suit your needs.
    |
    */

    'validity' => env(key: 'OTP_VALIDITY', default: 10),

    /*
    |--------------------------------------------------------------------------
    | Digits Only
    |-------------------------------------------------------------------------
    |
    | When set to true, the generated OTP will only contain digits. When set
    | to false, the generated OTP will contain both digits and alphanumeric
    | characters which makes it more difficult to guess the OTP.
    |
    */

    'digits_only' => env(key: 'OTP_DIGITS_ONLY', default: true),
];

Usage

Generate OTP

To generate an OTP, you can use the generate() method on the Otp class. . This method takes an identifier as a parameter. The identifier can be and email address, phone number, or any other unique identifier that you want to use to identify the user.

use NjoguAmos\Otp\Otp;

$otp = Otp::generate(identifier: 'example@gmail.com');

$otp->identifier; # example@gmail.com
$otp->token; # 123456
$otp->expires_in; # 10 minutes

The generate() method returns an instance of the \NjoguAmos\Otp\Models\Otp Eloquent Models class. You can access the identifier, token, and expires_at properties of the Otp class.

For example: you can use the token property to send the OTP to the user's email address.

use NjoguAmos\Otp\Otp;
use App\Mail\OTPMail;
use Illuminate\Support\Facades\Mail;

$email = 'example@gmail.com';

$otp = Otp::generate(identifier: $email);

Mail::to($email)->send(new OTPMail($otp));

Verify OTP

To verify an OTP, you can use the validate() method on the Otp class. This method takes an identifier and token as parameters.

If the OTP is valid, the method will return true. Otherwise, it will return false.

use NjoguAmos\Otp\Otp;

$email = 'example@gmail.com';
$otp = '123456';

$validated = Otp::validate(identifier: $email, token: $otp->token);

$validated // True or False

Note

It is advisable not to let the user know if the OTP does not match, or does not exist or expired. You can return a generic message to the user instead.

Invalidating a Token

It is highly recommended to invalid OTPs to avoid abuse. You can call invalidate() on the Otp object which deletes it from the database.

use NjoguAmos\Otp\Otp;

$otp = Otp::generate(identifier: 'example@gmail.com');

$otp->invalidate();

Note

Please note that validating a token automatically invalidates it to avoid second time use

Delete Expired OTPs

To periodically delete expired OTPs, you can use the model:prune Artisan command. This command will delete all expired OTPs from the database.

To do so, add model:prune to your routes/console.php file:

use Illuminate\Support\Facades\Schedule;
use NjoguAmos\Otp\Models\Otp as OtpModel;

Schedule::command('model:prune', ['--model' => [OtpModel::class]])->everyFiveMinutes();

Tip

Make sure the duration is greater than the validity time of the OTP.

Security

Tip

To prevent brute force attacks, rate limit the number of attempts to generate or verify an OTP tokens. This can be done by using the Laravel RateLimit middleware. User can verify tokens as long as they are valid. This means that the user can have multiple valid tokens. Once the token expires, it cannot be valid even if it still exists in the database. Schedule the model:prune command to run every 5 minutes to delete expired tokens.

Testing

composer test

Changelog

Please see RELEASE for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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