token generate
This commit is contained in:
parent
e3d1722f2c
commit
ffa659ea1d
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
$secure_config = [
|
||||
'token_type' => 'random_bytes', // random_bytes, md5, crypt, hash
|
||||
'token_type' => 'JWT', // random_bytes, md5, crypt, hash, JWT
|
||||
'token_expired_time' => "+30 days", // +1 day
|
||||
];
|
||||
|
||||
|
@ -11,6 +11,7 @@ use kernel\modules\user\models\User;
|
||||
use kernel\Request;
|
||||
use kernel\RestController;
|
||||
use kernel\services\TokenService;
|
||||
use Random\RandomException;
|
||||
|
||||
class SecureRestController extends RestController
|
||||
{
|
||||
@ -19,6 +20,9 @@ class SecureRestController extends RestController
|
||||
$this->model = new User();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RandomException
|
||||
*/
|
||||
#[NoReturn] public function actionAuth(): void
|
||||
{
|
||||
$request = new Request();
|
||||
@ -28,12 +32,13 @@ class SecureRestController extends RestController
|
||||
if ($model) {
|
||||
if (password_verify($data["password"], $model->password_hash)) {
|
||||
$model->access_token_expires_at = date("Y-m-d H:i:s", strtotime(App::$secure['token_expired_time']));
|
||||
switch (App::$secure['token_type']){
|
||||
case "JWT":
|
||||
$model->access_token = TokenService::JWT($_ENV['SECRET_KEY'], 'HS256');
|
||||
default:
|
||||
$model->access_token = TokenService::random_bytes(20);
|
||||
}
|
||||
$model->access_token = match (App::$secure['token_type']) {
|
||||
"JWT" => TokenService::JWT($_ENV['SECRET_KEY'], 'HS256'),
|
||||
"md5" => TokenService::md5(),
|
||||
"crypt" => TokenService::crypt(),
|
||||
"hash" => TokenService::hash('sha256'),
|
||||
default => TokenService::random_bytes(20),
|
||||
};
|
||||
|
||||
$res = [
|
||||
"access_token" => $model->access_token,
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace kernel\services;
|
||||
|
||||
use Firebase\JWT\JWT;
|
||||
use kernel\helpers\Debug;
|
||||
use Random\RandomException;
|
||||
|
||||
class TokenService
|
||||
@ -22,8 +23,40 @@ class TokenService
|
||||
*/
|
||||
public static function random_bytes(int $ln): string
|
||||
{
|
||||
$token = random_bytes($ln);
|
||||
return bin2hex($token);
|
||||
return bin2hex(random_bytes($ln));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws RandomException
|
||||
*/
|
||||
public static function md5(): string
|
||||
{
|
||||
return md5(microtime() . self::getSalt() . time());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RandomException
|
||||
*/
|
||||
public static function crypt(): string
|
||||
{
|
||||
return crypt(microtime(), self::getSalt());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RandomException
|
||||
*/
|
||||
public static function hash(string $alg): string
|
||||
{
|
||||
return hash($alg, self::getSalt());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RandomException
|
||||
*/
|
||||
public static function getSalt(): string
|
||||
{
|
||||
return bin2hex(random_bytes(10));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user