officialozioma/otp-generator

Laravel OTP 生成器和验证器

v1.0.0 2022-04-02 19:10 UTC

This package is auto-updated.

Last update: 2024-09-30 01:43:31 UTC


README

介绍

这是一个 Laravel 包,用于生成一次性密码 (OTP) 并进行验证。它在缓存中进行操作,支持所有 Laravel 缓存驱动器:"apc","array","database","file","memcached","redis"。

安装

使用 Composer 安装

composer require officalozioma/otp-generator

添加服务提供者和外观

对于 Laravel 5.5+

一旦添加了包,服务提供者和外观将自动发现。

对于 Laravel 5.2 / 5.3 / 5.4

将 ServiceProvider 添加到 config/app.php 中的 providers 数组

Ozioma\Otp\OtpServiceProvider::class

将 Facade 添加到 config/app.php 中的 aliases 数组

'Otp' => Ozioma\Otp\OtpFacade::class

配置

发布配置和语言文件

php artisan vendor:publish --provider="Ozioma\Otp\OtpServiceProvider"

此包在您的应用程序的配置文件夹中发布了一个 otp.php 文件,其中包含此包的设置。大多数变量都与环境变量绑定,您可以在 Laravel 应用程序的 .env 文件中添加键值对。

OTP_FORMAT=numeric
OTP_LENGTH=6
OTP_SENSITIVE=false
OTP_EXPIRES_TIME=15
OTP_ATTEMPT_TIMES=5
OTP_REPEATED=true
OTP_DEMO=false

用法

生成 OTP

Otp::generate(string $identifier)
  • $identifier:将与 OTP 绑定的身份。

示例

use OTP;

// in controller

$password = Otp::generate('name@domain.com');

这将生成一个有效期为 15 分钟的 OTP。

验证 OTP

Otp::validate(string $identifier, string $password)
  • $identifier:与 OTP 绑定的身份。
  • $password:与身份绑定的密码。

示例

use OTP;

// in controller

$result = Otp::validate('name@domain.com', '123456');

响应

成功时

{
  "status": true
}

无效 OTP

{
  "status": false,
  "error": "invalid"
}

已过期

{
  "status": false,
  "error": "expired"
}

最大尝试次数

{
  "status": false,
  "error": "max_attempt"
}
  • 达到最大允许尝试次数,默认为每个标识符 10 次。

通过 Laravel 验证器验证 OTP

// in a `FormRequest`

use Ozioma\Otp\Rules\OtpValidate;

public function rules()
{
    return [
        'code' => ['required', new OtpValidate('change-email:name@domain.com')]
    ];
}

// in a controller

$request->validate([
    'code' => ['required', new OtpValidate('change-email:name@domain.com')]
]);

通过会话 ID 验证 OTP

// Otp class

$result = Otp::validate('123456');

// in a `FormRequest`

use Ozioma\Otp\Rules\OtpValidate;

public function rules()
{
    return [
        'code' => ['required', new OtpValidate()]
    ];
}

// in a controller

$request->validate([
    'code' => ['required', new OtpValidate()]
]);
  • 设置中没有标识符时,将自动使用会话 ID 作为默认值,OTP 生成和验证将在同一会话(浏览器的 cookie)中完成。

高级用法

使用选项生成 OTP

$password = Otp::setLength(8)->setFormat('string')->setExpires(60)->setRepeated(false)->generate('identifier-key-here');

// or array option

$password = Otp::generate('identifier-key-here', [
    'length' => 8,
    'format' => 'string',
    'expires' => 60,
    'repeated' => false
]);
  • setLength($length):密码的长度。默认:6
  • setFormat($format):格式选项允许您决定在生成新密码时使用哪种生成器实现。选项:"string","numeric","numeric-no-zero","customize"。默认:"numeric"}
  • setExpires($minutes):密码的过期时间(分钟)。默认:15
  • setRepeated($boolean):密码的重复。当生成新密码时,之前的密码仍然有效,直到其中一个密码被使用或本身过期。默认:true

使用自定义密码生成 OTP

$password = Otp::setCustomize('12345678ABC@#$')->generate('identifier-key-here');
  • setCustomize($string):从自定义字符串中随机选择字母

使用特定尝试次数验证 OTP

$password = Otp::setAttempts(3)->validate('identifier-key-here', 'password-here');
  • setAttempts($times):不正确密码尝试的次数。默认:5

使用区分大小写的验证 OTP

$password = Otp::setSensitive(true)->generate('identifier-key-here');

// validate

$result = Otp::setSensitive(true)->validate('identifier-key-here', 'password-here');

// in controller

use Ozioma\Otp\Rules\OtpValidate;

$request->validate([
    'code' => ['required', new OtpValidate('identifier-key-here', ['sensitive' => true])]
]);
  • setSensitive($boolean):要求正确输入大写和小写字母。默认:true

使用单独的密码生成 OTP

$password = Otp::setLength([4,3,4])->setSeparator(':')->generate('identifier-key-here');

示例密码

3526:126:3697
  • setLength($array):密码的长度,使用数组来分隔每个长度。
  • setSeparator($string):密码的分隔符。默认:"-"

使用额外数据验证 OTP

$password = Otp::setData(['user_id' => auth()->id()])->generate('login-confirmation');
  • setData($var):允许您获取 OTP 的额外数据。
// validate

$result = Otp::setDisposable(false)->validate('login-confirmation', 'password-here');

// in controller

use Ozioma\Otp\Rules\OtpValidate;

$request->validate([
    'code' => ['required', new OtpValidate('login-confirmation', ['disposable' => false])]
]);
  • setDisposable($boolean):OTP 标识符的可处置性,当使用同一标识符的密码时,不同的密码无效。默认:true

成功响应时

{
  "status": true,
  "data": [
    "user_id": 10
  ]
}
  • 当您将可处置性设置为 false 时,您将能够支持不同用户在 OTP 的同一标识符键中使用不同密码和不同额外数据。

使用跳过使用验证 OTP

// validate

$result = Otp::setSkip(true)->validate('identifier-key-here', 'password-here');

// in controller

use Ozioma\Otp\Rules\OtpValidate;

$request->validate([
    'code' => ['required', new OtpValidate('identifier-key-here', ['skip' => true])]
]);
  • setSkip($boolean):在验证时跳过使用密码,这意味着您可以再次使用密码。默认:false
  • 当表单请求有错误响应时,将跳过使用密码,但请记住在控制器中 OTP::validate(...)

删除OTP

Otp::forget('identifier-key-here');
  • 删除具有此特定标识符的所有密码

删除特定密码

Otp::forget('identifier-key-here', 'password-here');

重置尝试次数

Otp::resetAttempt('identifier-key-here');

演示密码

将以下键值对添加到Laravel应用程序中的.env文件。

OTP_DEMO=true
  • 开发目的的演示模式,无需使用真实密码进行验证。
  • 默认演示密码:"1234","123456","12345678"

贡献

所有贡献都受欢迎!😄

许可

MIT许可(MIT)。