init
This commit is contained in:
125
Http/Controllers/Api/SecretController.php
Normal file
125
Http/Controllers/Api/SecretController.php
Normal file
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Google2FA\Http\Controllers\Api;
|
||||
|
||||
use App\Api\Controllers\Controller;
|
||||
use Exception;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Jason\Api\Api;
|
||||
use Modules\Google2FA\Models\Google2FA;
|
||||
|
||||
class SecretController extends Controller
|
||||
{
|
||||
protected ?Google2FA $google2fa;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function needInitialize()
|
||||
{
|
||||
$this->google2fa = Api::user()->google2fa;
|
||||
if (blank($this->google2fa)) {
|
||||
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
|
||||
{
|
||||
$this->needInitialize();
|
||||
|
||||
return $this->success($this->google2fa->google2fa_secret);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 获取密钥二维码地址
|
||||
*
|
||||
* @Date : 2022/12/1 14:31
|
||||
* @Author : <Jason.C>
|
||||
* @return JsonResponse
|
||||
* @throws Exception
|
||||
*/
|
||||
public function qrCodeUrl(): JsonResponse
|
||||
{
|
||||
$this->needInitialize();
|
||||
|
||||
return $this->success($this->google2fa->getQrCodeUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 开启两步验证
|
||||
*
|
||||
* @Date : 2022/12/1 15:42
|
||||
* @Author : <Jason.C>
|
||||
* @return JsonResponse
|
||||
* @throws Exception
|
||||
*/
|
||||
public function open(): JsonResponse
|
||||
{
|
||||
$this->needInitialize();
|
||||
|
||||
if ($this->google2fa->open()) {
|
||||
return $this->success('两步验证开启成功');
|
||||
} else {
|
||||
return $this->failed('开启失败');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 关闭两步验证
|
||||
*
|
||||
* @Date : 2022/12/1 15:43
|
||||
* @Author : <Jason.C>
|
||||
*/
|
||||
public function close(Request $request)
|
||||
{
|
||||
$this->needInitialize();
|
||||
$verify = $request->verify;
|
||||
|
||||
if (strlen($verify) != 6) {
|
||||
return $this->failed('请输入动态口令');
|
||||
}
|
||||
if (! $this->google2fa->verify($verify)) {
|
||||
return $this->failed('动态口令不正确');
|
||||
}
|
||||
|
||||
if ($this->google2fa->close()) {
|
||||
return $this->success('更新成功');
|
||||
} else {
|
||||
return $this->failed('更新失败');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 更新密钥
|
||||
*
|
||||
* @Date : 2022/12/1 15:29
|
||||
* @Author : <Jason.C>
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
* @throws Exception
|
||||
*/
|
||||
public function update(Request $request): JsonResponse
|
||||
{
|
||||
// 短信验证码
|
||||
$verify = $request->verify;
|
||||
|
||||
$this->needInitialize();
|
||||
|
||||
if ($this->google2fa->upgrade()) {
|
||||
return $this->success('更新成功');
|
||||
} else {
|
||||
return $this->failed('更新失败');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user