Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 95ae1fe999 | |||
| 9668830ee2 | |||
| f26539c41f | |||
| 3ead688d5f | |||
| 157c233ed6 | |||
| 40b4c2a4c9 |
+3
-3
@@ -445,8 +445,6 @@ if (!function_exists('fire')) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param object $event
|
* @param object $event
|
||||||
* @throws NotFindClassException
|
|
||||||
* @throws ReflectionException
|
|
||||||
*/
|
*/
|
||||||
function fire(object $event)
|
function fire(object $event)
|
||||||
{
|
{
|
||||||
@@ -541,7 +539,7 @@ if (!function_exists('trim_blank')) {
|
|||||||
|
|
||||||
if (!function_exists('get_file_extension')) {
|
if (!function_exists('get_file_extension')) {
|
||||||
|
|
||||||
function get_file_extension($filename)
|
function get_file_extension($filename): bool|int|string
|
||||||
{
|
{
|
||||||
$mime_types = [
|
$mime_types = [
|
||||||
'txt' => 'text/plain',
|
'txt' => 'text/plain',
|
||||||
@@ -882,6 +880,7 @@ if (!function_exists('di')) {
|
|||||||
/**
|
/**
|
||||||
* @param string $className
|
* @param string $className
|
||||||
* @return mixed
|
* @return mixed
|
||||||
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
function di(string $className): mixed
|
function di(string $className): mixed
|
||||||
{
|
{
|
||||||
@@ -896,6 +895,7 @@ if (!function_exists('duplicate')) {
|
|||||||
/**
|
/**
|
||||||
* @param string $className
|
* @param string $className
|
||||||
* @return mixed
|
* @return mixed
|
||||||
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
function duplicate(string $className): mixed
|
function duplicate(string $className): mixed
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ use Kiri\Error\Logger;
|
|||||||
use Kiri\Events\EventProvider;
|
use Kiri\Events\EventProvider;
|
||||||
use Kiri\Exception\InitException;
|
use Kiri\Exception\InitException;
|
||||||
use Kiri\Exception\NotFindClassException;
|
use Kiri\Exception\NotFindClassException;
|
||||||
use Kiri\Jwt\Jwt;
|
|
||||||
use Kiri\Kiri;
|
use Kiri\Kiri;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Server\ServerManager;
|
use Server\ServerManager;
|
||||||
@@ -372,16 +371,6 @@ abstract class BaseApplication extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Jwt
|
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
public function getJwt(): Jwt
|
|
||||||
{
|
|
||||||
return $this->get('jwt');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Server
|
* @return Server
|
||||||
* @throws
|
* @throws
|
||||||
|
|||||||
+19
-13
@@ -30,8 +30,11 @@ class Str
|
|||||||
if ($length < 1) $length = 20;
|
if ($length < 1) $length = 20;
|
||||||
$default = self::STRING . strtoupper(self::STRING) . self::NUMBER;
|
$default = self::STRING . strtoupper(self::STRING) . self::NUMBER;
|
||||||
$default = str_split($default);
|
$default = str_split($default);
|
||||||
$string .= str_repeat($default[array_rand($default)], $length);
|
for ($i = 0; $i < $length; $i++) {
|
||||||
return (string)$string;
|
shuffle($default);
|
||||||
|
$string .= $default[array_rand($default)];
|
||||||
|
}
|
||||||
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -45,7 +48,10 @@ class Str
|
|||||||
$number = '';
|
$number = '';
|
||||||
$default = str_split(self::NUMBER);
|
$default = str_split(self::NUMBER);
|
||||||
if ($length < 1) $length = 1;
|
if ($length < 1) $length = 1;
|
||||||
$number .= str_repeat($default[array_rand($default)], $length);
|
for ($i = 0; $i < $length; $i++) {
|
||||||
|
shuffle($default);
|
||||||
|
$number .= $default[array_rand($default)];
|
||||||
|
}
|
||||||
return $number;
|
return $number;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,7 +63,7 @@ class Str
|
|||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function cut_str_utf8($string, $sullen, bool $strip_tags = true, string $append = '...'): string
|
public static function cut_str_utf8($string, $sullen, bool $strip_tags = TRUE, string $append = '...'): string
|
||||||
{
|
{
|
||||||
if ($strip_tags) {
|
if ($strip_tags) {
|
||||||
$string = strip_tags($string);
|
$string = strip_tags($string);
|
||||||
@@ -83,10 +89,10 @@ class Str
|
|||||||
* @return bool
|
* @return bool
|
||||||
* 判断是否为json字符串
|
* 判断是否为json字符串
|
||||||
*/
|
*/
|
||||||
public static function isJson($data, $callback = null): bool
|
public static function isJson($data, $callback = NULL): bool
|
||||||
{
|
{
|
||||||
$json = !is_null(json_decode($data)) && !is_numeric($data);
|
$json = !is_null(json_decode($data)) && !is_numeric($data);
|
||||||
if ($json && is_callable($callback, true)) {
|
if ($json && is_callable($callback, TRUE)) {
|
||||||
return call_user_func($callback, $data);
|
return call_user_func($callback, $data);
|
||||||
}
|
}
|
||||||
return $json;
|
return $json;
|
||||||
@@ -99,10 +105,10 @@ class Str
|
|||||||
* @return bool
|
* @return bool
|
||||||
* 判断是否序列化字符串
|
* 判断是否序列化字符串
|
||||||
*/
|
*/
|
||||||
public static function isSerialize($data, $callBack = null): bool
|
public static function isSerialize($data, $callBack = NULL): bool
|
||||||
{
|
{
|
||||||
$false = !empty($data) && swoole_unserialize($data) !== false;
|
$false = !empty($data) && swoole_unserialize($data) !== FALSE;
|
||||||
if ($false && is_callable($callBack, true)) {
|
if ($false && is_callable($callBack, TRUE)) {
|
||||||
return call_user_func($callBack, $data);
|
return call_user_func($callBack, $data);
|
||||||
}
|
}
|
||||||
return $false;
|
return $false;
|
||||||
@@ -184,7 +190,7 @@ class Str
|
|||||||
* @return array
|
* @return array
|
||||||
* 剩余天,带分秒
|
* 剩余天,带分秒
|
||||||
*/
|
*/
|
||||||
public static function timeout($endTime, int $startTime = null): array
|
public static function timeout($endTime, int $startTime = NULL): array
|
||||||
{
|
{
|
||||||
$endTime = $endTime - (!empty($startTime) ? $startTime : time());
|
$endTime = $endTime - (!empty($startTime) ? $startTime : time());
|
||||||
|
|
||||||
@@ -227,7 +233,7 @@ class Str
|
|||||||
public static function clear(string $string): array|string|null
|
public static function clear(string $string): array|string|null
|
||||||
{
|
{
|
||||||
$char = '。、!?:;﹑•"…‘’“”〝〞∕¦‖— 〈〉﹞﹝「」‹›〖〗】【»«』『〕〔》《﹐¸﹕︰﹔!¡?¿﹖﹌﹏﹋'´ˊˋ―﹫︳︴¯_ ̄﹢﹦﹤‐˜﹟﹩﹠﹪﹡﹨﹍﹉﹎﹊ˇ︵︶︷︸︹︿﹀︺︽︾ˉ﹁﹂﹃﹄︻︼()';
|
$char = '。、!?:;﹑•"…‘’“”〝〞∕¦‖— 〈〉﹞﹝「」‹›〖〗】【»«』『〕〔》《﹐¸﹕︰﹔!¡?¿﹖﹌﹏﹋'´ˊˋ―﹫︳︴¯_ ̄﹢﹦﹤‐˜﹟﹩﹠﹪﹡﹨﹍﹉﹎﹊ˇ︵︶︷︸︹︿﹀︺︽︾ˉ﹁﹂﹃﹄︻︼()';
|
||||||
return preg_replace(array("/[[:punct:]]/i", '/[' . $char . ']/u', '/[ ]{2,}/'), '', $string);
|
return preg_replace(["/[[:punct:]]/i", '/[' . $char . ']/u', '/[ ]{2,}/'], '', $string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -243,7 +249,7 @@ class Str
|
|||||||
{
|
{
|
||||||
$str = '';
|
$str = '';
|
||||||
if (!$requestTime) {
|
if (!$requestTime) {
|
||||||
$requestTime = microtime(true);
|
$requestTime = microtime(TRUE);
|
||||||
}
|
}
|
||||||
$_user = str_split(md5($user . md5((string)$user)));
|
$_user = str_split(md5($user . md5((string)$user)));
|
||||||
ksort($_user);
|
ksort($_user);
|
||||||
@@ -268,7 +274,7 @@ class Str
|
|||||||
* @param bool $unfairest
|
* @param bool $unfairest
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function convertUnderline(string $str, bool $unfairest = true): string
|
public static function convertUnderline(string $str, bool $unfairest = TRUE): string
|
||||||
{
|
{
|
||||||
$str = ucwords(str_replace('_', ' ', $str));
|
$str = ucwords(str_replace('_', ' ', $str));
|
||||||
$str = str_replace(' ', '', lcfirst($str));
|
$str = str_replace(' ', '', lcfirst($str));
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ use ReflectionException;
|
|||||||
use ReflectionFunction;
|
use ReflectionFunction;
|
||||||
use ReflectionMethod;
|
use ReflectionMethod;
|
||||||
use ReflectionProperty;
|
use ReflectionProperty;
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Container
|
* Class Container
|
||||||
@@ -61,15 +62,27 @@ class Container extends BaseObject implements ContainerInterface
|
|||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $id
|
||||||
|
* @return mixed
|
||||||
|
* @throws ReflectionException
|
||||||
|
*/
|
||||||
|
public function get(string $id): mixed
|
||||||
|
{
|
||||||
|
return $this->make($id, [], []);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $class
|
* @param $class
|
||||||
* @param array $constrict
|
* @param array $constrict
|
||||||
* @param array $config
|
* @param array $config
|
||||||
*
|
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws
|
* @throws ReflectionException
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function get($class, array $constrict = [], array $config = []): mixed
|
public function make($class, array $constrict = [], array $config = []): mixed
|
||||||
{
|
{
|
||||||
if ($this->isInterface($class)) {
|
if ($this->isInterface($class)) {
|
||||||
$class = $this->_interfaces[$class];
|
$class = $this->_interfaces[$class];
|
||||||
@@ -81,6 +94,7 @@ class Container extends BaseObject implements ContainerInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $interface
|
* @param string $interface
|
||||||
* @param string $class
|
* @param string $class
|
||||||
@@ -435,4 +449,13 @@ class Container extends BaseObject implements ContainerInterface
|
|||||||
}
|
}
|
||||||
return $old;
|
return $old;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $id
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function has(string $id): bool
|
||||||
|
{
|
||||||
|
return isset($this->_singletons[$id]) || isset($this->_interfaces[$id]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Kiri\Di;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @mixin Container
|
|
||||||
*/
|
|
||||||
interface ContainerInterface
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -72,9 +72,9 @@ class HotReload extends Command
|
|||||||
$this->dirs = Config::get('inotify', [APP_PATH . 'app']);
|
$this->dirs = Config::get('inotify', [APP_PATH . 'app']);
|
||||||
swoole_async_set(['enable_coroutine' => false]);
|
swoole_async_set(['enable_coroutine' => false]);
|
||||||
if (!extension_loaded('inotify')) {
|
if (!extension_loaded('inotify')) {
|
||||||
$this->driver = Kiri::getDi()->get(Scaner::class, [$this->dirs, $this]);
|
$this->driver = Kiri::getDi()->make(Scaner::class, [$this->dirs, $this]);
|
||||||
} else {
|
} else {
|
||||||
$this->driver = Kiri::getDi()->get(Inotify::class, [$this->dirs, $this]);
|
$this->driver = Kiri::getDi()->make(Inotify::class, [$this->dirs, $this]);
|
||||||
}
|
}
|
||||||
if (Kiri::getPlatform()->isLinux()) {
|
if (Kiri::getPlatform()->isLinux()) {
|
||||||
swoole_set_process_name('[' . Config::get('id', 'sw service.') . '].sw:wather');
|
swoole_set_process_name('[' . Config::get('id', 'sw service.') . '].sw:wather');
|
||||||
|
|||||||
@@ -1,57 +0,0 @@
|
|||||||
<?php
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
|
|
||||||
namespace Kiri\Jwt;
|
|
||||||
|
|
||||||
|
|
||||||
use Annotation\Inject;
|
|
||||||
use Exception;
|
|
||||||
use Http\Message\ServerRequest;
|
|
||||||
use Kiri\Kiri;
|
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
|
||||||
use Psr\Http\Server\MiddlewareInterface;
|
|
||||||
use Psr\Http\Server\RequestHandlerInterface;
|
|
||||||
use Server\Constrict\ResponseInterface;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class CoreMiddleware
|
|
||||||
* @package Kiri\Kiri\Route
|
|
||||||
* 跨域中间件
|
|
||||||
*/
|
|
||||||
class JWTAuthMiddleware implements MiddlewareInterface
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
/** @var int */
|
|
||||||
public int $zOrder = 0;
|
|
||||||
|
|
||||||
|
|
||||||
#[Inject(ResponseInterface::class)]
|
|
||||||
public ResponseInterface $response;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param ServerRequest $request
|
|
||||||
* @param RequestHandlerInterface $handler
|
|
||||||
* @return \Psr\Http\Message\ResponseInterface
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): \Psr\Http\Message\ResponseInterface
|
|
||||||
{
|
|
||||||
$authorization = $request->getHeaderLine('Authorization');
|
|
||||||
if (empty($authorization)) {
|
|
||||||
return $this->response->json(['code' => 401, 'JWT voucher cannot be empty.']);
|
|
||||||
}
|
|
||||||
if (!str_starts_with($authorization, 'Bearer ')) {
|
|
||||||
return $this->response->json(['code' => 401, 'JWT Voucher Format Error.']);
|
|
||||||
}
|
|
||||||
$authorization = str_replace('Bearer ', '', $authorization);
|
|
||||||
$jwt = Kiri::app()->getJwt();
|
|
||||||
if (!$jwt->validator($authorization)) {
|
|
||||||
return $this->response->json(['code' => 401, 'JWT Validator fail.']);
|
|
||||||
}
|
|
||||||
return $handler->handle($request);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Kiri\Jwt;
|
|
||||||
|
|
||||||
|
|
||||||
use JetBrains\PhpStorm\Pure;
|
|
||||||
use Throwable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
class JWTAuthTokenException extends \Exception
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $message
|
|
||||||
* @param int $code
|
|
||||||
* @param Throwable|null $previous
|
|
||||||
*/
|
|
||||||
#[Pure] public function __construct($message = "", $code = 4001, Throwable $previous = null)
|
|
||||||
{
|
|
||||||
parent::__construct($message, 4001, $previous);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,201 +0,0 @@
|
|||||||
<?php
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Kiri\Jwt;
|
|
||||||
|
|
||||||
use Annotation\Inject;
|
|
||||||
use Exception;
|
|
||||||
use Http\Constrict\RequestInterface;
|
|
||||||
use Kiri\Abstracts\Component;
|
|
||||||
use Kiri\Abstracts\Config;
|
|
||||||
use Kiri\Core\Json;
|
|
||||||
use Kiri\Exception\ConfigException;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Jwt
|
|
||||||
* @package Kiri\Jwt
|
|
||||||
*/
|
|
||||||
class Jwt extends Component
|
|
||||||
{
|
|
||||||
|
|
||||||
use JwtHelper;
|
|
||||||
|
|
||||||
|
|
||||||
#[Inject(RequestInterface::class)]
|
|
||||||
private RequestInterface $request;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param RequestInterface $request
|
|
||||||
*/
|
|
||||||
public function setRequest(RequestInterface $request): void
|
|
||||||
{
|
|
||||||
$this->request = $request;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws ConfigException
|
|
||||||
* @throws Exception
|
|
||||||
* 'jwt' => [
|
|
||||||
* 'scene' => 'application',
|
|
||||||
* 'timeout' => 7200,
|
|
||||||
* 'encrypt' => '',
|
|
||||||
* 'iv' => '',
|
|
||||||
* 'key' => '',
|
|
||||||
* ]
|
|
||||||
*/
|
|
||||||
public function init()
|
|
||||||
{
|
|
||||||
$this->request = di(RequestInterface::class);
|
|
||||||
|
|
||||||
$this->public = Config::get('ssl.public', $this->public);
|
|
||||||
$this->private = Config::get('ssl.private', $this->private);
|
|
||||||
$this->timeout = Config::get('jwt.timeout', 7200);
|
|
||||||
|
|
||||||
$jwt = Config::get('jwt', []);
|
|
||||||
if ($jwt) {
|
|
||||||
$this->setScene($jwt['scene'] ?? 'application');
|
|
||||||
$this->setKey($jwt['key'] ?? get_called_class());
|
|
||||||
$length = openssl_cipher_iv_length($this->encrypt);
|
|
||||||
if ($length > 0) {
|
|
||||||
$defaultIv = openssl_random_pseudo_bytes($length);
|
|
||||||
$this->setIv($jwt['iv'] ?? $defaultIv);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $unionId
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function create(int $unionId): string
|
|
||||||
{
|
|
||||||
$this->user = $unionId;
|
|
||||||
$this->config['time'] = time();
|
|
||||||
$this->data = $this->request->getHeaders();
|
|
||||||
if (!isset($this->data['source'])) {
|
|
||||||
$this->data['source'] = 'browser';
|
|
||||||
}
|
|
||||||
return $this->createEncrypt($unionId);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
private function jwtHeader(): string
|
|
||||||
{
|
|
||||||
$string = openssl_encrypt(
|
|
||||||
json_encode(['type' => 'openssl', 'encrypt' => $this->encrypt]),
|
|
||||||
$this->encrypt,
|
|
||||||
$this->key,
|
|
||||||
0,
|
|
||||||
$this->iv
|
|
||||||
);
|
|
||||||
return str_replace('=', '', $string);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $unionId
|
|
||||||
* @return string
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private function jwtBody($unionId): string
|
|
||||||
{
|
|
||||||
$json = json_encode(['unionId' => $unionId, 'createTime' => time(), 'expire_at' => time() + $this->timeout]);
|
|
||||||
openssl_private_encrypt($json, $encode, $this->private);
|
|
||||||
return base64_encode($encode);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $unionId
|
|
||||||
* @return string
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private function createEncrypt($unionId): string
|
|
||||||
{
|
|
||||||
$params[] = $this->jwtHeader();
|
|
||||||
$params[] = $this->jwtBody($unionId);
|
|
||||||
|
|
||||||
$params[] = hash('sha256', $params[0] . $params[1]);
|
|
||||||
return implode('.', $params);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $token
|
|
||||||
* @return string|int
|
|
||||||
* @throws JWTAuthTokenException
|
|
||||||
*/
|
|
||||||
public function getUnionId($token): string|int
|
|
||||||
{
|
|
||||||
$unpack = $this->unpack($token);
|
|
||||||
if (!$this->_validator($unpack)) {
|
|
||||||
throw new JWTAuthTokenException('JWT certificate has expired.');
|
|
||||||
}
|
|
||||||
return $unpack['unionId'];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $token
|
|
||||||
* @return bool
|
|
||||||
* @throws JWTAuthTokenException
|
|
||||||
*/
|
|
||||||
public function validator($token): bool
|
|
||||||
{
|
|
||||||
return $this->_validator($this->unpack($token));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $unpack
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
private function _validator($unpack): bool
|
|
||||||
{
|
|
||||||
if ($unpack['expire_at'] < time()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $token
|
|
||||||
* @return string
|
|
||||||
* @throws JWTAuthTokenException
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function refresh($token): string
|
|
||||||
{
|
|
||||||
return $this->create($this->unpack($token)['unionId']);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $token
|
|
||||||
* @return mixed
|
|
||||||
* @throws JWTAuthTokenException
|
|
||||||
*/
|
|
||||||
private function unpack(string $token): array
|
|
||||||
{
|
|
||||||
if (count($explode = explode('.', $token)) != 3) {
|
|
||||||
throw new JWTAuthTokenException('JWT Voucher Format Error.');
|
|
||||||
}
|
|
||||||
if (hash('sha256', $explode[0] . $explode[1]) != $explode[2]) {
|
|
||||||
throw new JWTAuthTokenException('JWT Sign Validator Fail.');
|
|
||||||
}
|
|
||||||
if (!openssl_public_decrypt(base64_decode($explode[1]), $decode, $this->public)) {
|
|
||||||
throw new JWTAuthTokenException('JWT Voucher Unpack Error.');
|
|
||||||
}
|
|
||||||
return Json::decode($decode, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Kiri\Jwt;
|
|
||||||
|
|
||||||
trait JwtHelper
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
/** @var int $user */
|
|
||||||
private int $user;
|
|
||||||
|
|
||||||
private array $data;
|
|
||||||
|
|
||||||
private array $source = ['browser', 'android', 'iphone', 'pc', 'mingame'];
|
|
||||||
|
|
||||||
private array $config = ['token' => ''];
|
|
||||||
|
|
||||||
private ?int $timeout = 7200;
|
|
||||||
|
|
||||||
private string $key = 'www.xshucai.com';
|
|
||||||
|
|
||||||
|
|
||||||
private string $secret = '';
|
|
||||||
|
|
||||||
|
|
||||||
private string $scene = 'default';
|
|
||||||
|
|
||||||
|
|
||||||
private string $encrypt = 'aes-128-ecb';
|
|
||||||
|
|
||||||
|
|
||||||
private string $iv = '';
|
|
||||||
|
|
||||||
|
|
||||||
private ?string $public = '-----BEGIN PUBLIC KEY-----
|
|
||||||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6BuML3gtLGde7QKNuNST
|
|
||||||
UCB9gdHC7XIpOc7Wx2I64Esj3UxWHTgp3URj0ge8zpy7A3FfBdppR7d1nwoD6Xad
|
|
||||||
jqfjEWpTy4WwGYsOfH0tFl3wAmse0lebF4NFsS9pzrikQT6c9qsVm88pCjvg4i5t
|
|
||||||
WhTMEnpTFDYoDR0KXlLXltQMudBBUHFaVwP0wKJ/cGX7R1Mrv35K4MXwQFOuGZkP
|
|
||||||
hsp2rO9x5LjtSKIXbexy7WhUu6QMjD/XzgsXr9UF+ExYmBGXRVWgNFLMkiaCZ2Uz
|
|
||||||
WlQhpQrA5/wKd76dCzjvqw9M32OiZl2lCKT73cV8GUvt7BNsM1SiPhqfY7nhO6y3
|
|
||||||
cwIDAQAB
|
|
||||||
-----END PUBLIC KEY-----';
|
|
||||||
|
|
||||||
private ?string $private = '-----BEGIN RSA PRIVATE KEY-----
|
|
||||||
MIIEpQIBAAKCAQEA6BuML3gtLGde7QKNuNSTUCB9gdHC7XIpOc7Wx2I64Esj3UxW
|
|
||||||
HTgp3URj0ge8zpy7A3FfBdppR7d1nwoD6XadjqfjEWpTy4WwGYsOfH0tFl3wAmse
|
|
||||||
0lebF4NFsS9pzrikQT6c9qsVm88pCjvg4i5tWhTMEnpTFDYoDR0KXlLXltQMudBB
|
|
||||||
UHFaVwP0wKJ/cGX7R1Mrv35K4MXwQFOuGZkPhsp2rO9x5LjtSKIXbexy7WhUu6QM
|
|
||||||
jD/XzgsXr9UF+ExYmBGXRVWgNFLMkiaCZ2UzWlQhpQrA5/wKd76dCzjvqw9M32Oi
|
|
||||||
Zl2lCKT73cV8GUvt7BNsM1SiPhqfY7nhO6y3cwIDAQABAoIBADPihJHP8XktmmCs
|
|
||||||
43Vfv5Z3zNaKR2LA1Eph3E0xviuJYHkFqXJarbESqqW2qRQeoQeB/lXWnxYzAo4M
|
|
||||||
tRcpNss+6FlqRVUHi3gKR7C4Yq3PTemcfIVUpAy7gYa8LJDTYZRcJMZXNDtiMbBh
|
|
||||||
9kFZU4SBhaTTx2KLQKS9yyWOqzbBvyLXN+1+Wy477M9+MXXTKw79dO+pML6cR0yl
|
|
||||||
pNfVR5FX5L/GB5vOtQB/Aqg/CKT8NC5MzWPnKY+TPCCHZyoZuB9dLDuWOlqsN4QX
|
|
||||||
Y4B8fFca5yRwzHra5aGoqdaT/zGctt+I6V/f/KNQCo36f9LPxeXg1+FHvvtTj5WZ
|
|
||||||
N8CGPzECgYEA9R7lRMXzrHE4rK0DhxQXIFbIKKtxrimqZQdbwOUeYYD2R6CDSItK
|
|
||||||
z88RSYElmd6wiS7fYIaheXNqJ8Yu6SQFBF/yshBwjQVl9NJG94LJlgx1XnVZEju6
|
|
||||||
OZjMUOhHXBymtXnLo16pDRl8odc4MFLRH25/vLtwChUr+Qoyt54GzFUCgYEA8mjL
|
|
||||||
jdh94JAmcdnDXsKgjNOGyNWGDVvWoFmy8lEQsMXY1JJnEd3YfDM2prmv3vaoiXzi
|
|
||||||
YkSETl6ZUtJqh78MnHCBY1vI6EAcKQAF/kvP2TataRCXNcGNQwn2mtq+B+heTta6
|
|
||||||
Di8jjAdmdUAYHbmOQryBudiRYG7JEF038elzvKcCgYEAq81ByFguGBkrLev94vkz
|
|
||||||
1Fi+5bJ0dSuC4Fit+J8eEhz/gOiB26C1iL2LUkeQgS5R8XTG37K9DpDUQJhpXMMA
|
|
||||||
OTa+tgtLt6um8FdJokUq4V5ODSyWh28RcTklSzfifC8gsWVyU0kPl7zbW9uq6EPD
|
|
||||||
ixI5uaBuQMLiFSUOsx+xiBkCgYEAtqXHWeVZUy7KCNavomK7XeCzmfdovgAIw2FS
|
|
||||||
t8nk7YzlR6XYC1pAl7Ru5Ujb/v+TFaUHXkuJ9RLKK+Fna0jEU8thcl/iDTzg+vON
|
|
||||||
kIHG5j+Qga2CgXqI2Y5URXGz5XlsNbMNFUrnWcbpqEbW5O6/BgHLLSDEyQgwbygN
|
|
||||||
0zS3g9kCgYEAhssb7kOljdIul4lY5MXc67Zf1dp6S2bucLOxsG6cRW07b3pBz7QF
|
|
||||||
5aPE7ZwnkzTnA4HuGGauKj+qKGAR7ve55XClAq/XipiVFrjwV/t3LC6j5DoqTJYR
|
|
||||||
mlAZUEjsoaT9vjvjGTxl3uCm0TX5KTgtSJIt2kA1tYVjQef+/iZTHxY=
|
|
||||||
-----END RSA PRIVATE KEY-----';
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $publicKey
|
|
||||||
*/
|
|
||||||
public function setPublic(string $publicKey)
|
|
||||||
{
|
|
||||||
$this->public = $publicKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $timeout
|
|
||||||
*/
|
|
||||||
public function setTimeout(int $timeout)
|
|
||||||
{
|
|
||||||
$this->timeout = $timeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public function setScene(string $scene)
|
|
||||||
{
|
|
||||||
$this->scene = $scene;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $timeout
|
|
||||||
*/
|
|
||||||
public function setKey(string $timeout)
|
|
||||||
{
|
|
||||||
$this->key = $timeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $privateKey
|
|
||||||
*/
|
|
||||||
public function setPrivate(string $privateKey)
|
|
||||||
{
|
|
||||||
$this->private = $privateKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $privateKey
|
|
||||||
*/
|
|
||||||
public function setIv(string $privateKey)
|
|
||||||
{
|
|
||||||
$this->iv = $privateKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $privateKey
|
|
||||||
*/
|
|
||||||
public function setEncrypt(string $privateKey)
|
|
||||||
{
|
|
||||||
$this->encrypt = $privateKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
+10
-1
@@ -13,7 +13,7 @@ use JetBrains\PhpStorm\Pure;
|
|||||||
use Kiri\Abstracts\Config;
|
use Kiri\Abstracts\Config;
|
||||||
use Kiri\Core\Json;
|
use Kiri\Core\Json;
|
||||||
use Kiri\Di\Container;
|
use Kiri\Di\Container;
|
||||||
use Kiri\Di\ContainerInterface;
|
use Psr\Container\ContainerInterface;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Server\ServerManager;
|
use Server\ServerManager;
|
||||||
use Swoole\Coroutine;
|
use Swoole\Coroutine;
|
||||||
@@ -229,6 +229,15 @@ class Kiri
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Container
|
||||||
|
*/
|
||||||
|
public static function di(): Container
|
||||||
|
{
|
||||||
|
return static::$container;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $workerId
|
* @param $workerId
|
||||||
* @return mixed
|
* @return mixed
|
||||||
|
|||||||
Reference in New Issue
Block a user