From ffa659ea1dffa60ca6b7efc0812845480b08c173 Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Wed, 23 Oct 2024 11:38:53 +0300 Subject: [PATCH] token generate --- bootstrap/secure.php | 2 +- .../controllers/SecureRestController.php | 17 ++++++--- kernel/services/TokenService.php | 37 ++++++++++++++++++- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/bootstrap/secure.php b/bootstrap/secure.php index f2b0b37..a800dda 100644 --- a/bootstrap/secure.php +++ b/bootstrap/secure.php @@ -1,7 +1,7 @@ 'random_bytes', // random_bytes, md5, crypt, hash + 'token_type' => 'JWT', // random_bytes, md5, crypt, hash, JWT 'token_expired_time' => "+30 days", // +1 day ]; diff --git a/kernel/modules/secure/controllers/SecureRestController.php b/kernel/modules/secure/controllers/SecureRestController.php index 3900296..8075d69 100644 --- a/kernel/modules/secure/controllers/SecureRestController.php +++ b/kernel/modules/secure/controllers/SecureRestController.php @@ -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, diff --git a/kernel/services/TokenService.php b/kernel/services/TokenService.php index 5cb1c59..de5a4dd 100644 --- a/kernel/services/TokenService.php +++ b/kernel/services/TokenService.php @@ -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)); } } \ No newline at end of file