openai-mock-api
Version:
A mock OpenAI API server for testing LLM applications
49 lines • 1.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthenticationService = exports.AuthenticationMiddleware = void 0;
class AuthenticationMiddleware {
constructor(authService, logger) {
this.authService = authService;
this.logger = logger;
}
middleware() {
return (req, res, next) => {
// Skip auth for non-API routes
if (!req.path.startsWith('/v1')) {
return next();
}
const authHeader = req.headers.authorization;
if (!authHeader) {
this.logger.warn('Missing authorization header');
return this.sendUnauthorized(res, 'Authorization header is required');
}
const token = authHeader.replace('Bearer ', '');
if (!this.authService.validateApiKey(token)) {
this.logger.warn('Invalid API key provided');
return this.sendUnauthorized(res, 'Invalid API key provided');
}
next();
};
}
sendUnauthorized(res, message) {
const error = {
error: {
message,
type: 'invalid_request_error',
code: 'invalid_api_key',
},
};
res.status(401).json(error);
}
}
exports.AuthenticationMiddleware = AuthenticationMiddleware;
class AuthenticationService {
constructor(apiKey) {
this.apiKey = apiKey;
}
validateApiKey(apiKey) {
return apiKey === this.apiKey;
}
}
exports.AuthenticationService = AuthenticationService;
//# sourceMappingURL=auth.middleware.js.map