99 lines
3.9 KiB
PHP
Executable File
99 lines
3.9 KiB
PHP
Executable File
<?php
|
|
require_once __DIR__ . '/../core/bootstrap.php';
|
|
|
|
$fingerprint = filterRequest('fingerprint');
|
|
$password = filterRequest('password');
|
|
$audience = filterRequest('aud') ?? 'service';
|
|
|
|
if (empty($fingerprint) || empty($password)) {
|
|
jsonError("Fingerprint and password are required.");
|
|
exit();
|
|
}
|
|
|
|
try {
|
|
$con = Database::get('main');
|
|
|
|
// البحث بالبصمة للموظف
|
|
$fpHash = hash('sha256', $fingerprint);
|
|
$sql = "SELECT * FROM `users` WHERE `fingerprint_hash` = :fp AND `user_type` = 'service' LIMIT 1";
|
|
$stmt = $con->prepare($sql);
|
|
$stmt->execute([':fp' => $fpHash]);
|
|
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($user) {
|
|
// التحقق من حالة الحساب
|
|
if ($user['status'] === 'pending') {
|
|
jsonError("حسابك قيد المراجعة حالياً. يرجى الانتظار للموافقة.");
|
|
exit();
|
|
} elseif ($user['status'] === 'suspended') {
|
|
jsonError("هذا الحساب معلق. يرجى التواصل مع الإدارة.");
|
|
exit();
|
|
} elseif ($user['status'] !== 'approved') {
|
|
jsonError("لم يتم تفعيل حسابك بعد.");
|
|
exit();
|
|
}
|
|
|
|
// التحقق من كلمة المرور
|
|
|
|
if (password_verify($password, $user['password'])) {
|
|
|
|
// فك تشفير البيانات للعرض في التطبيق
|
|
$user['first_name'] = $encryptionHelper->decryptData($user['first_name']) ?: $user['first_name'];
|
|
$user['last_name'] = $encryptionHelper->decryptData($user['last_name']) ?: $user['last_name'];
|
|
$user['email'] = $encryptionHelper->decryptData($user['email']) ?: $user['email'];
|
|
$user['phone'] = $encryptionHelper->decryptData($user['phone']) ?: $user['phone'];
|
|
|
|
unset($user['password']);
|
|
|
|
// توليد التوكن أو استرجاع التوكن الحالي إذا كان صالحاً
|
|
$jwtService = new JwtService($redis);
|
|
$role = 'service';
|
|
$ttl = 14400; // 4 hours
|
|
$jwt = null;
|
|
$expires_in = $ttl;
|
|
|
|
// محاولة استعادة التوكن الحالي من Redis لتجنب التكرار
|
|
if ($redis) {
|
|
$existingToken = $redis->get("active_token:{$user['id']}:{$audience}");
|
|
if ($existingToken) {
|
|
$decoded = $jwtService->decodeToken($existingToken);
|
|
// يجب أن يكون التوكن صالحاً ويحتوي على بصمة الجهاز (إذا كان التشفير مفعلاً)
|
|
if ($decoded && $decoded->exp > time() && (isset($decoded->fingerPrint) || empty($jwtService->getFpPepper()))) {
|
|
$jwt = $existingToken;
|
|
$expires_in = $decoded->exp - time();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// إذا لم يوجد توكن صالح، نولد واحداً جديداً ونلغي القديم
|
|
if (!$jwt) {
|
|
if ($redis) {
|
|
$oldJti = $redis->get("active_jti:{$user['id']}");
|
|
if ($oldJti) {
|
|
$jwtService->revokeToken($oldJti, $ttl);
|
|
}
|
|
}
|
|
$jwt = $jwtService->generateAccessToken($user['id'], $role, $audience, $fingerprint);
|
|
$expires_in = $ttl;
|
|
}
|
|
|
|
printSuccess([
|
|
"message" => "Login successful",
|
|
"data" => $user,
|
|
"jwt" => $jwt,
|
|
"expires_in" => $expires_in
|
|
]);
|
|
|
|
} else {
|
|
jsonError("Incorrect password");
|
|
}
|
|
} else {
|
|
jsonError("الجهاز غير مسجل لموظف خدمة.");
|
|
}
|
|
} catch (Exception $e) {
|
|
error_log("[ServiceApp Login Error] " . $e->getMessage());
|
|
jsonError("Server error: " . $e->getMessage());
|
|
}
|
|
|
|
exit(); |