danielrobert/otp-generator

使用数据库生成和验证Otp的Laravel包

v1.07 2023-03-19 13:55 UTC

This package is auto-updated.

Last update: 2024-09-05 16:47:52 UTC


README

Latest Version on Packagist Quality Score Code Quality Github Workflow Status

Licence

安装

您可以使用Composer包管理器将Otp Generator安装到您的Laravel项目中

composer require danielrobert/otp-generator

安装otp-generator后,使用otp:install Artisan命令发布其配置。安装Otp Generator后,您还应运行迁移命令,以创建存储OTP所需的表

php artisan otp:install
 
php artisan migrate

配置

发布Otp Generator的配置后,其主配置文件将位于config/otp-generator.php。此配置文件允许您配置您的otp。每个配置选项都包括其用途的说明,因此请务必仔细研究此文件。

数据清理

不清理数据,otp表可以非常快速地积累记录。为了减轻这种情况,您可以安排otp:prune Artisan命令每天运行

$schedule->command('otp:prune')->daily();

默认情况下,所有已过期的条目或根据config/otp-generator.php配置超过30分钟的条目都将被清理。您可以在调用命令时使用minutes选项来确定保留数据的时间长度。例如,以下命令将删除所有60分钟前创建的记录

$schedule->command('otp:prune --minutes=60')->daily();

用法

use DanielRobert\Otp\Otp;
.
.
$otp =  Otp::generate($identifier);
.
$verify = Otp::validate($identifier, $otp->token);

// example response
{
  "status": true
  "message": "Otp is valid"
}

// to get an expiredAt time
$expires = Otp::expiredAt($identifier);

// example response 
+"status": true
+"expired_at": Illuminate\Support\Carbon @1611895244^ {
  ....
  #dumpLocale: null
  date: 2021-01-29 04:40:44.0 UTC (+00:00)
}

高级用法

除了从otp-generator.php配置文件配置otp外,您还可以直接配置它

use DanielRobert\Otp\Otp;
.
.
$otp =  Otp::setValidity(30)  // otp validity time in mins
      ->setLength(4)  // Lenght of the generated otp
      ->setMaximumOtpsAllowed(10) // Number of times allowed to regenerate otps
      ->setOnlyDigits(false)  // generated otp contains mixed characters ex:ad2312
      ->setUseSameToken(true) // if you re-generate Otp, you will get same token
      ->generate($identifier);
.
$verify = Otp::setAllowedAttempts(10) // number of times they can allow to attempt with wrong token
    ->validate($identifier, $otp->token);

测试

composer test

变更日志

有关最近更改的更多信息,请参阅变更日志

贡献

有关详细信息,请参阅贡献指南

安全漏洞

有关报告安全漏洞的详细信息,请查看我们的安全策略

致谢

许可

MIT许可证(MIT)。有关更多信息,请参阅许可文件