+25
-25
@@ -3,6 +3,7 @@
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use Jakach\Logging\Storage\Database;
|
||||
use Jakach\Logging\Storage\Repository;
|
||||
|
||||
session_set_cookie_params([
|
||||
'lifetime' => 86400 * 7,
|
||||
@@ -13,11 +14,11 @@ session_set_cookie_params([
|
||||
session_start();
|
||||
|
||||
$authToken = $_GET['auth'] ?? '';
|
||||
$errorRedirect = $_GET['redirect'] ?? '/';
|
||||
|
||||
if (!$authToken) {
|
||||
header('Content-Type: application/json');
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Missing auth token']);
|
||||
$_SESSION['auth_error'] = 'Missing authentication token.';
|
||||
header('Location: ' . $errorRedirect);
|
||||
exit;
|
||||
}
|
||||
|
||||
@@ -32,18 +33,31 @@ $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($httpCode !== 200 || !$response) {
|
||||
header('Content-Type: application/json');
|
||||
http_response_code(502);
|
||||
echo json_encode(['error' => 'Auth server unreachable']);
|
||||
$_SESSION['auth_error'] = 'Authentication server is unreachable. Please try again later.';
|
||||
header('Location: ' . $errorRedirect);
|
||||
exit;
|
||||
}
|
||||
|
||||
$data = json_decode($response, true);
|
||||
|
||||
if (!isset($data['status']) || $data['status'] !== 'success') {
|
||||
header('Content-Type: application/json');
|
||||
http_response_code(401);
|
||||
echo json_encode(['error' => 'Authentication failed', 'msg' => $data['msg'] ?? 'unknown']);
|
||||
$_SESSION['auth_error'] = 'Authentication failed: ' . ($data['msg'] ?? 'Unknown error');
|
||||
header('Location: ' . $errorRedirect);
|
||||
exit;
|
||||
}
|
||||
|
||||
$userToken = $data['user_token'] ?? '';
|
||||
|
||||
$db = new Database();
|
||||
$repo = new Repository($db);
|
||||
|
||||
$allowedTokens = $repo->getAllowedUserTokens();
|
||||
|
||||
if (empty($allowedTokens)) {
|
||||
$repo->setAllowedUserTokens([$userToken]);
|
||||
} elseif (!in_array($userToken, $allowedTokens, true)) {
|
||||
$_SESSION['auth_error'] = 'Your Jakach account is not authorized to access this system. Contact an administrator.';
|
||||
header('Location: ' . $errorRedirect);
|
||||
exit;
|
||||
}
|
||||
|
||||
@@ -52,22 +66,8 @@ $_SESSION['username'] = $data['username'] ?? 'unknown';
|
||||
$_SESSION['id'] = $data['id'] ?? '';
|
||||
$_SESSION['email'] = $data['email'] ?? '';
|
||||
$_SESSION['telegram_id'] = $data['telegram_id'] ?? '';
|
||||
$_SESSION['user_token'] = $data['user_token'] ?? '';
|
||||
|
||||
if (!headers_sent()) {
|
||||
$db = new Database();
|
||||
$repo = new \Jakach\Logging\Storage\Repository($db);
|
||||
$allowedTokens = $repo->getAllowedUserTokens();
|
||||
|
||||
if (!empty($allowedTokens) && !in_array($_SESSION['user_token'], $allowedTokens, true)) {
|
||||
$_SESSION = [];
|
||||
session_destroy();
|
||||
header('Content-Type: application/json');
|
||||
http_response_code(403);
|
||||
echo json_encode(['error' => 'Your account is not authorized to access this system']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
$_SESSION['user_token'] = $userToken;
|
||||
unset($_SESSION['auth_error']);
|
||||
|
||||
$redirect = $_GET['redirect'] ?? '/';
|
||||
header('Location: ' . $redirect);
|
||||
|
||||
Reference in New Issue
Block a user