49 lines
1.4 KiB
PHP
49 lines
1.4 KiB
PHP
<?php
|
|
|
|
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
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->model = new User();
|
|
}
|
|
|
|
#[NoReturn] public function actionAuth(): void
|
|
{
|
|
$request = new Request();
|
|
$data = $request->post();
|
|
$model = $this->model->where('username', $data['username'])->first();
|
|
$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']));
|
|
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,
|
|
"access_token_expires_at" => $model->access_token_expires_at,
|
|
];
|
|
}
|
|
$model->save();
|
|
}
|
|
|
|
$this->renderApi($res);
|
|
|
|
}
|
|
} |