完善接口逻辑,验证码接入

This commit is contained in:
2022-12-02 12:32:03 +08:00
parent b134b73709
commit 26c1e68c64
5 changed files with 130 additions and 26 deletions

View File

@@ -8,6 +8,7 @@ use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Jason\Api\Api;
use Modules\Google2FA\Models\Google2FA;
use Modules\User\Facades\Sms;
class SecretController extends Controller
{
@@ -16,7 +17,7 @@ class SecretController extends Controller
/**
* @throws Exception
*/
public function needInitialize()
protected function needInitialize()
{
$this->google2fa = Api::user()->google2fa;
if (blank($this->google2fa)) {
@@ -25,23 +26,67 @@ class SecretController extends Controller
}
/**
* Notes : 获取密钥
* Notes : 短信验证码校验
*
* @Date : 2022/12/2 12:12
* @Author : <Jason.C>
* @throws Exception
*/
protected function checkSmsVerify(): void
{
$verify = request('verify');
if (strlen($verify) != Sms::getConfig('length')) {
throw new Exception('请输入短信验证码');
}
$check = Sms::checkCode(Api::user()->getUsername(), $verify, 'G2FA');
if (! $check) {
throw new Exception('短信验证码不正确');
}
}
/**
* Notes : 校验两步验证码
*
* @Date : 2022/12/2 12:23
* @Author : <Jason.C>
* @return void
* @throws Exception
*/
public function checkG2FAVerify(): void
{
$this->needInitialize();
$code = request('code');
if (strlen($code) != 6) {
throw new Exception('请输入动态口令');
}
if (! $this->google2fa->verify($code)) {
if (config('app.debug')) {
throw new Exception('动态口令不正确'.app('g2fa')->getCurrentOtp($this->google2fa->google2fa_secret));
} else {
throw new Exception('动态口令不正确');
}
}
}
/**
* Notes : 获取密钥-需要短信
*
* @Date : 2022/12/1 14:31
* @Author : <Jason.C>
* @param Request $request
* @return JsonResponse
* @throws Exception
*/
public function index(Request $request): JsonResponse
public function index(): JsonResponse
{
$this->needInitialize();
$this->checkSmsVerify();
return $this->success($this->google2fa->google2fa_secret);
}
/**
* Notes : 获取密钥二维码地址
* Notes : 获取密钥二维码地址-需要短信
*
* @Date : 2022/12/1 14:31
* @Author : <Jason.C>
@@ -51,12 +96,13 @@ class SecretController extends Controller
public function qrCodeUrl(): JsonResponse
{
$this->needInitialize();
$this->checkSmsVerify();
return $this->success($this->google2fa->getQrCodeUrl());
}
/**
* Notes : 开启两步验证
* Notes : 开启两步验证-需要短信
*
* @Date : 2022/12/1 15:42
* @Author : <Jason.C>
@@ -66,6 +112,11 @@ class SecretController extends Controller
public function open(): JsonResponse
{
$this->needInitialize();
if ($this->google2fa->status) {
return $this->failed('请勿重复开启');
}
$this->checkSmsVerify();
if ($this->google2fa->open()) {
return $this->success('两步验证开启成功');
@@ -75,45 +126,42 @@ class SecretController extends Controller
}
/**
* Notes : 关闭两步验证
* Notes : 关闭两步验证-需要短信-需要动态口令
*
* @Date : 2022/12/1 15:43
* @Date : 2022/12/2 12:20
* @Author : <Jason.C>
* @return JsonResponse
* @throws Exception
*/
public function close(Request $request)
public function close(): JsonResponse
{
$this->needInitialize();
$verify = $request->verify;
if (strlen($verify) != 6) {
return $this->failed('请输入动态口令');
}
if (! $this->google2fa->verify($verify)) {
return $this->failed('动态口令不正确');
if (! $this->google2fa->status) {
return $this->failed('请勿重复关闭');
}
$this->checkSmsVerify();
$this->checkG2FAVerify();
if ($this->google2fa->close()) {
return $this->success('更新成功');
return $this->success('关闭两步验证成功');
} else {
return $this->failed('更新失败');
return $this->failed('关闭两步验证失败');
}
}
/**
* Notes : 更新密钥
* Notes : 更新密钥-需要短信-需要动态口令
*
* @Date : 2022/12/1 15:29
* @Author : <Jason.C>
* @param Request $request
* @return JsonResponse
* @throws Exception
*/
public function update(Request $request): JsonResponse
public function update(): JsonResponse
{
// 短信验证码
$verify = $request->verify;
$this->needInitialize();
$this->checkSmsVerify();
$this->checkG2FAVerify();
if ($this->google2fa->upgrade()) {
return $this->success('更新成功');
@@ -121,5 +169,4 @@ class SecretController extends Controller
return $this->failed('更新失败');
}
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Modules\Google2FA\Http\Controllers\Api;
use App\Api\Controllers\Controller;
use Jason\Api\Api;
use Modules\User\Facades\Sms;
use Overtrue\EasySms\Exceptions\InvalidArgumentException;
use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
class SmsController extends Controller
{
/**
* Notes : 获取验证码
*
* @Date : 2022/12/2 11:54
* @Author : <Jason.C>
*/
public function send()
{
$user = Api::user();
try {
Sms::sendVerificationCode($user->getMobile(), 'G2FA');
return $this->success('验证码发送成功');
} catch (InvalidArgumentException|NoGatewayAvailableException $e) {
return $this->failed($e->getMessage());
}
}
}