rahmatwaisi / otp-auth
使用laravel缓存轻松生成和验证OTP
v1.0.2
2023-09-15 14:29 UTC
Requires
- php: ^8.0.2
- illuminate/support: ^8.0|^9.0|^10.0
Requires (Dev)
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-15 16:46:08 UTC
README
安装
您可以使用Composer安装此包。
composer require rahmatwaisi/otp-auth
使用方法
创建OTP
您只需使用如下所示的 OtpGenerator
门面
use RahmatWaisi\OtpAuth\Facades\OtpGenerator; use RahmatWaisi\OtpAuth\Core\OtpType; . . // To Create a new OTP using a key like User::$id or User::$email, etc. $otp = OtpGenerator::create('key') // output: 234234
另一种更可定制的做法是确定前缀、密钥、TTL、长度、类型等。
$user = User::query()->inRandomOrder()->first(); $otp = OtpGenerator::createFrom('smth', $user->id) // output: 234234, a 6-digit integer $otp = OtpGenerator::createFrom('smth', $user->email, OtpType::NUMBER) // output: 234234, a 6-digit integer $otp = OtpGenerator::createFrom('smth', $user->username, OtpType::STRING) // output: "aBcDeF", a 6-char string $otp = OtpGenerator::createFrom('smth', $user->custom_key, OtpType::NUMBER, 12, now()->addDay()) // output: 123321234234, a 12-digit integer $otp = OtpGenerator::createFrom('smth', $user->whatever, OtpType::STRING, 8, now()->addDay()) // output: "aBcdEfgH", an 8-char string
上述所有代码都使用了包中的可发布配置,位于configs/otp.php
您还可以在运行时自定义任何配置的另一种方法是
$otp = OtpGenerator::builder() ->withPrefix('dummy_prefix') ->withKey('my_custom_key') ->withType(OtpType::NUMBER) ->withTtl(now()->addMinutes(2)) ->withLength(8) ->build(); // Output: 12345678, an 8-digit integer
解决、验证、删除OTP
您只需使用如下所示的 OtpGenerator
门面
use RahmatWaisi\OtpAuth\Facades\OtpGenerator; use RahmatWaisi\OtpAuth\Core\OtpType; . . // To Remove, Forget or Resolve an OTP using a key like User::$id or User::$email, etc. Do like this: // Forgets the OTP just using the key OtpGenerator::forget($user->id); // Checks wether an incoming OTP from request exists or not? OtpGenerator::verify($user->id, request()->get('otp')); OtpGenerator::verifyUsing('custom_prefix', $user->id, request()->get('otp')); // Removes an OTP using both key and its value OtpGenerator::remove($key, 'aBcDeFgH'); // Gets an OTP just using the key OtpGenerator::get($key); OtpGenerator::get($user->username);
另一种更可定制的做法是确定前缀、密钥、TTL、长度、类型等。
$user = User::query()->inRandomOrder()->first(); // Get an OTP just using the key and default configs OtpGenerator::resolver()->withDefaultSettings()->withKey($user->id)->resolve(); // Forgets an OTP just using the key and default configs OtpGenerator::resolver()->withDefaultSettings()->withKey($user->id)->forget(); // Checks for existence of an OTP just using the key and default configs OtpGenerator::resolver()->withDefaultSettings()->withKey($user->id)->exists(request()->get('otp')); // Get an OTP just using the key and default prefix OtpGenerator::resolver()->withDefaultPrefix()->withKey($user->id)->resolve(); // Checks for existence of an OTP just using the key and default perfix OtpGenerator::resolver()->withDefaultPrefix()->withKey($user->id)->exists(request()->get('otp'));
上述所有代码都使用了包中的可发布配置,位于configs/otp.php
您还可以在运行时自定义任何配置的另一种方法是
// Get an OTP just using the key and custom prefix $otp = OtpGenerator::resolver() ->withPrefix('smth') ->withKey($user->id) ->resolve();
结论
通过使用此包,您可以轻松地创建和验证任何长度的OTP表单。
贡献
请随时提出改进建议,我将非常感激来自任何人的任何贡献。谢谢。
在深夜深夜制作的 💙。