diff --git a/core/Auth/JwtService.php b/core/Auth/JwtService.php index 54fac05..91436b8 100644 --- a/core/Auth/JwtService.php +++ b/core/Auth/JwtService.php @@ -168,6 +168,33 @@ class JwtService self::abort(401, 'Invalid token issuer: expected ' . $this->issuer . ' but got ' . ($decoded->iss ?? 'none')); } + // 3.1 App Signature Verification (Security Layer) + $appSignature = $_SERVER['HTTP_X_APP_SIGNATURE'] ?? null; + if ($appSignature === null && function_exists('getallheaders')) { + $headers = array_change_key_case(getallheaders(), CASE_LOWER); + $appSignature = $headers['x-app-signature'] ?? null; + } + + // قائمة البصمات المعتمدة لكل تطبيق (يجب تعبئتها من ملف .env) + // APP_SIGNATURE_SERVICE, APP_SIGNATURE_DRIVER, APP_SIGNATURE_PASSENGER + $role = $decoded->role ?? 'unknown'; + $envKey = 'APP_SIGNATURE_' . strtoupper($role); + $expectedSignature = getenv($envKey) ?: getenv('APP_SIGNATURE_HASH'); + + if (!empty($expectedSignature)) { + if ($appSignature === null || !hash_equals($expectedSignature, $appSignature)) { + error_log("[SECURITY_ERROR] App Signature Mismatch/Missing! Role: $role | Expected: $expectedSignature | Got: " . ($appSignature ?? 'NONE') . " | User: $userId"); + + // الحظر النهائي: إذا كانت البصمة خاطئة، نرفض الطلب فوراً + self::abort(403, 'App integrity check failed. Please update your app.'); + } + } else { + // في حال لم يتم ضبط البصمة لهذا النوع من المستخدمين بعد، نسجلها فقط لتسهيل الإعداد + error_log("[SECURITY_INFO] Incoming App Signature for $role: " . ($appSignature ?? 'NONE') . " | User: $userId"); + } + + + // 4. User ID $userId = $decoded->user_id ?? $decoded->sub ?? null; if (!$userId) {