morcen/laravel-otp-generator
Composer 安装命令:
composer require morcen/laravel-otp-generator
包简介
Generate OTP for your Laravel application
README 文档
README
Generate OTP for your Laravel application
This package requires Laravel >= 8.x.
Installation
-
Install the package via composer:
composer require morcen/laravel-otp-generator
-
Publish the config file with:
php artisan vendor:publish --tag="otp-generator-config"Open
config/otp.phpand update the following:identifier- this is what this package will use to query for the OTP record in the databaseset- this defines the characters that will be used for the code. Possible values are:numbers- from zero to nine (0 to 9)lowercase- English alphabet fromatozuppercase- English alphabet fromAtoZothers- defaults to empty string. You can put any other characters you wish to see as part of the OTP code.all- uses all letters and numbers, including the characters inothersoption. This is the defualt option.
lifetime- defines how long the OTP will be valid for. Default is15minutes.length- defines the default length of the OTP. Though this can be simply overriden whenever thegenerateorgenerateFormethods are called. Default length is4.encrypt- if set totrue, it will have additional propertyhashin the returned object. If used withgenerateFor()method, the value that will be saved in the database will be the encrypted value. Available sincev1.1.0.alg- This will be the hashing algorithm used for the OTP code. This will only take effect ifencryptis set totrue. Valid options aresha1andmd5. Available sincev1.1.0.
-
Publish the migrations with:
php artisan vendor:publish --tag="otp-generator-migrations"and update it to include the
identifierfield mentioned in the previous step. For example, if you have'identifier' => 'email'
in your
config/otp.php, you have to add this line in the migration file created:Schema::create('otps', function (Blueprint $table) { $table->id(); $table->string('email'); // <-- this is the line added to match the `identifier` $table->string('code'); $table->unsignedInteger('expiration'); $table->timestamp('created_at'); });
-
Run the migrations with:
php artisan migrate
Usage
To generate an OTP that can be validated afterwards, use the method generatedFor(). It accepts two parameters:
$identifierValue(required) - the value that will be matched against theidentifier$length(optional) - the length of the code to use. If this is not provided, it will use the defaultlengthoption set inconfig/otp.php.
For example, we want to create an OTP for the email abcd@example.com:
use Morcen\LaravelOtpGenerator\Facades\Otp; $otp = Otp::generateFor('abcd@example.com');
$otp receives back the OTP object*, e.g.
{
"email": "abcd@example.com",
"code": "028988",
"hash": "6120a26f84406f452c1b27509505093ba4aa263d",
"expiration": 1647363156
}
And then to validate, use the method validateFor, accepting the $identifierValue and the OTP as parameters and returns bool:
Otp::validateFor('abcd@example.com', '028988'); // returns `true`
*NOTE: hash property only appears in the OTP object if encrypt is set to true.
To generate and receive an OTP code only:
use Morcen\LaravelOtpGenerator\Facades\Otp; $otp = Otp::generate();
$otp receives back just the OTP object like this:
{
"code": "234198",
"hash": "858edae3093a5b6f5b7812cff2e2e369da0a2290"
}
*NOTE: hash property only appears in the OTP object if encrypt is set to true.
To override the default length of the OTP, pass the length as a parameter. For example, to generate an OTP that is 10 characters long:
$otp = Otp::generateFor('abcd@example.com', 10);
or
$otp = Otp::generate(10);
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.
morcen/laravel-otp-generator 适用场景与选型建议
morcen/laravel-otp-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 96 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 03 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「morcen」 「laravel-otp-generator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 morcen/laravel-otp-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 morcen/laravel-otp-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 morcen/laravel-otp-generator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Alfabank REST API integration
a lightweight implementation of generating OTP and validating backed by Cache Storage for Laravel
API gateway for Laravel
Laravel package for Accurate Online API integration.
A modern UI dashboard for selected Laravel Artisan commands
Shared RCX Laravel DataTables UI and configuration helpers.
统计信息
- 总下载量: 96
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-14