feat: add admin menu, persistent json config, multi-provider support, and custom model fallback revolver
This commit is contained in:
parent
637a3cf255
commit
1c955619e7
21 changed files with 3742 additions and 388 deletions
52
src/auth.ts
52
src/auth.ts
|
|
@ -4,15 +4,35 @@ 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) {
|
||||
statsTracker.recordDirectFailure('missing_auth_header', req.path);
|
||||
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) {
|
||||
providedKey = req.query.api_key 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 header', path: req.path });
|
||||
requestLogger.logError(requestId, { message: 'Missing Authorization', path: req.path });
|
||||
res.status(401).json({
|
||||
error: {
|
||||
message: 'Missing Authorization header',
|
||||
message: 'Missing Authorization header or API key. Expected: Authorization: Bearer <api_key>',
|
||||
type: 'invalid_request_error',
|
||||
code: 'missing_api_key'
|
||||
}
|
||||
|
|
@ -20,26 +40,10 @@ export const authenticateApiKey = (req: Request, res: Response, next: NextFuncti
|
|||
return;
|
||||
}
|
||||
|
||||
// OpenAI format: "Bearer <api_key>"
|
||||
const parts = authHeader.split(' ');
|
||||
if (parts.length !== 2 || parts[0] !== 'Bearer') {
|
||||
statsTracker.recordDirectFailure('invalid_auth_format', req.path);
|
||||
const requestId = requestLogger.generateRequestId();
|
||||
requestLogger.logError(requestId, { message: 'Invalid Authorization format', path: req.path });
|
||||
res.status(401).json({
|
||||
error: {
|
||||
message: 'Invalid Authorization format. Expected: Bearer <api_key>',
|
||||
type: 'invalid_request_error',
|
||||
code: 'invalid_api_key_format'
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const providedKey = parts[1];
|
||||
|
||||
if (providedKey !== config.apiPassword) {
|
||||
statsTracker.recordDirectFailure('invalid_api_key', req.path);
|
||||
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({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue