- Add TelemetryTracker to record request fallback journeys, tracking step-by-step cascades across tiers, candidate error codes, cooldowns, and next actions - Add LiveWatcher for RAM-only interactive request inspection with 250-item FIFO safety cap and automatic memory flush on toggle off - Add REST and SSE streaming endpoints for real-time telemetry and live request logs - Upgrade Admin Telemetry tab with visual fallback breadcrumbs and live key health matrix - Add dedicated Live Logs tab with deep multi-tab inspector (Input, Output, Metadata, Debug), search, filters, and JSON export - Add unit and integration tests (test-live-watcher.ts, test-telemetry-fallback.ts)
60 lines
1.9 KiB
TypeScript
60 lines
1.9 KiB
TypeScript
import { Request, Response, NextFunction } from 'express';
|
|
import { config } from './config';
|
|
import { requestLogger } from './logger';
|
|
import { statsTracker } from './stats';
|
|
|
|
export const authenticateApiKey = (req: Request, res: Response, next: NextFunction): void => {
|
|
let providedKey: string | undefined;
|
|
|
|
const authHeader = req.headers.authorization;
|
|
if (authHeader) {
|
|
const parts = authHeader.split(' ');
|
|
if (parts.length === 2 && parts[0].toLowerCase() === 'bearer') {
|
|
providedKey = parts[1];
|
|
} else if (parts.length === 1) {
|
|
providedKey = parts[0];
|
|
}
|
|
}
|
|
|
|
if (!providedKey) {
|
|
providedKey = (req.headers['x-api-key'] as string) || (req.headers['x-admin-key'] as string);
|
|
}
|
|
|
|
if (!providedKey && (req.query.api_key || req.query.token || req.query.apiKey)) {
|
|
providedKey = (req.query.api_key || req.query.token || req.query.apiKey) as string;
|
|
}
|
|
|
|
if (!providedKey) {
|
|
if (req.path.startsWith('/v1')) {
|
|
statsTracker.recordDirectFailure('missing_auth_header', req.path);
|
|
}
|
|
const requestId = requestLogger.generateRequestId();
|
|
requestLogger.logError(requestId, { message: 'Missing Authorization', path: req.path });
|
|
res.status(401).json({
|
|
error: {
|
|
message: 'Missing Authorization header or API key. Expected: Authorization: Bearer <api_key>',
|
|
type: 'invalid_request_error',
|
|
code: 'missing_api_key'
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (providedKey !== config.apiPassword) {
|
|
if (req.path.startsWith('/v1')) {
|
|
statsTracker.recordDirectFailure('invalid_api_key', req.path);
|
|
}
|
|
const requestId = requestLogger.generateRequestId();
|
|
requestLogger.logError(requestId, { message: 'Invalid API key', path: req.path });
|
|
res.status(401).json({
|
|
error: {
|
|
message: 'Invalid API key',
|
|
type: 'invalid_request_error',
|
|
code: 'invalid_api_key'
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
|
|
next();
|
|
};
|