Make OTP verification independent of the encryption mode
The verification tables (token_verification*, phone_verification*) use the phone number as a lookup key: written when the code is sent, read when it is checked. Storing it encrypted worked only because encryptData() is deterministic — under AES-GCM the two sides would produce different ciphertexts and no code would ever verify, locking every user out of registration and OTP sign-in. otpPhoneKey() stores a keyed HMAC of the normalised number instead. No schema change is needed since the column is textual, local and international formats now resolve to the same key, and the value cannot be reversed without the pepper. It falls back to the previous behaviour when no pepper is configured. Applied to both sides of every affected flow — request/verify, and the driver and passenger send/verify pairs — including the OTP value itself where it is compared by equality rather than decrypted. auth/otp/verify.php already decrypts the token before comparing, so it needed no change there. Also adds ENCRYPTION_MODE to EncryptionHelper: encryptData() writes GCM when set to 'gcm', CBC otherwise. Verified in both directions — rows written under CBC stay readable after switching, and rows written under GCM stay readable after rolling back — so the switch is reversible by an environment variable. The admin console's own OTP is unaffected: it keys the table by the stored ciphertext read from adminUser, identical on both sides. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
c9b4d14da6
commit
a1c19b052d
@@ -38,6 +38,31 @@ function filterRequest(string $name, string $type = 'string'): mixed
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* مفتاح بحث ثابت لجداول التحقق (token_verification*, phone_verification*).
|
||||
*
|
||||
* هذه الجداول تستخدم رقم الهاتف كمفتاح بحث لا كبيان يُعرض: يُكتب عند الإرسال
|
||||
* ويُقرأ عند التحقق. تخزينه مشفّراً كان يعمل فقط لأن التشفير حتمي — ومع
|
||||
* AES-GCM العشوائي يُنتج الإرسال والتحقق قيمتين مختلفتين فلا ينجح أي رمز.
|
||||
*
|
||||
* البديل: بصمة HMAC حتمية للرقم بعد تطبيعه. لا تحتاج تعديل المخطط (العمود
|
||||
* نصي أصلاً)، وتوحّد صيغ الرقم المحلية والدولية، ولا يمكن عكسها بلا المفتاح.
|
||||
*/
|
||||
function otpPhoneKey(?string $phone): string
|
||||
{
|
||||
if ($phone === null || trim($phone) === '') return '';
|
||||
|
||||
global $blindIndex, $encryptionHelper;
|
||||
|
||||
if ($blindIndex) {
|
||||
return 'K:' . $blindIndex->index('otp.phone', $phone);
|
||||
}
|
||||
|
||||
// بلا BLIND_INDEX_PEPPER نعود للسلوك القديم حتى لا يتعطل التحقق
|
||||
return $encryptionHelper ? $encryptionHelper->encryptData($phone) : $phone;
|
||||
}
|
||||
|
||||
// ── ردود JSON موحدة ─────────────────────────────────────────
|
||||
function jsonSuccess(mixed $data = null, string $message = 'success', int $code = 200): never
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user