定制 eagleeye/otp 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

eagleeye/otp

Composer 安装命令:

composer require eagleeye/otp

包简介

Laravel OTP Generator

README 文档

README

A laravel package to generate OTP.


🚀 Installation

Composer will allows you to quickly install via the command line.

  composer require eagleeye/otp

🚀Vendor Publishing

NOTE
Publishing vendors are optional only required if you are willing to change configuration and use database as OTP storage.


Publish config file

php artisan vendor:publish --provider="Eagleeye\Otp\OtpServiceProvider" --tag=config

Publish database migration file

php artisan vendor:publish --provider="Eagleeye\Otp\OtpServiceProvider" --tag=migrations

Configuration File

NOTE
You will find the OTP configuration file in config folder name as otp.php .


otp.php

<?php

return
[   /**
    * [Description for prefix]
    *
    * Example: G  | Output G-12345678
    * 
    */
    'prefix'=>null,

    /**
     * [Description for boot]
     *
     * Example: numeric         | Output  12345678
     * Example: alphabetic      | Output  ktylnfdgf
     * Example: alphanumeric    | Output  kt7l7fdg9
     * Example: mixnumeric      | Output  !45<45!)
     * Example: mixalphabetic   | Output  !ta<hg!)
     * Example: mixalphanumeric | Output  !t4<7g!)
     */
    'type'=>'numeric',

    /**
     * [Description for length]
     *
     * Example: 6    | Output 123456
     * Example: 8    | Output 12345678
     * 
     */
    'length'=>'6',

    /**
     * [Description for Mood]
     *
     * @return [type]
     * 
     */
    'mood'=>env('APP_ENV'),

    /**
     * [Description for storage]
     *
     * Example: databse     | OTP will store in databse
     * Example: cache       | OTP will store in cache
     * Example: session     | OTP will store in session
     */
    'storage'=>'database',

    /**
     * [Description for expire]
     *
     * OTP expire time 
     * In seconds
     * 
     */
    'expire'=>"60",

    /**
     * [Description for history]
     *
     * If true previous expired or validated otp wont delete
     * In boolean
     *
     */
    'history'=>false,


    /**
     * [Description for resend_in]
     *
     * Resend remaining time for interval mode
     * Should be smaller then expire time
     * In seconds
     *
     */
    'resend_in'=>"60",

    /**
     * [Description for duplicate]
     *
     * Send previously generated for resend till expire or validate
     * In boolean
     * Duplicate till expired
     *
     */
    'duplicate'=>false,


    /**
     * [Description for case]
     *
     * Example: lower    | Output werfghyt
     * Example: upper    | Output AFKUTDFG
     */
    'case'=>'lower',


    /**
     * [Description for table_name]
     *
     * Table name to create table in databse
     * 
     */
    'table_name'=>'otp_table'
];

  • Prefix

    • The prefix default value is null. If you would like to add a text or trademark or a single character like google registration OTP in every generated otp, Just add the prefix value.<br> Prefix and Actual OTP will be seperated by a ( - )

      Prefix Without Prefix With Prefix
      P 12345678 P-12345678
  • type

    • The type represent OTP string type .default value is numeric.<br> Prefix and Actual OTP will be seperated by a -

      Type Output Character Types
      numeric 12345678 Number
      alphabetic ktylnfdgf Alphabet
      alphanumeric kt7l7fdg9 Number,Alphabet
      mixnumeric !45<45! Number,Special Character
      mixalphabetic !tad%hgr Alphabet,Special Character
      mixalphanumeric !t4<7g! Number,Alphabet,Special Character
  • Length

    • The length represent total number of character's in generated OTP .Default length is 6.

      Length OTP
      6 123456
      8 12345678
  • Storage

    • Cache Storage used as default storage to store OTP .Dont forget to publish Otp migration file before using database as OTP storage.

      storage Options:

      • cache
      • session
      • database
  • Expire

    • expire - The validity period of the OTP in seconds
  • Case

    • The case - differentiating between capital and lower-case letters.

      Case OTP
      lower generator
      upper GENERATOR
  • Table Name

    • table_name - With the name OTP databse migration table will be created .otp_table is default table name.


🚀Usage


Import OTP facade

  use Eagleeye\Otp\Facades\OTP;

Static Function : get

//Facade accessor public function
public function get(String $key)
{
         //processing...
}

This will generate a OTP that valid till expire time(configured in config file),For every otp request the method response new otp string with new expire time:


  • $key: The key that will be tied to the OTP.

Example

<?php

$otp = OTP::get('usertoken');

echo $otp;

// 20220317221648
// https://example/url

"267958"

Static Function : interval

//Facade accessor public function
public function interval(String $key)
{
        //processing...
}

This will generate a OTP that valid till expire time(configured in config file),for new request it will generate new otp if only previous otp with the $key expired or empty:


  • $key: The key that will be tied to the OTP.

Example if otp hasnt expired

<?php

$otp = OTP::interval('usertoken');

echo $otp;

// 20220317221648
// https://example/url

array:2 [▼
  "expired" => false,
  "remaining" => "00 00:01:49" //Remaining time of expiration
]

Example if otp expired

<?php

$otp = OTP::interval('usertoken');

echo $otp;

// 20220317221648
// https://example/url

array:2 [▼
  "expired" => true,
  "otp" => "282561", //New otp
  "remaining" => "00 00:01:49" //Remaining time of expiration
]

Static Function : action

//Facade accessor public function
public function action(String $key,Callable $callback)
{
     //processing...
}

This will generate a OTP that valid till expire time(configured in config file),For every otp request the method response new otp string with new expire time. The function takes one extra callable parameter to do additional work's(SMS,EMAIL,etc..) before returning otp string:


  • $key: The key that will be tied to the OTP.
  • $callback: Take a callable function . Specially usefull to implement sms gateway's ,Email,etc...

Example

<?php

$otp = OTP::action('usertoken',function($otp){
    //Sms::send('+8801*******',$otp);
});

echo $otp;

// 20220317221648
// https://example/url

"267958"

Static Function : intervalaction

//Facade accessor public function
public function intervalaction(String $key,Callable $callback)
{
     //processing...
}

Same as Interval the function will generate new otp until previous one expired:


  • $key: The key that will be tied to the OTP.
  • $callback: Take a callable function . Specially usefull to implement sms gateway's ,Email,etc...

Example if otp hasnt expired

<?php

$otp = OTP::intervalaction('usertoken',function($otp){
    //Sms::send('+8801*******',$otp);
});

echo $otp;

// 20220317221648
// https://example/url

array:2 [▼
  "expired" => false
  "remaining" => "00 00:01:49" //Remaining time of expiration
]

Example if otp expired

<?php

$otp = OTP::intervalaction('usertoken',function($otp){
    //Sms::send('+8801*******',$otp);
});

echo $otp;

// 20220317221648
// https://example/url

array:2 [▼
  "expired" => true
  "otp" => "282561" //New otp
  "remaining" => "00 00:01:49" //Remaining time of expiration
]

Static Function : Verify

//Facade accessor public function
public function verify($key,$otp)
{
     //processing...
}

This verify function will verify otp with generated otp and expired the otp from system.


Example

<?php

$result=OTP::verify('usertoken','12345678');
echo $result;


"true"

Static Function : Readonly

//Facade accessor public function
public function readonly($options=nullable)
{
     //processing...
}

This will return a random generated string for other uses.


  • $options: It takes a array as parameter to replace Configuration file parameters.

Example

<?php

$result=OTP::readonly(['prefix'=>'sn','length'=>15,'case'=>'upper','type'=>'alphabetic']);
echo $result;

// 20220331221403
// http://otp.test/

"sn-ISUQXTFPQYJIMSR"



Author

👤 Shuvo Dewan



Contribution

If you find an issue with this package or you have any suggestion please help out.

eagleeye/otp 适用场景与选型建议

eagleeye/otp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 278 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 03 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 eagleeye/otp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 eagleeye/otp 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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