xtwoend / hyperf-otp
OTP服务支持TOTP
dev-master / 1.0.x-dev
2021-04-13 05:42 UTC
Requires
- hyperf-ext/mail: ^2.1
- hyperf/guzzle: ^2.1
- spomky-labs/otphp: ^10.0
This package is auto-updated.
Last update: 2024-09-13 13:15:32 UTC
README
此包旨在简化OTP验证流程的设置。无需烦恼,只需插入即可。按照以下步骤操作,您将能够获得一个完整工作的OTP验证系统。您可以使用它进行身份验证或电子商务生产销售、订单确认。
安装
安装包
使用composer安装此包
composer require xtwoend/hyperf-otp
发布配置
完成后,使用以下命令将配置发布到您的配置文件夹
php bin/hyperf.php vendor:publish xtwoend/hyperf-otp
此命令将创建一个config/otp.php
文件。
邮件配置
从.env
文件设置邮件配置。无需其他更改。
短信配置
由于短信网关使用不同的方法和额外的头和参数,您可能需要更新otp.php
文件中的短信配置。
迁移数据库
运行以下命令以创建otps表。
php bin/hyperf.php migrate
它将创建一个包含所需列的otps表。
环境
将以下键值对添加到Laravel应用程序中的.env
文件
# Basic OTP Configs
OTP_SERVICE='enabled'
OTP_TABLE_NAME='otps'
OTP_TIMEOUT=120
OTP_DIGIT=5
OTP_RESEND_SERVICE='enabled'
OTP_MAX_RETRY=2
OTP_MAX_RESEND=1
# Company and Service
OTP_SERVICE_NAME=
OTP_COMPANY_NAME=
# OTP via Email / SMS
OTP_SEND_BY_EMAIL=1
OTP_SEND_BY_SMS=1
# Email Configurations
OTP_EMAIL_FROM=
OTP_EMAIL_FROM_NAME=
OTP_EMAIL_SUBJECT=
# SMS Configurations
OTP_SMSC_URL='https://sms'
OTP_SMSC_METHOD=
OTP_COUNTRY_CODE=
OTP_SMSC_OVER_JSON=
OTP_SMSC_PARAM_TO_NAME=
OTP_SMSC_PARAM_MSG_NAME=
OTP_SMSC_USER=
OTP_SMSC_PASS=
定义
配置中功能的定义
- service:启用/禁用OTP服务
- timeout:OTP超时
- digit:OTP数字
- resend-service:启用/禁用重发服务
- max-retry:单个请求的最大重试次数
- max-resend:单个请求的最大重发次数
- service-name:服务使用的对象
- company-name:公司名称
- send-by:有两种方式共享OTP(电子邮件/短信)
- email:此密钥指定电子邮件所需的信息(例如,发件人、姓名、主题等)
- sms:配置与短信网关发送短信。(通用配置器)
OTP请求模板
一旦模板文件已发布,请打开template/
示例控制器
以下是在OtpController中调用OTP验证器的示例。
namespace App\Controllers; use Xtwoend\HyperfOtp\Object\OtpRequestObject; use Xtwoend\HyperfOtp\OtpValidator; use Xtwoend\HyperfOtp\Object\OtpValidateRequestObject; class OtpController extends Controller { /** * @return array */ public function requestForOtp() { return OtpValidator::requestOtp( new OtpRequestObject('1432', 'buy-shirt', '01711084714', 'ferdousul.haque@gmail.com') ); } /** * @param Request $request * @return array */ public function validateOtp(RequestInterface $request) { $uniqId = $request->input('uniqueId'); $otp = $request->input('otp'); return OtpValidator::validateOtp( new OtpValidateRequestObject($uniqId,$otp) ); } /** * @param Request $request * @return array */ public function resendOtp(RequestInterface $request) { $uniqueId = $request->input('uniqueId'); return OtpValidator::resendOtp($uniqueId); } }
将以下内容添加到routes/web.php
文件中。
Route::get('/test/otp-request', 'OtpController@requestForOtp');
Route::get('/test/otp-validate', 'OtpController@validateOtp');
Route::get('/test/otp-resend', 'OtpController@resendOtp');
响应/错误描述
以下表格描述了响应中生成的错误代码及其对应含义。
{ "code": 201, "message": "OTP Sent to the recipient", "requestId": 1432, "type": "buy-shirt" }
请求OTP响应代码
OTP验证响应代码
许可证
MIT
特别感谢
- Nahid Bin Azhar 提供反馈。
支持
- 对于任何错误,请帮助创建一个问题。
- 对于任何安装或配置问题,请随时联系我。 ferdousul.haque@gmail.com
特色文章
示例短信网关配置
Muthofun
如果您试图集成孟加拉国最受欢迎的短信网关之一,Muthofun是我们国家的一个流行的批量短信网关。以下是Muthofun短信网关的示例配置
'smsc' => [ 'url' => env('OTP_SMSC_URL'), 'method' => env('OTP_SMSC_METHOD', 'GET'), 'add_code' => env('OTP_COUNTRY_CODE',null), 'json' => env('OTP_SMSC_OVER_JSON',1), 'headers' => [], 'params' => [ 'send_to_param_name' => env('OTP_SMSC_PARAM_TO_NAME','number'), 'msg_param_name' => env('OTP_SMSC_PARAM_MSG_NAME','msg'), 'others' => [ 'user' => env('OTP_SMSC_USER'), 'password' => env('OTP_SMSC_PASS'), 'unicode' => 1 ], ] ];
.env文件将是以下内容
OTP_SMSC_URL='http://clients.muthofun.com:8901/esmsgw/sendsms.jsp?'
OTP_SMSC_METHOD='GET'
OTP_COUNTRY_CODE='88'
OTP_SMSC_OVER_JSON=0
OTP_SMSC_PARAM_TO_NAME='mobiles'
OTP_SMSC_PARAM_MSG_NAME='sms'
OTP_SMSC_USER='YourUserName'
OTP_SMSC_PASS='YourPassWord'
Infobip
以下是如何与知名的短信网关Infobip SMS平台集成的示例。
使用GET方法
'smsc' => [ 'url' => env('OTP_SMSC_URL'), 'method' => env('OTP_SMSC_METHOD', 'GET'), 'add_code' => env('OTP_COUNTRY_CODE',null), 'json' => env('OTP_SMSC_OVER_JSON',1), 'headers' => [], 'params' => [ 'send_to_param_name' => env('OTP_SMSC_PARAM_TO_NAME','number'), 'msg_param_name' => env('OTP_SMSC_PARAM_MSG_NAME','msg'), 'others' => [ 'username' => env('OTP_SMSC_USER'), 'password' => env('OTP_SMSC_PASS'), 'from' => 'InfoSMS', 'flash' => true ], ] ];
.env文件将是以下内容
OTP_SMSC_URL='https://{baseUrl}/sms/1/text/query?'
OTP_SMSC_METHOD='GET'
OTP_COUNTRY_CODE='88'
OTP_SMSC_OVER_JSON=0
OTP_SMSC_PARAM_TO_NAME='to'
OTP_SMSC_PARAM_MSG_NAME='text'
OTP_SMSC_USER='YourUserName'
OTP_SMSC_PASS='YourPassWord'
msg91
与msg91短信网关集成的示例。
使用GET方法
'smsc' => [ 'url' => env('OTP_SMSC_URL'), 'method' => env('OTP_SMSC_METHOD', 'GET'), 'add_code' => env('OTP_COUNTRY_CODE',null), 'json' => env('OTP_SMSC_OVER_JSON',1), 'headers' => [], 'params' => [ 'send_to_param_name' => env('OTP_SMSC_PARAM_TO_NAME','number'), 'msg_param_name' => env('OTP_SMSC_PARAM_MSG_NAME','msg'), 'others' => [ 'authkey' => 'YourAuthKey', 'sender' => 'YourSenderId', 'route' => '1', 'country' => '88', ], ], 'wrapper' => 'sms', ];
.env文件将是以下内容
OTP_SMSC_URL='https://control.msg91.com/api/v2/sendsms?'
OTP_SMSC_METHOD='POST'
OTP_COUNTRY_CODE='88'
OTP_SMSC_OVER_JSON=1
OTP_SMSC_PARAM_TO_NAME='to'
OTP_SMSC_PARAM_MSG_NAME='text'
OTP_SMSC_USER='YourUserName'
OTP_SMSC_PASS='YourPassWord'