完善接口逻辑,验证码接入
This commit is contained in:
@@ -25,4 +25,13 @@ interface CanUseGoogle2FA
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getNickname(): string;
|
public function getNickname(): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notes : 获取绑定手机号
|
||||||
|
*
|
||||||
|
* @Date : 2022/12/2 12:10
|
||||||
|
* @Author : <Jason.C>
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getMobile(): string;
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,7 @@ use Illuminate\Http\JsonResponse;
|
|||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Jason\Api\Api;
|
use Jason\Api\Api;
|
||||||
use Modules\Google2FA\Models\Google2FA;
|
use Modules\Google2FA\Models\Google2FA;
|
||||||
|
use Modules\User\Facades\Sms;
|
||||||
|
|
||||||
class SecretController extends Controller
|
class SecretController extends Controller
|
||||||
{
|
{
|
||||||
@@ -16,7 +17,7 @@ class SecretController extends Controller
|
|||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function needInitialize()
|
protected function needInitialize()
|
||||||
{
|
{
|
||||||
$this->google2fa = Api::user()->google2fa;
|
$this->google2fa = Api::user()->google2fa;
|
||||||
if (blank($this->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
|
* @Date : 2022/12/1 14:31
|
||||||
* @Author : <Jason.C>
|
* @Author : <Jason.C>
|
||||||
* @param Request $request
|
|
||||||
* @return JsonResponse
|
* @return JsonResponse
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function index(Request $request): JsonResponse
|
public function index(): JsonResponse
|
||||||
{
|
{
|
||||||
$this->needInitialize();
|
$this->needInitialize();
|
||||||
|
$this->checkSmsVerify();
|
||||||
|
|
||||||
return $this->success($this->google2fa->google2fa_secret);
|
return $this->success($this->google2fa->google2fa_secret);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Notes : 获取密钥二维码地址
|
* Notes : 获取密钥二维码地址-需要短信
|
||||||
*
|
*
|
||||||
* @Date : 2022/12/1 14:31
|
* @Date : 2022/12/1 14:31
|
||||||
* @Author : <Jason.C>
|
* @Author : <Jason.C>
|
||||||
@@ -51,12 +96,13 @@ class SecretController extends Controller
|
|||||||
public function qrCodeUrl(): JsonResponse
|
public function qrCodeUrl(): JsonResponse
|
||||||
{
|
{
|
||||||
$this->needInitialize();
|
$this->needInitialize();
|
||||||
|
$this->checkSmsVerify();
|
||||||
|
|
||||||
return $this->success($this->google2fa->getQrCodeUrl());
|
return $this->success($this->google2fa->getQrCodeUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Notes : 开启两步验证
|
* Notes : 开启两步验证-需要短信
|
||||||
*
|
*
|
||||||
* @Date : 2022/12/1 15:42
|
* @Date : 2022/12/1 15:42
|
||||||
* @Author : <Jason.C>
|
* @Author : <Jason.C>
|
||||||
@@ -66,6 +112,11 @@ class SecretController extends Controller
|
|||||||
public function open(): JsonResponse
|
public function open(): JsonResponse
|
||||||
{
|
{
|
||||||
$this->needInitialize();
|
$this->needInitialize();
|
||||||
|
if ($this->google2fa->status) {
|
||||||
|
return $this->failed('请勿重复开启');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->checkSmsVerify();
|
||||||
|
|
||||||
if ($this->google2fa->open()) {
|
if ($this->google2fa->open()) {
|
||||||
return $this->success('两步验证开启成功');
|
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>
|
* @Author : <Jason.C>
|
||||||
|
* @return JsonResponse
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function close(Request $request)
|
public function close(): JsonResponse
|
||||||
{
|
{
|
||||||
$this->needInitialize();
|
$this->needInitialize();
|
||||||
$verify = $request->verify;
|
if (! $this->google2fa->status) {
|
||||||
|
return $this->failed('请勿重复关闭');
|
||||||
if (strlen($verify) != 6) {
|
|
||||||
return $this->failed('请输入动态口令');
|
|
||||||
}
|
|
||||||
if (! $this->google2fa->verify($verify)) {
|
|
||||||
return $this->failed('动态口令不正确');
|
|
||||||
}
|
}
|
||||||
|
$this->checkSmsVerify();
|
||||||
|
$this->checkG2FAVerify();
|
||||||
|
|
||||||
if ($this->google2fa->close()) {
|
if ($this->google2fa->close()) {
|
||||||
return $this->success('更新成功');
|
return $this->success('关闭两步验证成功');
|
||||||
} else {
|
} else {
|
||||||
return $this->failed('更新失败');
|
return $this->failed('关闭两步验证失败');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Notes : 更新密钥
|
* Notes : 更新密钥-需要短信-需要动态口令
|
||||||
*
|
*
|
||||||
* @Date : 2022/12/1 15:29
|
* @Date : 2022/12/1 15:29
|
||||||
* @Author : <Jason.C>
|
* @Author : <Jason.C>
|
||||||
* @param Request $request
|
|
||||||
* @return JsonResponse
|
* @return JsonResponse
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function update(Request $request): JsonResponse
|
public function update(): JsonResponse
|
||||||
{
|
{
|
||||||
// 短信验证码
|
|
||||||
$verify = $request->verify;
|
|
||||||
|
|
||||||
$this->needInitialize();
|
$this->needInitialize();
|
||||||
|
$this->checkSmsVerify();
|
||||||
|
$this->checkG2FAVerify();
|
||||||
|
|
||||||
if ($this->google2fa->upgrade()) {
|
if ($this->google2fa->upgrade()) {
|
||||||
return $this->success('更新成功');
|
return $this->success('更新成功');
|
||||||
@@ -121,5 +169,4 @@ class SecretController extends Controller
|
|||||||
return $this->failed('更新失败');
|
return $this->failed('更新失败');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
30
Http/Controllers/Api/SmsController.php
Normal file
30
Http/Controllers/Api/SmsController.php
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,4 +15,9 @@ Route::group([
|
|||||||
* 更新密钥
|
* 更新密钥
|
||||||
*/
|
*/
|
||||||
$router->put('secret', 'SecretController@update');
|
$router->put('secret', 'SecretController@update');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取短信验证码
|
||||||
|
*/
|
||||||
|
$router->post('sms', 'SmsController@send');
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -51,4 +51,17 @@ trait WithGoogle2FA
|
|||||||
{
|
{
|
||||||
return 'NICK-NAME';
|
return 'NICK-NAME';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notes : 获取绑定手机号
|
||||||
|
*
|
||||||
|
* @override
|
||||||
|
* @Date : 2022/12/2 12:11
|
||||||
|
* @Author : <Jason.C>
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getMobile(): string
|
||||||
|
{
|
||||||
|
return 'MOBILE';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user