services/storage-api/lib/security.js Repository StreszCzarka---Krypto-AI-news-serwis Original path services/storage-api/lib/security.jsRole DOCS Size 835 bytes Lines 25 SHA-256 6e25366c8d6c12213903660b9e208071135bbe7e4adcd795302ace0e3907d18aDisplayed range 1–25 Previous file/page · Project index · Next file/page
function createApiKeyGuard(expectedKey) {
return function requireApiKey(req, res, next) {
if (!expectedKey) {
return res.status(503).json({ error: 'Internal API authentication is not configured' });
}
if (req.get('x-api-key') !== expectedKey) {
return res.status(401).json({ error: 'Unauthorized' });
}
return next();
};
}
function normalizeTableName(name) {
const safe = String(name || '');
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(safe)) throw new Error('Invalid table name');
return safe.charAt(0).toUpperCase() + safe.slice(1);
}
function safeField(name) {
const safe = String(name || '');
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(safe)) throw new Error('Invalid field name');
return safe;
}
module.exports = { createApiKeyGuard, normalizeTableName, safeField };