54 lines
1.6 KiB
PHP
54 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace kernel\modules\secure\services;
|
|
|
|
use kernel\App;
|
|
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;
|
|
use kernel\services\TokenService;
|
|
|
|
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);
|
|
$secretCode->code_expires_at = date("Y-m-d H:i:s", strtotime("+5 minutes"));;
|
|
$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();
|
|
}
|
|
|
|
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),
|
|
};
|
|
}
|
|
|
|
} |