creste secret key

This commit is contained in:
2024-10-22 16:40:40 +03:00
parent 7ccf0957bf
commit c9fe2f160a
10 changed files with 136 additions and 19 deletions

View File

@ -20,6 +20,8 @@ class App
static User $user;
static array $secure;
public ModuleService $moduleService;
public static Database $db;

View File

@ -0,0 +1,25 @@
<?php
namespace kernel\console\controllers;
use kernel\console\ConsoleController;
use kernel\services\TokenService;
use Random\RandomException;
class SecureController extends ConsoleController
{
/**
* @throws RandomException
*/
public function actionCreateSecretKey(): void
{
$envFile = \EnvEditor\EnvFile::loadFrom(ROOT_DIR . "/.env");
$envFile->setValue("SECRET_KEY", TokenService::random_bytes(15));
$envFile->saveTo(ROOT_DIR . "/.env");
$this->out->r("Secret key successfully created.", "green");
}
}

View File

@ -17,6 +17,10 @@ App::$collector->group(["prefix" => "admin-theme"], callback: function (RouteCol
App::$collector->console('uninstall', [\kernel\console\controllers\AdminThemeController::class, 'actionUninstallTheme']);
});
App::$collector->group(["prefix" => "secure"], callback: function (RouteCollector $router){
App::$collector->console('create-secret-key', [\kernel\console\controllers\SecureController::class, 'actionCreateSecretKey']);
});
App::$collector->group(["prefix" => "admin"], callback: function (RouteCollector $router){
App::$collector->console('init', [\kernel\console\controllers\AdminConsoleController::class, 'actionInit']);
});

View File

@ -5,10 +5,12 @@ namespace kernel\modules\secure\controllers;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use JetBrains\PhpStorm\NoReturn;
use kernel\App;
use kernel\helpers\Debug;
use kernel\modules\user\models\User;
use kernel\Request;
use kernel\RestController;
use kernel\services\TokenService;
class SecureRestController extends RestController
{
@ -25,21 +27,13 @@ class SecureRestController extends RestController
$res = [];
if ($model) {
if (password_verify($data["password"], $model->password_hash)) {
$baseUrl = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://';
$baseUrl .= $_SERVER['HTTP_HOST'];
// $baseUrl .= $_SERVER['REQUEST_URI'];;
$jwt = JWT::encode(
payload: [
"iat" => time(),
"exp" => date("Y-m-d H:i:s", strtotime("+30 days"))
],
key: $model->password_hash,
alg: 'HS256'
);
$model->access_token = $jwt;
$model->access_token_expires_at =
JWT::decode($jwt, new Key($model->password_hash, 'HS256'))->exp;
$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);
}
$res = [
"access_token" => $model->access_token,

View File

@ -0,0 +1,29 @@
<?php
namespace kernel\services;
use Firebase\JWT\JWT;
use Random\RandomException;
class TokenService
{
public static function JWT(string|\OpenSSLCertificate|\OpenSSLAsymmetricKey $key, string $alg, array $payload = []): string
{
return JWT::encode(
payload: $payload,
key: $key,
alg: $alg
);
}
/**
* @throws RandomException
*/
public static function random_bytes(int $ln): string
{
$token = random_bytes($ln);
return bin2hex($token);
}
}