tzsk / otp
无需数据库的OTP生成器和验证器
Requires
- php: ^8.1
- illuminate/cache: ^8.0|^9.0|^10.0|^11.0
- illuminate/filesystem: ^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- laravel/legacy-factories: ^1.0
- laravel/pint: ^1.4
- orchestra/testbench: ^6.0|^7.0|^8.0|^9.0
- phpunit/phpunit: ^9.3|^10.5
- vimeo/psalm: ^5.0
- dev-master
- 8.0.0
- 7.0.0
- 6.0.1
- 6.0.0
- 5.1.3
- 5.1.2
- 5.1.1
- 5.1.0
- 5.0.1
- 5.0.0
- 4.0.1
- 4.0.0
- 3.0.0
- 2.0.0
- 1.0.1
- 1.0.0
- dev-dependabot/composer/phpunit/phpunit-10.5.24
- dev-dependabot/composer/vimeo/psalm-5.25.0
- dev-dependabot/composer/orchestra/testbench-8.23.2
- dev-dependabot/composer/laravel/pint-1.15.1
- dev-dependabot/composer/phpunit/phpunit-10.0.18
This package is auto-updated.
Last update: 2024-08-27 23:17:56 UTC
README
这是一个用于PHP(无需使用任何数据库)创建带有效期的OTP的工具。这是一个主要用于Laravel的包,但也可在Laravel之外使用。
📦 安装
通过Composer
composer require tzsk/otp
要发布laravel的配置文件,你可以运行
php artisan otp:publish
🔥 在Laravel中用法
导入外观类
use Tzsk\Otp\Facades\Otp;
生成OTP
$otp = Otp::generate($unique_secret); // Returns - string
上述生成的OTP将仅在默认有效期内的唯一密钥下进行验证。
提示: OTP通常用于用户验证。因此,确定
唯一密钥
的最简单方法通常是用户的电子邮件或电话号码。或者甚至是用户ID。你甚至可以创造性地考虑唯一密钥。你可以使用md5($email)
,即用户电子邮件或电话号码的md5。
匹配OTP
$valid = Otp::match($otp, $unique_secret); // Returns - boolean
其他生成和匹配选项
还有其他生成或匹配OTP的方法
// Generate - Otp::digits(8)->generate($unique_secret); // 8 Digits, Default expiry from config Otp::expiry(30)->generate($unique_secret); // 30 min expiry, Default digits from config Otp::digits(8)->expiry(30)->generate($unique_secret); // 8 digits, 30 min expiry // The above generate method can be swaped with other generator methods. Ex - Otp::make($unique_secret); Otp::create($unique_secret);
确保在检查时设置相同的配置。这意味着,如果你在创建时使用了8位数字和30分钟,你也将必须在使用检查时使用8位数字和30分钟。
// Match - (Different Runtime) // The first example above Otp::check($otp, $unique_secret); // -> false Otp::digits(8)->check($otp, $unique_secret); // -> true // The second example above Otp::check($otp, $unique_secret); // -> false Otp::expiry(30)->check($otp, $unique_secret); // -> true // The third example above Otp::check($otp, $unique_secret); // -> false Otp::digits(8)->expiry(30)->check($otp, $unique_secret); // -> true
在上面的示例中,在匹配OTP时,我们可以看到在匹配OTP时需要使用与创建OTP时相同的配置。
安全优势: - 使用相同的配置进行匹配的主要优势是第三方无法在没有了解配置的情况下使用此工具为相关用户生成相同的OTP。
🌊 辅助函数的使用
您还可以使用提供的辅助函数来使用此包
$otp = otp()->make($secret); $otp = otp()->digits(8)->expiry(20)->make($secret);
😍 Laravel之外的使用
使用与上述相同的方式通过composer安装包。然后只需使用提供的辅助函数即可。 生成:
/** * Now you need to have a directory in your filesystem where the package can do it's magic. * Make sure you prevent access to this directory and files using apache or ngnix config. */ // Let's assume the directory you have created is `./otp-tmp` $manager = otp('./otp-tmp'); /** * Default properties - * $digits -> 4 * $expiry -> 10 min */ $manager->digits(6); // To change the number of OTP digits $manager->expiry(20); // To change the mins until expiry $manager->generate($unique_secret); // Will return a string of OTP $manager->match($otp, $unique_secret); // Will return true or false.
所有功能与Laravel用法部分中记录的功能相同。这里只需使用实例而不是静态外观。
注意: 如果你使用Laravel,你不需要做任何事情。它将检测laravel的默认缓存存储。
示例
$manager->digits(...)->expiry(...)->generate($unique_secret); // And... $manager->digits(...)->expiry(...)->match($otp, $unique_secret);
同时,请记住,在匹配OTP时,请保持数字和有效期配置与生成OTP时相同。
🔬 测试
composer test
📅 更新日志
请参阅更新日志以获取有关最近更改的更多信息。
❤️ 贡献
请参阅贡献指南以获取详细信息。
🔒 安全漏洞
请查看我们的安全策略了解如何报告安全漏洞。
👑 信用
👮♂️ 许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。