graxmonzo / laravel-one-time-password
Laravel 的单次密码 (OTP) 生成特性
1.1.1
2021-01-19 15:15 UTC
Requires
- php: ^7.4|^8.0
- christian-riesen/base32: ^1.5.2
- spomky-labs/otphp: 10.0.1
Requires (Dev)
- orchestra/testbench: ^6.9.0
- phpunit/phpunit: ^9.3
- vimeo/psalm: ^4.4
README
Laravel 实现了 Rails 的 active_model_otp 包。
简单的 OTP 生成。
$code = $user->otp(); // => "324650" $user->verify($code); // => true $user->verify($code); // => false
安装
您可以通过 composer 安装此包
composer require graxmonzo/laravel-one-time-password
用法
您的 Eloquent 模型应使用 GraxMonzo\OneTimePassWord\HasOTP
特性和 GraxMonzo\OneTimePassWord\OTPOptions
类。
该特性包含一个抽象方法 otpOptions()
,您必须自行实现。
您的模型迁移应包含用于保存 OTP 密钥和计数的字段。
以下是如何实现该特性的示例
namespace App; use GraxMonzo\OneTimePassword\HasOTP; use GraxMonzo\OneTimePassword\OTPOptions; use Illuminate\Database\Eloquent\Model; class YourEloquentModel extends Model { use HasOTP; /** * Get the options for generating OTP. */ public function otpOptions() : OTPOptions { return OTPOptions::create() ->saveToFields('otp_secret', 'otp_counter') ->digitsCount(6); // optional } }
及其迁移
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateYourEloquentModelTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('your_eloquent_models', function (Blueprint $table) { $table->increments('id'); $table->string('otp_secret'); $table->integer('otp_counter'); $table->timestamps(); }); } }
获取代码
$model = new YourEloquentModel(); $model->otp(); # => "186522" $code = $model->otp(); # => "850738"
验证代码
$model->verify($code); # => true $model->verify($code); # => false
使用计数调整验证代码
$model->verify($code); # => true $model->otp_counter -= 1; $model->verify($code); # => true
测试
composer test
变更日志
请参阅 CHANGELOG 了解最近更改的更多信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全漏洞
请审查 我们的安全策略 了解如何报告安全漏洞。
致谢
许可证
MIT 许可证 (MIT)。请参阅 许可证文件 了解更多信息。