Update: 2026-07-17 00:31:27
This commit is contained in:
@@ -1,25 +1,51 @@
|
|||||||
<?php
|
<?php
|
||||||
|
/**
|
||||||
|
* generate_mock_data.php
|
||||||
|
* Standalone JWT generator for stress testing.
|
||||||
|
* Does NOT use connect.php (which requires HTTP context and JWT auth).
|
||||||
|
* Reads the JWT secret the same way driver_socket.php does.
|
||||||
|
*/
|
||||||
error_reporting(0);
|
error_reporting(0);
|
||||||
ini_set('display_errors', 0);
|
ini_set('display_errors', '0');
|
||||||
// stress_test/generate_mock_data.php
|
|
||||||
// Generates a valid JWT for the driver using the system's JWT_SECRET_KEY
|
// Load JWT library from loction_server vendor
|
||||||
require_once dirname(__DIR__) . '/backend/connect.php';
|
|
||||||
require_once dirname(__DIR__) . '/loction_server/vendor/autoload.php';
|
require_once dirname(__DIR__) . '/loction_server/vendor/autoload.php';
|
||||||
|
|
||||||
use Firebase\JWT\JWT;
|
use Firebase\JWT\JWT;
|
||||||
use Firebase\JWT\Key;
|
|
||||||
|
|
||||||
|
// ── Load .env (same parser as driver_socket.php) ──
|
||||||
|
function loadEnvFile(string $path): void {
|
||||||
|
if (!file_exists($path)) return;
|
||||||
|
foreach (file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
|
||||||
|
if (str_starts_with(trim($line), '#') || !str_contains($line, '=')) continue;
|
||||||
|
[$name, $value] = explode('=', $line, 2);
|
||||||
|
putenv(trim($name) . '=' . trim($value, "\"'"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try location server env first (same source driver_socket.php uses)
|
||||||
|
loadEnvFile('/home/location/env/.env');
|
||||||
|
// Fallback: try backend .env
|
||||||
|
loadEnvFile(dirname(__DIR__) . '/backend/.env');
|
||||||
|
|
||||||
|
// ── Resolve JWT secret (same logic as driver_socket.php) ──
|
||||||
function getJwtSecret(): string {
|
function getJwtSecret(): string {
|
||||||
$keyPath = getenv('JWT_SECRET_KEY_PATH');
|
$keyPath = getenv('JWT_SECRET_KEY_PATH');
|
||||||
if ($keyPath && file_exists($keyPath)) {
|
if ($keyPath && file_exists($keyPath)) {
|
||||||
return trim(file_get_contents($keyPath));
|
return trim(file_get_contents($keyPath));
|
||||||
}
|
}
|
||||||
return getenv('JWT_SECRET_KEY') ?: '';
|
$key = getenv('JWT_SECRET_KEY');
|
||||||
|
if ($key) return $key;
|
||||||
|
// backend .env uses JWT_SECRET (without _KEY suffix)
|
||||||
|
$key = getenv('JWT_SECRET');
|
||||||
|
if ($key) return $key;
|
||||||
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$secret = getJwtSecret();
|
$secret = getJwtSecret();
|
||||||
if (empty($secret)) {
|
if (empty($secret)) {
|
||||||
die("Error: JWT_SECRET_KEY not found. Cannot generate mock JWT.\n");
|
echo json_encode(['error' => 'JWT secret not found', 'jwt' => '']);
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
$driverId = $argv[1] ?? '999999';
|
$driverId = $argv[1] ?? '999999';
|
||||||
@@ -28,12 +54,12 @@ $payload = [
|
|||||||
'sub' => (string)$driverId,
|
'sub' => (string)$driverId,
|
||||||
'role' => 'driver',
|
'role' => 'driver',
|
||||||
'iat' => time(),
|
'iat' => time(),
|
||||||
'exp' => time() + 86400
|
'exp' => time() + 86400,
|
||||||
];
|
];
|
||||||
|
|
||||||
$jwt = JWT::encode($payload, $secret, 'HS256');
|
$jwt = JWT::encode($payload, $secret, 'HS256');
|
||||||
|
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
'driver_id' => $driverId,
|
'driver_id' => $driverId,
|
||||||
'jwt' => $jwt
|
'jwt' => $jwt,
|
||||||
]);
|
]);
|
||||||
|
|||||||
Reference in New Issue
Block a user