Update: 2026-07-17 00:32:33
This commit is contained in:
@@ -1,65 +1,71 @@
|
||||
<?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.
|
||||
* Standalone JWT generator — ZERO external dependencies.
|
||||
* Generates HS256 JWT using pure PHP (no composer/vendor needed).
|
||||
*/
|
||||
error_reporting(0);
|
||||
ini_set('display_errors', '0');
|
||||
|
||||
// Load JWT library from loction_server vendor
|
||||
require_once dirname(__DIR__) . '/loction_server/vendor/autoload.php';
|
||||
// Show errors so we can debug
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', '1');
|
||||
|
||||
use Firebase\JWT\JWT;
|
||||
|
||||
// ── Load .env (same parser as driver_socket.php) ──
|
||||
// ── 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) {
|
||||
if (str_starts_with(trim($line), '#') || !str_contains($line, '=')) continue;
|
||||
$line = trim($line);
|
||||
if (empty($line) || $line[0] === '#' || strpos($line, '=') === false) continue;
|
||||
[$name, $value] = explode('=', $line, 2);
|
||||
putenv(trim($name) . '=' . trim($value, "\"'"));
|
||||
}
|
||||
}
|
||||
|
||||
// Try location server env first (same source driver_socket.php uses)
|
||||
// Try multiple .env locations
|
||||
loadEnvFile('/home/location/env/.env');
|
||||
// Fallback: try backend .env
|
||||
loadEnvFile(dirname(__DIR__) . '/backend/.env');
|
||||
|
||||
// ── Resolve JWT secret (same logic as driver_socket.php) ──
|
||||
// ── 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;
|
||||
// backend .env uses JWT_SECRET (without _KEY suffix)
|
||||
// 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', 'jwt' => '']);
|
||||
echo json_encode(['error' => 'JWT secret not found in any .env', 'jwt' => '']);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$driverId = $argv[1] ?? '999999';
|
||||
|
||||
$payload = [
|
||||
$jwt = generate_jwt($secret, [
|
||||
'sub' => (string)$driverId,
|
||||
'role' => 'driver',
|
||||
'iat' => time(),
|
||||
'exp' => time() + 86400,
|
||||
];
|
||||
|
||||
$jwt = JWT::encode($payload, $secret, 'HS256');
|
||||
|
||||
echo json_encode([
|
||||
'driver_id' => $driverId,
|
||||
'jwt' => $jwt,
|
||||
]);
|
||||
|
||||
echo json_encode(['driver_id' => $driverId, 'jwt' => $jwt]);
|
||||
|
||||
Reference in New Issue
Block a user