graxmonzo/laravel-one-time-password 问题修复 & 功能扩展

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

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

graxmonzo/laravel-one-time-password

Composer 安装命令:

composer require graxmonzo/laravel-one-time-password

包简介

One time password (OTP) generation trait for Laravel

README 文档

README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

Laravel implementation of Rails's active_model_otp package.

Simple OTP generation.

$code = $user->otp(); // => "324650"
$user->verify($code); // => true
$user->verify($code); // => false

Installation

You can install the package via composer:

composer require graxmonzo/laravel-one-time-password

Usage

Your Eloquent models should use the GraxMonzo\OneTimePassWord\HasOTP trait and the GraxMonzo\OneTimePassWord\OTPOptions class.

The trait contains an abstract method otpOptions() that you must implement yourself.

Your models' migrations should have a fields to save the OTP secret and counter to.

Here's an example of how to implement the trait:

namespace App;

use GraxMonzo\OneTimePassword\HasOTP;
use GraxMonzo\OneTimePassword\OTPOptions;
use Illuminate\Database\Eloquent\Model;

class YourEloquentModel extends Model
{
    use HasOTP;

    /**
     * Get the options for generating OTP.
     */
    public function otpOptions() : OTPOptions
    {
        return OTPOptions::create()
            ->saveToFields('otp_secret', 'otp_counter')
            ->digitsCount(6); // optional
    }
}

With its migration:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateYourEloquentModelTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('your_eloquent_models', function (Blueprint $table) {
            $table->increments('id');
            $table->string('otp_secret');
            $table->integer('otp_counter');
            $table->timestamps();
        });
    }
}

Get code

$model = new YourEloquentModel();

$model->otp(); # => "186522"
$code = $model->otp(); # => "850738"

Verify code

$model->verify($code); # => true
$model->verify($code); # => false

Verify code with counter adjust

$model->verify($code); # => true
$model->otp_counter -= 1;
$model->verify($code); # => true

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.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固