MicroFrameWork/kernel/modules/secure/services/SecureService.php

54 lines
1.6 KiB
PHP
Raw Normal View History

2024-12-09 13:46:10 +03:00
<?php
namespace kernel\modules\secure\services;
2024-12-09 16:14:43 +03:00
use kernel\App;
2024-12-09 13:46:10 +03:00
use kernel\FormModel;
use kernel\helpers\Debug;
use kernel\modules\secure\models\SecretCode;
use kernel\modules\user\models\User;
use kernel\modules\user\service\UserService;
2024-12-09 16:14:43 +03:00
use kernel\services\TokenService;
2024-12-09 13:46:10 +03:00
class SecureService
{
public static function createSecretCode(User $user): void
{
$secretCode = new SecretCode();
$secretCode->user_id = $user->id;
$secretCode->code = mt_rand(100000, 999999);
$secretCode->code_expires_at = date("Y-m-d H:i:s", strtotime("+5 minutes"));;
$secretCode->save();
}
public static function updateSecretCode(User $user): void
{
$secretCode = SecretCode::where('user_id', $user->id)->first();
$secretCode->code = mt_rand(100000, 999999);
2024-12-09 14:45:44 +03:00
$secretCode->code_expires_at = date("Y-m-d H:i:s", strtotime("+5 minutes"));;
2024-12-09 13:46:10 +03:00
$secretCode->save();
}
public static function getCodeByUserId(int $user_id)
{
return SecretCode::where('user_id', $user_id)->one()->code;
}
public static function getByField(string $field, mixed $value)
{
return SecretCode::where($field, $value)->first();
}
2024-12-09 16:14:43 +03:00
public static function generateAccessToken(): string
{
return 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),
};
}
2024-12-09 13:46:10 +03:00
}