murkrow/simple-otp
Composer 安装命令:
composer require murkrow/simple-otp
包简介
README 文档
README
This package provides a simple way to manage One-Time Passwords (OTPs) for your application users. This package includes functionality for generating, validating, and managing OTPs associated with your user models in a Laravel application.
Installation
To install the Simple OTP package, run the following command:
composer require murkrow/simple-otp
Running migrations
Remember to run the migrations to create the otps table.
php artisan migrate
(Optional) Publishing vendor files
You can publish the config file and the migration using
php artisan vendor:publish
Usage
Adding the Trait to Your User Model
To enable OTP functionality for your user model, add the HasOtps trait to the model. Typically, this will be your User model.
namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use Murkrow\Otp\Traits\HasOtps; class User extends Authenticatable { use HasOtps; // Your user model code here }
Generating an OTP
To generate an OTP, use the OtpBuilder class. You can customize the OTP length, whether it is alphanumeric, its expiration time, and associate it with a user.
use Murkrow\Otp\Builders\OtpBuilder; $otp = (new OtpBuilder()) ->forUser($user) // User object ->length(6) // Length of the OTP ->alphaNumeric(true) // Alphanumeric or numeric ->tag('login') // Optional tag ->expiresInMinutes(30) // Expiration time in minutes ->create();
Validating an OTP
To validate an OTP, use the validateOtp method provided by the HasOtps trait:
$user = User::find(1); // User object $isValid = $user->validateOtp($otp, 'login'); // OTP and optional tag if ($isValid) { // OTP is valid } else { // OTP is invalid }
Validating and Removing an OTP
To validate and remove an OTP after successful validation, use the validateAndRemoveOtp method:
$isValid = $user->validateAndRemoveOtp($otp, 'login'); // OTP and optional tag if ($isValid) { // OTP is valid and removed } else { // OTP is invalid }
Example
Here is an example of how to generate an OTP for a user, validate it, and then remove it after successful validation:
use App\Models\User; use Murkrow\Otp\Builders\OtpBuilder; // Assume $user is an instance of the User model $user = User::find(1); // Generate an OTP $otpBuilder = new OtpBuilder(); $otp = $otpBuilder ->forUser($user) ->length(6) ->alphaNumeric(true) ->expiresInMinutes(30) ->create(); // Validate the OTP $isValid = $user->validateOtp($otp->code, 'login'); if ($isValid) { echo "OTP is valid!"; } else { echo "Invalid OTP!"; } // Validate and remove the OTP $isValidAndRemoved = $user->validateAndRemoveOtp($otp->code, 'login'); if ($isValidAndRemoved) { echo "OTP is valid and has been removed!"; } else { echo "Invalid OTP!"; }
Configuration
Maximum Number of OTPs Per User
By default, a user can have up to 5 OTPs at a time. If a user exceeds this limit, the oldest OTP will be deleted.
You can change this behavior by setting the max_otps_per_user configuration value in the otp configuration file.
// config/otp.php max_otps_per_user' => 5,
License
This package is open-sourced software licensed under the GNU General Public License v3.0.
murkrow/simple-otp 适用场景与选型建议
murkrow/simple-otp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 445 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 08 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 murkrow/simple-otp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 murkrow/simple-otp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 445
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2023-08-09