morcen/laravel-otp-generator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package requires Laravel >= 8.x.

Installation

  1. Install the package via composer:

    composer require morcen/laravel-otp-generator
  2. Publish the config file with:

    php artisan vendor:publish --tag="otp-generator-config"

    Open config/otp.php and update the following:

    • identifier - this is what this package will use to query for the OTP record in the database
    • set - 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 from a to z
      • uppercase - English alphabet from A to Z
      • others- 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 in others option. This is the defualt option.
    • lifetime - defines how long the OTP will be valid for. Default is 15 minutes.
    • length - defines the default length of the OTP. Though this can be simply overriden whenever the generate or generateFor methods are called. Default length is 4.
    • encrypt - if set to true, it will have additional property hash in the returned object. If used with generateFor() method, the value that will be saved in the database will be the encrypted value. Available since v1.1.0.
    • alg - This will be the hashing algorithm used for the OTP code. This will only take effect if encrypt is set to true. Valid options are sha1 and md5. Available since v1.1.0.
  3. Publish the migrations with:

    php artisan vendor:publish --tag="otp-generator-migrations"

    and update it to include the identifier field 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');
    });
  4. 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 the identifier
  • $length (optional) - the length of the code to use. If this is not provided, it will use the default length option set in config/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 我们能提供哪些服务?
定制开发 / 二次开发

基于 morcen/laravel-otp-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 96
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-14