diff --git a/bootstrap/secure.php b/bootstrap/secure.php index 714beb3..a8baf09 100644 --- a/bootstrap/secure.php +++ b/bootstrap/secure.php @@ -2,7 +2,7 @@ $secure_config = [ 'web_auth_type' => 'email_code', // login_password, email_code - 'token_type' => 'crypt', // random_bytes, md5, crypt, hash, JWT + 'token_type' => 'hash', // 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 7e82699..163964f 100644 --- a/kernel/modules/secure/controllers/SecureRestController.php +++ b/kernel/modules/secure/controllers/SecureRestController.php @@ -36,14 +36,16 @@ class SecureRestController extends RestController $res = []; 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'])); - $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), - }; + if ($model->access_token_expires_at < date("Y-m-d H:i:s") or $model->access_token === null){ + $model->access_token_expires_at = date("Y-m-d H:i:s", strtotime(App::$secure['token_expired_time'])); + $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 de5a4dd..b24096b 100644 --- a/kernel/services/TokenService.php +++ b/kernel/services/TokenService.php @@ -32,7 +32,7 @@ class TokenService */ public static function md5(): string { - return md5(microtime() . self::getSalt() . time()); + return md5(microtime() . self::getSalt(10) . time()); } /** @@ -40,7 +40,7 @@ class TokenService */ public static function crypt(): string { - return crypt(microtime(), self::getSalt()); + return crypt(microtime(), self::getSalt(20)); } /** @@ -48,15 +48,15 @@ class TokenService */ public static function hash(string $alg): string { - return hash($alg, self::getSalt()); + return hash($alg, self::getSalt(10)); } /** * @throws RandomException */ - public static function getSalt(): string + public static function getSalt(int $length): string { - return bin2hex(random_bytes(10)); + return bin2hex(random_bytes($length)); } } \ No newline at end of file