Files
Siro/stress_test/generate_mock_data.php
T

72 lines
2.1 KiB
PHP

<?php
/**
* generate_mock_data.php
* Standalone JWT generator — ZERO external dependencies.
* Generates HS256 JWT using pure PHP (no composer/vendor needed).
*/
// Show errors so we can debug
error_reporting(E_ALL);
ini_set('display_errors', '1');
// ── Load .env ──
function loadEnvFile(string $path): void {
if (!file_exists($path)) return;
foreach (file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
$line = trim($line);
if (empty($line) || $line[0] === '#' || strpos($line, '=') === false) continue;
[$name, $value] = explode('=', $line, 2);
putenv(trim($name) . '=' . trim($value, "\"'"));
}
}
// Try multiple .env locations
loadEnvFile('/home/location/env/.env');
loadEnvFile(dirname(__DIR__) . '/backend/.env');
// ── Resolve JWT secret ──
function getJwtSecret(): string {
// Priority 1: key file
$keyPath = getenv('JWT_SECRET_KEY_PATH');
if ($keyPath && file_exists($keyPath)) {
return trim(file_get_contents($keyPath));
}
// Priority 2: JWT_SECRET_KEY env var
$key = getenv('JWT_SECRET_KEY');
if ($key) return $key;
// Priority 3: JWT_SECRET env var (backend .env uses this)
$key = getenv('JWT_SECRET');
if ($key) return $key;
return '';
}
// ── Pure PHP HS256 JWT (no libraries needed) ──
function base64url_encode(string $data): string {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
function generate_jwt(string $secret, array $payload): string {
$header = base64url_encode(json_encode(['alg' => 'HS256', 'typ' => 'JWT']));
$body = base64url_encode(json_encode($payload));
$sig = base64url_encode(hash_hmac('sha256', "$header.$body", $secret, true));
return "$header.$body.$sig";
}
// ── Main ──
$secret = getJwtSecret();
if (empty($secret)) {
echo json_encode(['error' => 'JWT secret not found in any .env', 'jwt' => '']);
exit(1);
}
$driverId = $argv[1] ?? '999999';
$jwt = generate_jwt($secret, [
'sub' => (string)$driverId,
'role' => 'driver',
'iat' => time(),
'exp' => time() + 86400,
]);
echo json_encode(['driver_id' => $driverId, 'jwt' => $jwt]);