/
githubmirror
/
novu
Обзор
Документация
Войти
/
githubmirror
/
novu
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
next
enterprise/workers/socket/src/middleware/internal-auth.ts
38 строк
1 KB
Adam Chmara
chore(root): apply biome formatter & linter fixes NV-6436 (#8838)
31 июл 2025, 14:59
Не верифицирован
31 июл 2025, 14:59
855fb8f
Код
Авторство
О чём код?
import type { Context, Next } from 'hono'; export async function authenticateInternalAPI(context: Context, next: Next) { const authHeader = context.req.header('Authorization'); const providedKey = authHeader?.replace('Bearer ', ''); if (!providedKey) { return context.json({ error: 'Unauthorized: Missing API key' }, 401); } const validApiKey = context.env.INTERNAL_API_KEY; if (!validApiKey) { console.error('INTERNAL_API_KEY environment variable not configured'); return context.json({ error: 'Server configuration error' }, 500); } // Use constant-time comparison to prevent timing attacks if (!constantTimeEquals(providedKey, validApiKey)) { return context.json({ error: 'Unauthorized: Invalid API key' }, 401); } await next(); } function constantTimeEquals(a: string, b: string): boolean { if (a.length !== b.length) { return false; } let result = 0; for (let i = 0; i < a.length; i += 1) { result |= a.charCodeAt(i) ^ b.charCodeAt(i); } return result === 0; }