aliirfaan / laravel-simple-otp
在Laravel中生成和验证OTP(一次性密码)
README
此包允许您生成OTP(一次性密码)。然后您可以验证OTP代码并根据其有效性授予访问权限。
灵活性
此包与Laravel Auth无关,您可以使用它向项目中任何模型发送OTP。例如,您可以向商家或客户模型发送OTP。无论是编写REST API还是基于视图的后端,我都尝试编写独立使用的方法。
功能
- 根据长度生成随机OTP
- 使用对象ID和对象类型将OTP代码与模型对象关联
- 对OTP代码进行哈希处理以提高安全性
- 基于存在性、相等性和过期性验证OTP代码
- 抛出自定义异常
- OTP过期、OTP未找到、OTP不匹配的事件
要求
安装
您可以使用Composer在现有的Laravel项目上安装此包
$ composer require aliirfaan/laravel-simple-otp
通过编辑config/app.php
文件并添加到提供者数组中来注册ServiceProvider
aliirfaan\LaravelSimpleOtp\SimpleOtpServiceProvider::class,
注意:使用以下内容适用于Laravel <5.1版本
'aliirfaan\LaravelSimpleOtp\SimpleOtpServiceProvider',
使用以下命令发布文件
$ php artisan vendor:publish --provider="aliirfaan\LaravelSimpleOtp\SimpleOtpServiceProvider"
或者仅使用php artisan vendor:publish
,并从输出列表中选择aliirfaan\LaravelSimpleOtp\SimpleOtpServiceProvider
。
应用ModelGotOtps
表的迁移
$ php artisan migrate
配置
此包在应用程序的config
文件夹中发布了一个otp.php
文件,其中包含此包的设置。大多数变量都绑定到环境变量,但您可以直接编辑此文件或将配置键添加到.env
文件中。
otp_does_expire | Bool (true or false)
OTP代码是否会过期
如果为true,则使用otp_timeout_seconds中指定的秒数来获取过期日期
'otp_does_expire' => true
otp_timeout_seconds | Numeric
OTP过期后的秒数
仅在otp_does_expire设置为true时考虑
'otp_timeout_seconds' => 180
otp_digit_length | Numeric
OTP将有多少位数字
'otp_digit_length' => 6
otp_should_encode | Bool (true or false)
是否在数据库中保存之前对OTP进行哈希处理
使用框架哈希来哈希OTP。请参阅Laravel文档中的安全 > 哈希
'otp_should_encode' => false
otp_should_simulate | Bool (true or false) 是否模拟OTP代码生成
'otp_should_simulate' => false
otp_simulate_fillable_digit | Numeric 如果启用模拟,则用于生成OTP的数字。生成的OTP将使用可填充的数字。例如:1111
'otp_simulate_fillable_digit' => 1
用法
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use aliirfaan\LaravelSimpleOtp\Models\ModelGotOtp; // otp model use aliirfaan\LaravelSimpleOtp\Services\OtpHelperService; // otp helper service use aliirfaan\LaravelSimpleOtp\Exceptions\ExpiredException; use aliirfaan\LaravelSimpleOtp\Exceptions\NotFoundException; use aliirfaan\LaravelSimpleOtp\Exceptions\NotMatchException; class TestController extends Controller { protected $otpModel; /** * Load model in constructor using dependency injection */ public function __construct(ModelGotOtp $otpModel) { $this->otpModel = $otpModel; } /** * Include our service using dependency injection */ public function test_send_otp(Request $request, OtpHelperService $otpHelperService) { // after an action like login, get yout model from the database $modelId = 1; $yourExampleModelObj = App\ExampleModel::find($modelId); // generate OTP, it will return an array with otp_code and otp_hash key $otpCode = $otpHelperService->generateOtpCode(); // model type can be anything but it must be unique if you want to send OTP to multiple model classes // it can also be the class name of the object. You get it using new \ReflectionClass($yourExampleModelObj))->getShortName() $modelType = 'exampleModel'; $phoneNumber = $yourExampleModelObj->phone; $otpData = [ 'model_id' => $modelId, 'model_type' => $modelType, 'otp_intent' => 'OTP_LOGIN', 'otp_code' => $otpCode['otp_hash'] ]; /** * create otp * use createOtp($otpData, false) to add a row for each otp sent * use createOtp($otpData) to update if row exists */ $createOtp = $this->otpModel->createOtp($otpData); // send otp using your own code $message = 'Your OTP is: '. $otpCode['otp_code']; } /** * Include our service using dependency injection */ public function test_verify_otp(Request $request, OtpHelperService $otpHelperService) { // normally you will get this via $request $modelId = 1; $modelType = 'exampleModel'; $otpIntent = 'OTP_LOGIN', $otpCode = '123456'; // get otp $otpObj = $this->otpModel->getOtp($modelId, $modelType, $otpIntent); // verify otp try { $otpCodeIsValid = $otpHelperService->otpCodeIsValid($otpObj, $otpCode); // update otp validated flag $updateOtp = $this->otpModel->updateOtp($otpObj->id); } catch (\aliirfaan\LaravelSimpleOtp\Exceptions\NotFoundException $e) { // } catch (\aliirfaan\LaravelSimpleOtp\Exceptions\NotMatchException $e) { // } catch (\aliirfaan\LaravelSimpleOtp\Exceptions\ExpiredException $e) { // } catch (\Exception $e) { // } } }
许可证
The MIT License (MIT)
版权所有 (c) 2020
特此授予任何获得此软件及其相关文档副本(“软件”)的人免费使用软件的权利,不受任何限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许向提供软件的人授予上述权利,前提是遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
本软件按照“现状”提供,不提供任何形式的质量保证,无论是明示的还是隐含的,包括但不限于适销性、特定目的适用性和非侵权性。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任承担责任,无论源于合同行为、侵权行为或其他行为,也不论与软件或其使用或其他方式相关。