tzsk/otp
Composer 安装命令:
composer require tzsk/otp
包简介
A secure, database-free One-Time Password (OTP) generator and verifier for PHP and Laravel.
关键字:
README 文档
README
A secure, database-free One-Time Password (OTP) generator and verifier for PHP. While primarily designed as a Laravel package, it can also be used independently in any PHP application.
📦 Installation
Via Composer:
composer require tzsk/otp
To publish the configuration file for Laravel, run:
php artisan otp:publish
🔥 Usage in Laravel
Import the facade class:
use Tzsk\Otp\Facades\Otp;
Generate an OTP:
$otp = Otp::generate($unique_secret); // Returns - string
The OTP generated above will only be successfully validated if the same unique secret is provided within the default expiration time.
TIP: OTPs are commonly used for user verification. The most straightforward approach to determining the
unique_secretis to use the user's email address, phone number, or User ID. You can also be creative with the unique secret, such as usingmd5($email)to create an MD5 hash of the user's email or phone number.
Match an OTP:
$valid = Otp::match($otp, $unique_secret); // Returns - boolean
Other Generate & Match Options:
There are other ways of generating or matching an 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 generate method above can be swapped with other generator methods. Ex - Otp::make($unique_secret); Otp::create($unique_secret);
Make sure to use the same configuration during validation. For example, if you specified 8 digits and a 30-minute expiration during creation, you must also specify 8 digits and a 30-minute expiration during verification.
// Match - (Different Runtime) // For the first example above Otp::check($otp, $unique_secret); // -> false Otp::digits(8)->check($otp, $unique_secret); // -> true // For the second example above Otp::check($otp, $unique_secret); // -> false Otp::expiry(30)->check($otp, $unique_secret); // -> true // For the third example above Otp::check($otp, $unique_secret); // -> false Otp::digits(8)->expiry(30)->check($otp, $unique_secret); // -> true
As demonstrated in the examples above, the exact configuration used to generate the OTP must be provided when matching the OTP with the secret.
Security Advantage: The primary advantage of requiring the same configuration during verification is that it prevents a malicious actor from using this tool to generate the same OTP for a targeted user without knowing the exact configuration parameters used.
🌊 Helper usage
You can use the package with the provided helper function as well:
$otp = otp()->make($secret); $otp = otp()->digits(8)->expiry(20)->make($secret);
😍 Usage outside Laravel
Install the package with Composer exactly as described above. Then, simply use the provided helper function.
Generate:
/** * You will need a directory in your filesystem where the package can store data. * Ensure you restrict access to this directory and its files using your web server configuration (Apache or Nginx). */ // Let's assume the directory you 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.
All functionalities remain identical to those documented in the Laravel Usage section. The only difference is that you use the $manager instance instead of the static Facade.
NOTE: You don't need to specify a path if you are using Laravel. The package will automatically detect and utilize Laravel's default cache store.
Example:
$manager->digits(...)->expiry(...)->generate($unique_secret); // And... $manager->digits(...)->expiry(...)->match($otp, $unique_secret);
Again, remember that when verifying an OTP, the digit and expiration configuration must match the settings used during generation.
🔬 Testing
composer test
📅 Changelog
Please see CHANGELOG for more information on what has changed recently.
❤️ Contributing
Please see CONTRIBUTING for details.
🔒 Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
👑 Credits
👮♂️ License
The MIT License (MIT). Please see License File for more information.
tzsk/otp 适用场景与选型建议
tzsk/otp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 665.43k 次下载、GitHub Stars 达 243, 最近一次更新时间为 2019 年 01 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「security」 「laravel」 「otp」 「2fa」 「Two-factor Authentication」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tzsk/otp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tzsk/otp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tzsk/otp 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provide a way to secure accesses to all routes of an symfony application.
It's a barebone security class written on PHP
Contao CMS integrity check for some files
A PHP security linter to detect insecure functions like var_dump, print_r, and other dangerous functions in your codebase
Alfabank REST API integration
Cbox SSRF — a hardened, config-driven guard against server-side request forgery for outbound URLs in Laravel. Blocks private/reserved/cloud-metadata targets, pins DNS, and refuses redirects.
统计信息
- 总下载量: 665.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 245
- 点击次数: 35
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-01-23
