| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | import type { Request, Response, NextFunction } from 'express' |
| |
|
| | function getValidKeys(): Set<string> | null { |
| | const multi = process.env.GODMODE_API_KEYS |
| | if (multi) { |
| | return new Set(multi.split(',').map(k => k.trim()).filter(Boolean)) |
| | } |
| | const single = process.env.GODMODE_API_KEY |
| | if (single) { |
| | return new Set([single.trim()]) |
| | } |
| | return null |
| | } |
| |
|
| | export function apiKeyAuth(req: Request, res: Response, next: NextFunction): void { |
| | const validKeys = getValidKeys() |
| |
|
| | |
| | if (!validKeys) { |
| | ;(req as any).apiKeyId = 'anonymous' |
| | next() |
| | return |
| | } |
| |
|
| | const authHeader = req.headers.authorization |
| | if (!authHeader || !authHeader.startsWith('Bearer ')) { |
| | res.status(401).json({ |
| | error: 'Missing or invalid Authorization header. Use: Bearer <your-api-key>', |
| | }) |
| | return |
| | } |
| |
|
| | const key = authHeader.slice(7).trim() |
| | if (!validKeys.has(key)) { |
| | res.status(403).json({ error: 'Invalid API key' }) |
| | return |
| | } |
| |
|
| | |
| | ;(req as any).apiKeyId = key.slice(0, 8) |
| | next() |
| | } |
| |
|