token generate

This commit is contained in:
Билай Станислав 2024-10-23 11:38:53 +03:00
parent e3d1722f2c
commit ffa659ea1d
3 changed files with 47 additions and 9 deletions

View File

@ -1,7 +1,7 @@
<?php <?php
$secure_config = [ $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 'token_expired_time' => "+30 days", // +1 day
]; ];

View File

@ -11,6 +11,7 @@ use kernel\modules\user\models\User;
use kernel\Request; use kernel\Request;
use kernel\RestController; use kernel\RestController;
use kernel\services\TokenService; use kernel\services\TokenService;
use Random\RandomException;
class SecureRestController extends RestController class SecureRestController extends RestController
{ {
@ -19,6 +20,9 @@ class SecureRestController extends RestController
$this->model = new User(); $this->model = new User();
} }
/**
* @throws RandomException
*/
#[NoReturn] public function actionAuth(): void #[NoReturn] public function actionAuth(): void
{ {
$request = new Request(); $request = new Request();
@ -28,12 +32,13 @@ class SecureRestController extends RestController
if ($model) { if ($model) {
if (password_verify($data["password"], $model->password_hash)) { 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'])); $model->access_token_expires_at = date("Y-m-d H:i:s", strtotime(App::$secure['token_expired_time']));
switch (App::$secure['token_type']){ $model->access_token = match (App::$secure['token_type']) {
case "JWT": "JWT" => TokenService::JWT($_ENV['SECRET_KEY'], 'HS256'),
$model->access_token = TokenService::JWT($_ENV['SECRET_KEY'], 'HS256'); "md5" => TokenService::md5(),
default: "crypt" => TokenService::crypt(),
$model->access_token = TokenService::random_bytes(20); "hash" => TokenService::hash('sha256'),
} default => TokenService::random_bytes(20),
};
$res = [ $res = [
"access_token" => $model->access_token, "access_token" => $model->access_token,

View File

@ -3,6 +3,7 @@
namespace kernel\services; namespace kernel\services;
use Firebase\JWT\JWT; use Firebase\JWT\JWT;
use kernel\helpers\Debug;
use Random\RandomException; use Random\RandomException;
class TokenService class TokenService
@ -22,8 +23,40 @@ class TokenService
*/ */
public static function random_bytes(int $ln): string public static function random_bytes(int $ln): string
{ {
$token = random_bytes($ln); return bin2hex(random_bytes($ln));
return bin2hex($token); }
/**
* @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));
} }
} }