承接 nealyip/laravel-otp-validation 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

nealyip/laravel-otp-validation

Composer 安装命令:

composer require nealyip/laravel-otp-validation

包简介

Validate before making action with otp

README 文档

README

The packages handle otp in various way for you. It is designed for modal dialog and called by ajax.

Install

composer require nealyip/laravel-otp-validation

Add this provider to config/app.php

Nealyip\LaravelOTPValidation\Providers\OTPServiceProvider::class,

Publish config

php .\artisan vendor:publish --provider=Nealyip\LaravelOTPValidation\Providers\OTPServiceProvider

Configuration

You may translate error message under resources/lang/vendor/otp_messages

By default it use log transport service, you may change it to the given mail service or write your own sms provider.

For the given mail service, simply change config/otp_validation.php

// for example
'transport'    => \Nealyip\LaravelOTPValidation\Transport\MailTransport::class,

for the default mail transport class, define your mail driver, from address and name (MAIL_FROM_ADDRESS and MAIL_FROM_NAME for smtp)

OTP_SEED you can change the seed for otp by changing OTP_SEED env
check otp_validation.php for more details

Development

To implement your own transport interface, simply implement the Nealyip\LaravelOTPValidation\Transport\TransportInterface interface. for example,

namespace App\SMS\Transport;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Nealyip\LaravelOTPValidation\Transport\TransportInterface;

class SMSTransport implements TransportInterface {
    /**
     * @var Client 
     */
    private $_client;

    public function __construct()
    {
        $this->_client = new Client();
    }

    /**
     * @inheritDoc
     */
    public function type()
    {
        return static::TYPE_SMS;
    }
    
    /**
     * @inheritdoc
     */
    public function send($phone_number, $message)
    {

        $sms_url = 'https://someurl';
        $parsed_url = parse_url($sms_url);
        $host       = $parsed_url['host'];

        try {

            $this->_client->request('POST', $sms_url, [
                'http_errors' => true,
                'headers'     => [
                    'Host'         => $host,
                    'Authorization'=> 'Bearer ' . config('sms.auth_code') ,
                    'Content-Type' => 'application/json',
                    'Accept'       => 'application/json'
                ],
                'json'        => compact('phone_number', 'message')
            ]);
        } catch (GuzzleException $e) {
            throw new \Exception(trans('error.fail_to_send'), 0, $e);
        }
    }
}

and change your config file (config/otp_validation.php)

'transport'    => App\SMS\Transport\SMSTransport::class,

How to use

First you are required to implement Nealyip\LaravelOTPValidation\OTP\OTPTarget to your user model. Implement the target user mobile number and email address functions for otp transportation.

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements Nealyip\LaravelOTPValidation\OTP\OTPTarget {

    /**
     * Provide the email for the user used by the Email Provider, 
     * the return value may varies by the scene.
     * May return empty string if you use only SMS otp.
     *
     * @param string $scene
     * @return string
     */
    public function otpEmail($scene = null)
    {

        return $this->email;
    }
    
    /**
     * Provide the mobile phone number for the user used by the SMS Provider,
     * the return value may varies by the scene.
     *
     * @param null $scene
     * @return string
     */
    public function otpMobile($scene = null)
    {

        return $this->country_code . $this->area_code . $this->mobile_number;
    }

    /**
     * A unique user id, for example
     *
     * @return string
     */
    public function otpIdentifier()
    {
        return $this->id;
    }
}

Add routes for password attempt and resend request

Route::group(['prefix' => 'backend'], function(){
    otp_validation_routes();
});

To send the first otp, just call the send method from the OTPValidationService

use Nealyip\LaravelOTPValidation\Services\OTPValidationService;

class FormController{

protected $_service;

public function __construct(OTPValidationService $service) {
    $this->_service = $service;
}

public function create(){
    ...
    $user = request()->user();
    $payload = $this->_service->scene('changepassword')->send($user, ['id' => $user->id], [], 'You are about to change your password, please complete with this one time password :otp');
    
    return response()->json(['sent' => true, 'key' => $payload->key]);   
}

And you may build a modal dialog from the client side and ask for the password.

To attempt a password, just call the routes, for example

PUT /backend/otp_validation/{key}
with json message body
{otp: 'password'}

to request resend

POST /backend/otp_validation/{key}

if the otp is correct it will return a success response with HTTP status code 200

you can then consume the otp

use Nealyip\LaravelOTPValidation\Services\OTPValidationService;

class FormController{

protected $_service;

public function __construct(OTPValidationService $service) {
    $this->_service = $service;
}

public function create(Request $request){
    ...
    
        $user = request()->user();
    if ($request->input('otp')) {
        // WrongTargetException will be thrown if the otp is incorrect
        $this->_service->scene('changepassword')->consume($user, ['id' => $user->id], $request->input('otp'));    
        
        // success validate the otp
        // do your staff here
        
        return .....
    } else {
        $payload = $this->_service->scene('changepassword')->send($user, ['id' => $user->id], [], 'You are about to change your password, please complete with this one time password :otp');
        
        return response()->json(['sent' => true, 'key' => $payload->key]);
    }   
}

nealyip/laravel-otp-validation 适用场景与选型建议

nealyip/laravel-otp-validation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 248 次下载、GitHub Stars 达 9, 最近一次更新时间为 2017 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 9
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-09-21