feat: admin login

This commit is contained in:
2024-10-27 00:34:45 +08:00
parent 3a39ff3790
commit d76e37a81d
18 changed files with 698 additions and 3 deletions

View File

@@ -15,7 +15,7 @@ class AdminReturn
* @param array $debug
* @return array
*/
public static function success(string $msg = 'success', array $data = [], int $code = ReturnCode::SUCCESS, array $debug = []): array
public function success(string $msg = 'success', array $data = [], int $code = ReturnCode::SUCCESS, array $debug = []): array
{
$res = [
'code' => $code,
@@ -34,7 +34,7 @@ class AdminReturn
* @param array $debug
* @return array
*/
public static function error(string $msg = 'error', array $data = [], int $code = ReturnCode::ERROR, array $debug = []): array
public function error(string $msg = 'error', int $code = ReturnCode::ERROR, array $data = [], array $debug = []): array
{
$res = [
'code' => $code,

View File

@@ -0,0 +1,42 @@
<?php
namespace App\Lib\Crypto;
use Exception;
class AdminPasswordCrypto implements CryptoInterface
{
/**
* 明文密码
* @var string
*/
public string $data = '';
/**
* 加密盐
* @var string
*/
public string $salt = '';
/**
* admin password 加密
* @return string
*/
public function encrypt(): string
{
try {
return hash("sha256", $this->salt . hash("sha256", $this->salt . $this->data));
} catch (Exception) {
return '';
}
}
/**
* 解密 不需要解密
* @return string
*/
public function decrypt(): string
{
return '';
}
}

View File

@@ -0,0 +1,76 @@
<?php
/**
* This lib file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Lib\Crypto;
use Exception;
use function Hyperf\Config\config;
/**
* 接口加密类
*/
class ApiCrypto implements CryptoInterface
{
/**
* 加密数据
* @var string
*/
public string $data = '';
/**
* 加密key
* @var string
*/
private string $key;
/**
* 构造方法 配置写入
*/
public function __construct()
{
$this->key = config('system.api_return_key');
}
/**
* api加密接口
* @return string
*/
public function encrypt(): string
{
try {
//设置偏移量
$iv = substr(md5($this->data), 0, 16);
//使用 openssl 加密数据
$encrypted = openssl_encrypt($this->data,'AES-128-CBC',$this->key,OPENSSL_RAW_DATA,$iv);
return $iv.'|'.base64_encode($encrypted);
} catch (Exception) {
return '';
}
}
/**
* api解密接口
* @return string
*/
public function decrypt(): string
{
try {
$array = explode('|',$this->data);
//获取偏移量
$iv = $array[0];
//获取加密数据
$encrypted = base64_decode($array[1]);
//使用 openssl 解密数据 并返回
return openssl_decrypt($encrypted, 'AES-128-CBC',$this->key,OPENSSL_RAW_DATA,$iv);
} catch (Exception) {
return '';
}
}
}

View File

@@ -0,0 +1,57 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Lib\Crypto;
use Exception;
/**
* 加密工厂
*/
class CryptoFactory
{
/**
* 加密类主体
* @var CryptoInterface
*/
protected CryptoInterface $cryptoInterface;
/**
* 加密工厂
* @param string $type
* @param string $dataStr
* @param string $key
* @return ApiCrypto|CryptoInterface|JwtCrypto
* @throws Exception
*/
public function cryptoClass(string $type, string $dataStr, string $key = ''): JwtCrypto|CryptoInterface|ApiCrypto
{
switch ($type) {
case 'api':
$apiCrypto = new ApiCrypto();
$this->cryptoInterface = $apiCrypto;
break;
case 'jwt':
$jwtCrypto = new JwtCrypto();
$this->cryptoInterface = $jwtCrypto;
break;
case 'admin-password':
$adminCrypto = new AdminPasswordCrypto();
$this->cryptoInterface = $adminCrypto;
$this->cryptoInterface->salt = $key;
break;
default:
throw new Exception('The encryption algorithm does not exist');
}
$this->cryptoInterface->data = $dataStr;
return $this->cryptoInterface;
}
}

View File

@@ -0,0 +1,20 @@
<?php
/**
* This lib file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Lib\Crypto;
/**
* 加密类接口
*/
interface CryptoInterface
{
public function encrypt();
public function decrypt();
}

View File

@@ -0,0 +1,80 @@
<?php
/**
* This lib file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Lib\Crypto;
use Exception;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use function Hyperf\Config\config;
class JwtCrypto implements CryptoInterface
{
/**
* 加密数据
* @var string
*/
public string $data = '';
/**
* 加密 key
* @var string
*/
private string $key;
/**
* 加密过期时间
* @var int
*/
private int $expire;
/**
* 构造函数 获取配置
*/
public function __construct()
{
$this->key = config('system.jwt_key');
$this->expire = (int)config('system.jwt_expire');
}
/**
* jwt 加密
* @return string
*/
public function encrypt(): string
{
try {
$time = time();
$payload = [
'iat' => $time, //签发时间
'nbf' => $time, //(Not Before)某个时间点后才能访问比如设置time+30表示当前时间30秒后才能使用
'exp' => $time + $this->expire,
'data' => json_decode($this->data,true),
];
return JWT::encode($payload, $this->key,'HS256');
} catch (Exception) {
return '';
}
}
/**
* jwt 解密
* @return array
*/
public function decrypt(): array
{
try {
return (array)JWT::decode($this->data, new Key($this->key, 'HS256'));
} catch (Exception) {
return [];
}
}
}