Files
Siro/stress_test/generate_mock_data.php
T

139 lines
4.5 KiB
PHP

<?php
/**
* generate_mock_data.php
* Standalone JWT & Price Token generator for Stress Test.
*/
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE);
ini_set('display_errors', '0');
$_SERVER['REQUEST_METHOD'] = $_SERVER['REQUEST_METHOD'] ?? 'POST';
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_HOST'] ?? 'localhost';
$_SERVER['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1';
// Load bootstrap if available for EncryptionHelper
$bootstrapPath = dirname(__DIR__) . '/backend/core/bootstrap.php';
if (file_exists($bootstrapPath)) {
try {
require_once $bootstrapPath;
} catch (Throwable $e) {}
}
// Fallback .env loader
$loadedFiles = [];
function loadEnvFile(string $path): void {
global $loadedFiles;
if (!file_exists($path)) {
$loadedFiles[] = "NOT_FOUND: $path";
return;
}
$loadedFiles[] = "LOADED: $path";
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);
$val = trim($value, "\"'");
putenv(trim($name) . '=' . $val);
$_ENV[trim($name)] = $val;
$_SERVER[trim($name)] = $val;
}
}
loadEnvFile(dirname(__DIR__) . '/docker/.env');
loadEnvFile(dirname(__DIR__) . '/backend/.env');
function getJwtSecret(): string {
$keyPath = getenv('JWT_SECRET_KEY_PATH');
if ($keyPath && file_exists($keyPath)) {
return trim(file_get_contents($keyPath));
}
$key = getenv('JWT_SECRET_KEY') ?: ($_ENV['JWT_SECRET_KEY'] ?? '');
if ($key) return $key;
$key = getenv('JWT_SECRET') ?: ($_ENV['JWT_SECRET'] ?? '');
if ($key) return $key;
return '';
}
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";
}
$secret = getJwtSecret();
if (empty($secret)) {
echo json_encode(['error' => 'JWT secret not found in any .env', 'jwt' => '']);
exit(0);
}
$userId = $argv[1] ?? '999999';
$role = $argv[2] ?? 'driver';
$aud = ($role === 'driver') ? (getenv('allowedDriver2') ?: 'driver-app:ios') : (getenv('allowed2') ?: 'passenger-app:ios');
$fpPepper = getenv('FP_PEPPER') ?: ($_ENV['FP_PEPPER'] ?? '');
$fpHeader = 'mock_device_fp_' . $userId;
$fpInToken = (!empty($fpPepper)) ? hash('sha256', $fpHeader . $fpPepper) : null;
$payload = [
'iss' => getenv('APP_ISSUER') ?: 'SiroApp',
'aud' => $aud,
'user_id' => (string)$userId,
'sub' => (string)$userId,
'role' => $role,
'token_type' => 'access',
'jti' => bin2hex(random_bytes(16)),
'iat' => time(),
'exp' => time() + 86400,
];
if ($fpInToken) {
$payload['fingerPrint'] = $fpInToken;
}
$jwt = generate_jwt($secret, $payload);
// Generate encrypted price token for passenger matching add_ride.php
$priceToken = '';
if ($role === 'passenger') {
$tokenData = json_encode([
'passenger_id' => (string)$userId,
'start_location' => '31.95,35.91',
'end_location' => '31.96,35.92',
'expires' => time() + 86400,
'prices' => [
'Economy' => [
'price' => '5',
'driver_price' => '4.5'
]
]
]);
if (isset($GLOBALS['encryptionHelper']) && is_object($GLOBALS['encryptionHelper'])) {
$priceToken = $GLOBALS['encryptionHelper']->encryptData($tokenData);
} elseif (isset($encryptionHelper) && is_object($encryptionHelper)) {
$priceToken = $encryptionHelper->encryptData($tokenData);
} else {
try {
$encKey = getenv('ENCRYPTION_KEY') ?: $secret;
$keyBytes = substr(hash('sha256', $encKey, true), 0, 32);
$iv = random_bytes(12);
$tag = '';
$ciphertext = openssl_encrypt($tokenData, 'aes-256-gcm', $keyBytes, OPENSSL_RAW_DATA, $iv, $tag);
$priceToken = 'GCM:' . base64_encode($iv) . ':' . base64_encode($ciphertext . $tag);
} catch (Throwable $e) {
$priceToken = 'dummy';
}
}
}
echo json_encode([
'user_id' => $userId,
'role' => $role,
'jwt' => $jwt,
'fp' => $fpHeader,
'price_token' => $priceToken
]);