achinthailabs/laravel5-otp

Laravel OTP 生成器和验证工具

dev-main 2022-06-20 07:49 UTC

This package is auto-updated.

Last update: 2024-09-20 14:50:58 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

代码直接从 https://github.com/teckwei1993/laravel-otp 获取,并进行了调整以适应我们的需求。请使用原始仓库进行您的相关工作。

一个用于 Laravel 单次密码 (OTP) 生成和验证的包,不使用 Eloquent 模型,因为它由 缓存 完成。缓存连接与您的 Laravel 缓存配置相同,支持: "apc", "array", "database", "file", "memcached", "redis"。

安装

通过 Composer

$ composer require achinthailabs/laravel5-otp

添加服务提供者和外观

适用于 Laravel 5.5+

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

适用于 Laravel 5.2 / 5.3 / 5.4

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

Achinthailabs\Otp\OtpServiceProvider::class

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

'Otp' => Achinthailabs\Otp\OtpFacade::clas

配置

发布配置和语言文件

php artisan vendor:publish --provider="Achinthailabs\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('reg:name@domain.com');

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

验证 OTP

Otp::validate(string $identifier, string $password)
  • $identifier:与 OTP 相关联的身份。
  • $password:与身份关联的密码。

示例

use OTP;

// in controller

$result = Otp::validate('reg: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 Achinthailabs\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 Achinthailabs\Otp\Rules\OtpValidate;

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

// in a controller

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

高级用法

使用选项生成 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 Achinthailabs\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 Achinthailabs\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 Achinthailabs\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"

变更日志

有关最近更改的更多信息,请参阅CHANGELOG

贡献

有关详细信息,请参阅CONTRIBUTINGCODE_OF_CONDUCT

安全性

如果您发现任何安全相关的问题,请发送电子邮件至achintha@incubatelabs.com,而不是使用问题跟踪器。

鸣谢

许可证

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