Files
intaleq_v3_pure_php/Admin/auth/loginWallet.php
Hamza-Ayed e999cd4ffa admin 12
2026-04-30 18:23:29 +03:00

81 lines
2.6 KiB
PHP

<?php
/**
* Admin/auth/loginWallet.php
* توليد توكن خاص بسيرفر المحفظة (Wallet SSO)
* يتم توقيعه بالمفتاح المشترك (SECRET_KEY_PAY)
*/
declare(strict_types=1);
require_once __DIR__ . '/../../core/bootstrap.php';
use Firebase\JWT\JWT;
// التحقق من الجلسة الحالية للأدمن
$admin = authenticateJWT();
if ($admin->role !== 'admin') {
jsonError("Unauthorized. Admin access required.");
exit;
}
try {
// جلب المفتاح المشترك لسيرفر المحفظة
// الأولوية لملف المفتاح المخصص للمدفوعات إن وجد، وإلا نستخدم الـ env
$payKeyPath = '/home/intaleq-api/.secret_key_pay';
$payKey = file_exists($payKeyPath) ? trim(file_get_contents($payKeyPath)) : getenv('SECRET_KEY_PAY');
if (empty($payKey)) {
// Fallback للمفتاح الرئيسي إذا لم يتوفر مفتاح خاص بالدفع (يجب التأكد من تطابقه مع سيرفر المحفظة)
$payKey = trim(@file_get_contents('/home/intaleq-api/.secret_key'));
}
if (empty($payKey)) {
jsonError("Internal configuration error: Shared secret key missing.");
exit;
}
$issuer = getenv('APP_ISSUER') ?: 'Tripz-Wallet';
$audience = 'Tripz-Wallet';
$hmacSecret = getenv('SECRET_KEY_HMAC') ?: '';
$ttl = 3600; // ساعة واحدة
$iat = time();
$exp = $iat + $ttl;
// محتوى التوكن (Payload)
$payload = [
'iss' => $issuer,
'aud' => $audience,
'user_id' => $admin->user_id,
'role' => 'admin',
'iat' => $iat,
'exp' => $exp,
'jti' => bin2hex(random_bytes(16))
];
// إضافة بصمة الجهاز للتوكن لزيادة الأمان
$fpHeader = $_SERVER['HTTP_X_DEVICE_FP'] ?? null;
$fpPepper = getenv('FP_PEPPER');
if ($fpHeader && $fpPepper) {
$payload['fingerPrint'] = hash('sha256', $fpHeader . $fpPepper);
}
// توليد التوكن
$jwt = JWT::encode($payload, $payKey, 'HS256');
// حساب الـ HMAC Hash المطلوب لسيرفر المحفظة للتحقق
// بناءً على authenticateJWT المرسل: hash_hmac('sha256', $userId, $hmacSecret)
$hmacHash = hash_hmac('sha256', (string)$admin->user_id, $hmacSecret);
printSuccess([
"status" => "success",
"jwt" => $jwt,
"hmac" => $hmacHash,
"expires_in" => $ttl
]);
} catch (Exception $e) {
error_log("[Admin Wallet SSO Error] " . $e->getMessage());
jsonError("Server Error: " . $e->getMessage());
}