Enforce API key quota within the database transaction

This commit is contained in:
2026-09-06 10:40:45 +03:00
parent 1c0b48655e
commit 1a62a876e6
5 changed files with 63 additions and 11 deletions
+3 -4
View File
@@ -1174,10 +1174,8 @@ const server = http.createServer(async (req, res) => {
if (req.method === 'POST') {
try {
const mevcut = store.listApiKeys(user.email).length;
kotaKontrol(user.email, 'apiAnahtari', mevcut, 1, { isAdmin: user.isAdmin });
const body = await readBody(req).catch(() => ({}));
const k = store.createApiKey(user.email, body.ad);
const body = await readBody(req);
const k = store.createApiKey(user.email, body.ad, { limit: haklar(user.email, { isAdmin: user.isAdmin }).apiAnahtari });
// `anahtar` YALNIZCA burada döner; sunucuda yalnızca hash'i saklanır
return json(res, 200, { success: true, anahtar: k });
} catch (e) {
@@ -1188,6 +1186,7 @@ const server = http.createServer(async (req, res) => {
const ok = store.revokeApiKey(user.email, url.searchParams.get('id'));
return json(res, ok ? 200 : 404, { success: ok, error: ok ? undefined : 'Anahtar bulunamadı veya bu hesaba ait değil.', anahtarlar: store.listApiKeys(user.email), limit: haklar(user.email, { isAdmin: user.isAdmin }).apiAnahtari });
}
if (req.method !== 'GET') return json(res, 405, { success: false, error: 'Desteklenmeyen metot.' });
return json(res, 200, { success: true, anahtarlar: store.listApiKeys(user.email), limit: haklar(user.email, { isAdmin: user.isAdmin }).apiAnahtari });
}