init
This commit is contained in:
23
Config/config.php
Normal file
23
Config/config.php
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PragmaRX\Google2FA\Support\Constants;
|
||||||
|
|
||||||
|
return [
|
||||||
|
'name' => 'Google2FA',
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成的密钥长度
|
||||||
|
*/
|
||||||
|
'key_length' => 32,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 密钥默认加密方式
|
||||||
|
*/
|
||||||
|
'algorithm' => Constants::SHA1,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 密钥再生间隔
|
||||||
|
*/
|
||||||
|
'key_interval' => 30
|
||||||
|
|
||||||
|
];
|
||||||
28
Contracts/CanUseGoogle2FA.php
Normal file
28
Contracts/CanUseGoogle2FA.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Google2FA\Contracts;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Relations\MorphOne;
|
||||||
|
|
||||||
|
interface CanUseGoogle2FA
|
||||||
|
{
|
||||||
|
public function google2fa(): MorphOne;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notes : 获取用户名称
|
||||||
|
*
|
||||||
|
* @Date : 2022/12/1 13:47
|
||||||
|
* @Author : <Jason.C>
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getUsername(): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notes : 获取用户昵称
|
||||||
|
*
|
||||||
|
* @Date : 2022/12/1 13:47
|
||||||
|
* @Author : <Jason.C>
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getNickname(): string;
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
|
||||||
|
class CreateGoogle2FAsTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::create('google2fas', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->morphs('subscriber');
|
||||||
|
$table->text('google2fa_secret')->nullable();
|
||||||
|
$table->boolean('status')->default(0);
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('google2fas');
|
||||||
|
}
|
||||||
|
}
|
||||||
21
Google2FA.php
Normal file
21
Google2FA.php
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Google2FA;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Artisan;
|
||||||
|
|
||||||
|
class Google2FA
|
||||||
|
{
|
||||||
|
protected static string $mainTitle = '谷歌两步验证';
|
||||||
|
|
||||||
|
public static function install()
|
||||||
|
{
|
||||||
|
Artisan::call('migrate', [
|
||||||
|
'--path' => 'modules/Google2FA/Database/Migrations',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function uninstall()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
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('更新失败');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
142
Models/Google2FA.php
Normal file
142
Models/Google2FA.php
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Google2FA\Models;
|
||||||
|
|
||||||
|
use App\Models\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||||
|
use Illuminate\Support\Facades\Crypt;
|
||||||
|
|
||||||
|
class Google2FA extends Model
|
||||||
|
{
|
||||||
|
protected $table = 'google2fas';
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'status' => 'boolean',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected static function boot()
|
||||||
|
{
|
||||||
|
parent::boot();
|
||||||
|
|
||||||
|
self::creating(function ($model) {
|
||||||
|
$model->google2fa_secret = app('g2fa')->generateSecretKey(config('google2fa.key_length'));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notes : 所属模型
|
||||||
|
*
|
||||||
|
* @Date : 2022/12/1 13:28
|
||||||
|
* @Author : <Jason.C>
|
||||||
|
* @return MorphTo
|
||||||
|
*/
|
||||||
|
public function subscriber(): MorphTo
|
||||||
|
{
|
||||||
|
return $this->morphTo(__FUNCTION__, 'subscriber_type', 'subscriber_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notes : 更新密钥
|
||||||
|
*
|
||||||
|
* @Date : 2022/12/1 13:26
|
||||||
|
* @Author : <Jason.C>
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function upgrade(): bool
|
||||||
|
{
|
||||||
|
$this->google2fa_secret = app('g2fa')->generateSecretKey(config('google2fa.key_length'));
|
||||||
|
return $this->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notes : 开启两步验证
|
||||||
|
*
|
||||||
|
* @Date : 2022/12/1 15:45
|
||||||
|
* @Author : <Jason.C>
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function open(): bool
|
||||||
|
{
|
||||||
|
$this->status = 1;
|
||||||
|
return $this->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notes : 关闭两步验证
|
||||||
|
*
|
||||||
|
* @Date : 2022/12/1 15:44
|
||||||
|
* @Author : <Jason.C>
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function close(): bool
|
||||||
|
{
|
||||||
|
$this->status = 0;
|
||||||
|
return $this->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notes :
|
||||||
|
*
|
||||||
|
* @Date : 2022/12/1 13:31
|
||||||
|
* @Author : <Jason.C>
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getQRCodeUrl(): string
|
||||||
|
{
|
||||||
|
return app('g2fa')->getQRCodeUrl(
|
||||||
|
$this->subscriber->getUsername(),
|
||||||
|
$this->subscriber->getNickname().'@'.config('app.name'),
|
||||||
|
$this->google2fa_secret
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notes : 验证
|
||||||
|
*
|
||||||
|
* @Date : 2022/11/30 20:49
|
||||||
|
* @Author : <Jason.C>
|
||||||
|
* @param string $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function verify(string $value): bool
|
||||||
|
{
|
||||||
|
return app('g2fa')->verifyGoogle2FA($this->google2fa_secret, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notes : 获取当前验证码
|
||||||
|
*
|
||||||
|
* @Date : 2022/12/2 11:49
|
||||||
|
* @Author : <Jason.C>
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getCurrentOtp()
|
||||||
|
{
|
||||||
|
return app('g2fa')->getCurrentOtp($this->google2fa_secret);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notes : 存储到数据库的数据加密
|
||||||
|
*
|
||||||
|
* @Date : 2022/12/2 11:49
|
||||||
|
* @Author : <Jason.C>
|
||||||
|
* @param $value
|
||||||
|
*/
|
||||||
|
public function setGoogle2faSecretAttribute($value): void
|
||||||
|
{
|
||||||
|
$this->attributes['google2fa_secret'] = Crypt::encrypt($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notes : 解密
|
||||||
|
*
|
||||||
|
* @Date : 2022/12/2 11:49
|
||||||
|
* @Author : <Jason.C>
|
||||||
|
* @param $value
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getGoogle2faSecretAttribute($value): string
|
||||||
|
{
|
||||||
|
return Crypt::decrypt($value);
|
||||||
|
}
|
||||||
|
}
|
||||||
72
Providers/Google2FAServiceProvider.php
Normal file
72
Providers/Google2FAServiceProvider.php
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Google2FA\Providers;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Application;
|
||||||
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
|
class Google2FAServiceProvider extends ServiceProvider
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string $moduleName
|
||||||
|
*/
|
||||||
|
protected string $moduleName = 'Google2FA';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string $moduleNameLower
|
||||||
|
*/
|
||||||
|
protected string $moduleNameLower = 'google2fa';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Boot the application events.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function boot(): void
|
||||||
|
{
|
||||||
|
$this->registerConfig();
|
||||||
|
$this->loadMigrationsFrom(module_path($this->moduleName, 'Database/Migrations'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register the service provider.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function register(): void
|
||||||
|
{
|
||||||
|
$this->app->register(RouteServiceProvider::class);
|
||||||
|
|
||||||
|
$this->app->singleton('g2fa', function (Application $app) {
|
||||||
|
$google2fa = app('pragmarx.google2fa');
|
||||||
|
$google2fa->setAlgorithm($app->make('config')->get('google2fa.algorithm'));
|
||||||
|
$google2fa->setKeyRegeneration($app->make('config')->get('google2fa.key_interval'));
|
||||||
|
return $google2fa;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register config.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function registerConfig(): void
|
||||||
|
{
|
||||||
|
$this->publishes([
|
||||||
|
module_path($this->moduleName, 'Config/config.php') => config_path($this->moduleNameLower.'.php'),
|
||||||
|
], 'config');
|
||||||
|
$this->mergeConfigFrom(
|
||||||
|
module_path($this->moduleName, 'Config/config.php'), $this->moduleNameLower
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the services provided by the provider.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function provides(): array
|
||||||
|
{
|
||||||
|
return ['g2fa'];
|
||||||
|
}
|
||||||
|
}
|
||||||
28
Providers/RouteServiceProvider.php
Normal file
28
Providers/RouteServiceProvider.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Google2FA\Providers;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Route;
|
||||||
|
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
||||||
|
|
||||||
|
class RouteServiceProvider extends ServiceProvider
|
||||||
|
{
|
||||||
|
protected string $moduleNamespace = 'Modules\Google2FA\Http\Controllers';
|
||||||
|
|
||||||
|
protected string $moduleName = 'Google2FA';
|
||||||
|
|
||||||
|
public function map()
|
||||||
|
{
|
||||||
|
$this->mapApiRoutes();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function mapApiRoutes()
|
||||||
|
{
|
||||||
|
Route::as(config('api.route.as'))
|
||||||
|
->domain(config('api.route.domain'))
|
||||||
|
->middleware(config('api.route.middleware'))
|
||||||
|
->namespace($this->moduleNamespace.'\Api')
|
||||||
|
->prefix(config('api.route.prefix').'/'.strtolower($this->moduleName))
|
||||||
|
->group(module_path($this->moduleName, 'Routes/api.php'));
|
||||||
|
}
|
||||||
|
}
|
||||||
18
Routes/api.php
Normal file
18
Routes/api.php
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Routing\Router;
|
||||||
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
|
Route::group([
|
||||||
|
'middleware' =>config('api.route.middleware_auth'),
|
||||||
|
], function (Router $router) {
|
||||||
|
$router->get('secret', 'SecretController@index');
|
||||||
|
$router->get('secret/qr_code_url', 'SecretController@qrCodeUrl');
|
||||||
|
|
||||||
|
$router->post('secret/open', 'SecretController@open');
|
||||||
|
$router->post('secret/close', 'SecretController@close');
|
||||||
|
/**
|
||||||
|
* 更新密钥
|
||||||
|
*/
|
||||||
|
$router->put('secret', 'SecretController@update');
|
||||||
|
});
|
||||||
54
Traits/WithGoogle2FA.php
Normal file
54
Traits/WithGoogle2FA.php
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Google2FA\Traits;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Relations\MorphOne;
|
||||||
|
use Modules\Google2FA\Models\Google2FA;
|
||||||
|
|
||||||
|
trait WithGoogle2FA
|
||||||
|
{
|
||||||
|
public static function bootWithGoogle2FA()
|
||||||
|
{
|
||||||
|
self::created(function ($model) {
|
||||||
|
$model->google2fa()->create();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notes : 模型关联
|
||||||
|
*
|
||||||
|
* @Date : 2022/12/1 14:15
|
||||||
|
* @Author : <Jason.C>
|
||||||
|
* @return MorphOne
|
||||||
|
*/
|
||||||
|
public function google2fa(): MorphOne
|
||||||
|
{
|
||||||
|
return $this->morphOne(Google2FA::class, 'subscriber');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notes : 获取用户名
|
||||||
|
*
|
||||||
|
* @override
|
||||||
|
* @Date : 2022/12/1 14:15
|
||||||
|
* @Author : <Jason.C>
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getUsername(): string
|
||||||
|
{
|
||||||
|
return 'USER-NAME';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notes : 获取昵称
|
||||||
|
*
|
||||||
|
* @override
|
||||||
|
* @Date : 2022/12/1 14:15
|
||||||
|
* @Author : <Jason.C>
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getNickname(): string
|
||||||
|
{
|
||||||
|
return 'NICK-NAME';
|
||||||
|
}
|
||||||
|
}
|
||||||
24
composer.json
Normal file
24
composer.json
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"name": "uztech/google2fa-module",
|
||||||
|
"description": "",
|
||||||
|
"type": "laravel-module",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Jason.Chen",
|
||||||
|
"email": "chenjxlg@163.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"require": {
|
||||||
|
"pragmarx/google2fa-laravel": "^2.0",
|
||||||
|
"simplesoftwareio/simple-qrcode": "^4.2"
|
||||||
|
},
|
||||||
|
"extra": {
|
||||||
|
"module-dir": "modules"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Modules\\Google2FA\\": ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
15
module.json
Normal file
15
module.json
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"name": "Google2FA",
|
||||||
|
"alias": "google2fa",
|
||||||
|
"description": "谷歌两步验证",
|
||||||
|
"keywords": [],
|
||||||
|
"priority": 0,
|
||||||
|
"providers": [
|
||||||
|
"Modules\\Google2FA\\Providers\\Google2FAServiceProvider"
|
||||||
|
],
|
||||||
|
"aliases": {},
|
||||||
|
"files": [],
|
||||||
|
"requires": [],
|
||||||
|
"version": "1.0.0",
|
||||||
|
"author": "Jason.Chen"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user