Files
tsd_test/www/first.loc/api/authentication-jwt/validate_token.php
adminps_pv 4df494767b
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 6m48s
c4
2026-03-25 14:11:55 +00:00

70 lines
2.3 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// Заголовки
header("Access-Control-Allow-Origin:*");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// Требуется для декодирования JWT
include_once "Config/core.php";
include_once "libs/php-jwt/BeforeValidException.php";
include_once "libs/php-jwt/ExpiredException.php";
include_once "libs/php-jwt/SignatureInvalidException.php";
include_once "libs/php-jwt/JWT.php";
include_once "libs/php-jwt/Key.php";
use \Firebase\JWT\JWT;
use \Firebase\JWT\Key;
// Получаем значение веб-токена JSON
$data = json_decode(file_get_contents("php://input"));
// Получаем JWT
$jwt = isset($data->jwt) ? $data->jwt : "";
// Если JWT не пуст
if ($jwt) {
// Если декодирование выполнено успешно, показать данные пользователя
try {
// Декодирование jwt
$decoded = JWT::decode($jwt, new Key($key, 'HS256'));
// Код ответа
http_response_code(200);
// Покажем детали
echo json_encode(array(
"result"=>1,
"message" => "Доступ разрешен",
"data" => $decoded->data
));
}
// Если декодирование не удалось, это означает, что JWT является недействительным
catch (Exception $e) {
// Код ответа
http_response_code(401);
// Сообщим пользователю что ему отказано в доступе и покажем сообщение об ошибке
echo json_encode(array(
"result"=>0,
"message" => "Close ".$jwt,
"error" => $e->getMessage()
));
}
}
// Покажем сообщение об ошибке, если JWT пуст
else {
// Код ответа
// http_response_code(401);
// Сообщим пользователю что доступ запрещен
echo json_encode(array("message" => "Access denied"));
}