143 lines
4.7 KiB
PHP
Executable File
143 lines
4.7 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Filters;
|
|
|
|
use App\Helpers\JWTToken;
|
|
use CodeIgniter\Filters\FilterInterface;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Firebase\JWT\JWT;
|
|
use Firebase\JWT\Key;
|
|
use ReflectionClass;
|
|
|
|
require_once('../vendor/autoload.php');
|
|
|
|
use App\Models\EmployeeModel;
|
|
use App\Models\LevelContactModel;
|
|
|
|
|
|
class AuthJWT implements FilterInterface
|
|
{
|
|
// public function before(RequestInterface $request, $arguments = null)
|
|
// {
|
|
// $jwt = $request->getHeader('Authorization');
|
|
|
|
// if ($jwt) {
|
|
// if (JWTToken::validateJWT($jwt)) {
|
|
// $data = JWTToken::validateJWT($jwt);
|
|
// $data = json_decode($data);
|
|
|
|
// $id = $data->decoded->id;
|
|
// if(isset($data->decoded->emp_code)){
|
|
// $model = new EmployeeModel();
|
|
// $user_data = $model->where('id', $id)->first();
|
|
|
|
// if($user_data['token_time_out'] > time()){
|
|
// $data =["token_time_out" => time() + getenv('TOKENTIMEOUT') ];
|
|
// $model->update($id, $data);
|
|
// return true;
|
|
// }else{
|
|
|
|
// // if($user_data['token_time_out'] != "" && $user_data['token_time_out'] != NULL)
|
|
|
|
// $data =["token_time_out" => ''];
|
|
// $model->update($id, $data);
|
|
// header('Content-Type: application/json');
|
|
// http_response_code(401);
|
|
// // $error = json_encode(["status" => 401, "message" => $data->message]);
|
|
// $error = json_encode(["status" => 401, "message" => "Token is Invalid"]);
|
|
// echo $error;
|
|
// exit;
|
|
// }
|
|
// }else{
|
|
// $model = new LevelContactModel();
|
|
// $hr_data = $model->where('id', $id)->first();
|
|
|
|
// if($hr_data['token_time_out'] > time()){
|
|
// $data =["token_time_out" => time() + getenv('TOKENTIMEOUT') ];
|
|
// $model->update($id, $data);
|
|
// return true;
|
|
// }else{
|
|
// $data =["token_time_out" => ''];
|
|
// $model->update($id, $data);
|
|
// header('Content-Type: application/json');
|
|
// http_response_code(401);
|
|
// // $error = json_encode(["status" => 401, "message" => $data->message]);
|
|
// $error = json_encode(["status" => 401, "message" => "Token is Invalid"]);
|
|
// echo $error;
|
|
// exit;
|
|
// }
|
|
// }
|
|
|
|
// }
|
|
// } else {
|
|
// header('Content-Type: application/json');
|
|
// http_response_code(403);
|
|
// $error = json_encode(["status" => 403, "message" => "Access Forbidden!"]);
|
|
// echo $error;
|
|
// exit();
|
|
// }
|
|
// }
|
|
|
|
public function before(RequestInterface $request, $arguments = null)
|
|
{
|
|
$authHeader = $request->getHeaderLine('Authorization');
|
|
|
|
if (!$authHeader) {
|
|
return $this->reject(403, 'Access Forbidden');
|
|
}
|
|
|
|
$result = JWTToken::validateJWT($authHeader);
|
|
|
|
if ($result['status'] !== true) {
|
|
return $this->reject(401, $result['message']);
|
|
}
|
|
|
|
$decoded = $result['decoded'];
|
|
$id = $decoded['id'] ?? null;
|
|
|
|
if (!$id) {
|
|
return $this->reject(401, 'Invalid token payload');
|
|
}
|
|
|
|
if (isset($decoded['emp_code'])) {
|
|
$model = new EmployeeModel();
|
|
} else {
|
|
$model = new LevelContactModel();
|
|
$id = $decoded['post_hr_id'] ?? null;
|
|
}
|
|
|
|
$user = $model->find($id);
|
|
|
|
if (!$user || $user['token_time_out'] <= time()) {
|
|
$model->update($id, ['token_time_out' => null]);
|
|
return $this->reject(401, 'Token expired');
|
|
}
|
|
|
|
// Refresh sliding expiration
|
|
$model->update($id, [
|
|
'token_time_out' => time() + getenv('TOKENTIMEOUT')
|
|
]);
|
|
|
|
return true;
|
|
}
|
|
|
|
private function reject(int $code, string $message)
|
|
{
|
|
return service('response')
|
|
->setStatusCode($code)
|
|
->setJSON(['status' => $code, 'message' => $message])
|
|
->send();
|
|
}
|
|
|
|
|
|
|
|
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
|
|
{
|
|
// Do something here after the response is sent
|
|
}
|
|
|
|
|
|
|
|
}
|