ichtrojan/laravel-otp

一个用于生成和验证一次性密码(OTP)的简单包

v2.0.0 2023-12-24 22:16 UTC

This package is auto-updated.

Last update: 2024-09-06 09:17:43 UTC


README

Latest Stable Version Total Downloads License

介绍 🖖

这是一个用于生成和验证一次性密码(OTP)的简单包。这主要可以在身份验证中实现。

安装 💽

通过composer安装

composer require ichtrojan/laravel-otp

运行迁移

php artisan migrate

使用 🧨

注意
响应以对象的形式返回。您可以使用箭头运算符(->)访问其属性

生成OTP

<?php

use Ichtrojan\Otp\Otp;

(new Otp)->generate(string $identifier, string $type, int $length = 4, int $validity = 10);
  • $identifier: 将与OTP相关联的标识。
  • $type: 要生成的令牌类型,支持的类型有numericalpha_numeric
  • $length (可选 | 默认 = 4): 要生成的令牌长度。
  • $validity (可选 | 默认 = 10): OTP的有效期(分钟)。

示例

<?php

use Ichtrojan\Otp\Otp;

(new Otp)->generate('michael@okoh.co.uk', 'numeric', 6, 15);

这将生成一个有效期为15分钟的六位OTP,成功的响应将是

{
  "status": true,
  "token": "282581",
  "message": "OTP generated"
}

验证OTP

<?php

use Ichtrojan\Otp\Otp;

(new Otp)->validate(string $identifier, string $token)
  • $identifier: 与OTP相关联的标识。
  • $token: 与标识相关联的令牌。

示例

<?php

use Ichtrojan\Otp\Otp;

(new Otp)->validate('michael@okoh.co.uk', '282581');

响应

成功

{
  "status": true,
  "message": "OTP is valid"
}

不存在

{
  "status": false,
  "message": "OTP does not exist"
}

无效*

{
  "status": false,
  "message": "OTP is not valid"
}

过期

{
  "status": false,
  "message": "OTP Expired"
}

删除过期令牌

您可以通过运行以下artisan命令来删除过期令牌

php artisan otp:clean

您还可以将此artisan命令添加到app/Console/Kernel.php中,以便自动按计划清理

<?php

protected function schedule(Schedule $schedule)
{
    $schedule->command('otp:clean')->daily();
}

贡献

如果您发现此包有任何问题或建议,请帮忙。我并不完美。