kenkioko / laravel-otp
一个用于生成和验证一次性密码(OTP)的简单包。[基于 ichtrojan/laravel-otp 分支](https://github.com/ichtrojan/laravel-otp)
2.0.0
2021-10-26 23:59 UTC
This package is auto-updated.
Last update: 2024-09-27 06:07:47 UTC
README
简介 🖖
这是一个用于生成和验证一次性密码(OTP)的简单包。这主要用于身份验证。这是一个从 ichtrojan/laravel-otp 分支出来的项目。在这个版本中,默认的 $identifier
类型是 App\User
而不是 string
类型。
安装 💽
通过 composer 安装
composer require kenkioko/laravel-otp
将服务提供者添加到 config/app.php
文件中
<?php /* |-------------------------------------------------------------------------- | Autoloaded Service Providers |-------------------------------------------------------------------------- | | The service providers listed here will be automatically loaded on the | request to your application. Feel free to add your own services to | this array to grant expanded functionality to your applications. | */ 'providers' => [ ... Kenkioko\OTP\OTPServiceProvider::class, ]; ...
将别名添加到 config/app.php
文件中
<?php /* |-------------------------------------------------------------------------- | Class Aliases |-------------------------------------------------------------------------- | | This array of class aliases will be registered when this application | is started. However, feel free to register as many as you wish as | the aliases are "lazy" loaded so they don't hinder performance. | */ 'aliases' => [ ... 'OTP' => Kenkioko\OTP\OTP::class, ]; ...
使用
php artisan vendor:publish --provider="Kenkioko\OTP\OTPServiceProvider"
运行迁移
php artisan migrate
使用 🧨
注意
响应以对象形式返回。您可以使用箭头操作符(->
)访问其属性
生成 OTP
<?php OTP::generate(App\User $identifier, int $digits = 4, int $validity = 5)
$identifier
:将与类型为\App\User::class
的 OTP 相关联的身份。$digit (可选 | 默认 = 4)
:要生成的数字数量,可以是 4、5 或 6 中的任意一个。$validity (可选 | 默认 = 5)
:OTP 的有效期为多少分钟。
示例
<?php $user = App\User::find(1); $otp = OTP::generate($user, 6, 15);
这将生成一个六位数的 OTP,有效期为 15 分钟,成功响应将为
{
"status": true,
"token": "282581",
"message": "OTP generated"
}
验证 OTP
<?php OTP::validate(App\User $identifier, string $token)
$identifier
:与类型为\App\User::class
的 OTP 相关联的身份。$token
:与身份关联的令牌。
示例
<?php $user = App\User::find(1); $otp = OTP::generate($user, '282581');
延长 OTP 的有效期
<?php OTP::extend(App\User $identifier, string $token, int $validity = 1)
$identifier
:与类型为\App\User::class
的 OTP 相关联的身份。$token
:与身份关联的令牌。$validity (可选 | 默认 = 1)
:OTP 的有效期为多少分钟。
示例
<?php $user = App\User::find(1); $otp = OTP::extend($user, '282581', 5);
响应
使用 Laravel 的本地化功能来显示消息。翻译文件位于 translations\en\messages.php
文件中。请将该文件用作其他语言的模板。
成功时
{
"status": true,
'message' => __("laravel-otp::messages.otp_message", ['password' => '12345']),
// "otp_message" => "Your one-time password (OTP) to enter the system is: :password"
}
有效
{
"status": false,
'message' => __("laravel-otp::messages.otp_valid"),
// "otp_valid" => "The one-time password (OTP) token is valid."
}
无效
{
"status": false,
'message' => __("laravel-otp::messages.otp_invalid"),
// "otp_invalid" => "The one-time password (OTP) token is not valid".
}
过期
{
"status": false,
'message' => __("laravel-otp::messages.otp_expired"),
// "otp_expired" => "token seems to be expired. Please request a new OTP code."
}
缺少字段
{
"status": false,
'message' => __("laravel-otp::messages.otp_missing"),
// "otp_missing" => "The one-time password (OTP) token does not exist.",
}
不匹配
{
"status": false,
'message' => __("laravel-otp::messages.otp_mismatch"),
// "otp_mismatch" => "The one-time password (OTP) token doesn't match any token in database."
}
有效期延长
{
"status": false,
'message' => __("laravel-otp::messages.otp_extended", ['minutes' => 5 ]),
// "otp_extended" => "Your one-time password (OTP) token expiry was extended by :minutes minutes."
}
贡献
这是一个从 ichtrojan/laravel-otp 分支出来的项目。
如果您发现此包有任何问题或建议,请帮忙。我不完美。