35 lines
948 B
TypeScript
35 lines
948 B
TypeScript
/**
|
|
* Middleware functions for the proxy detection API
|
|
*/
|
|
|
|
import { FastifyRequest, FastifyReply } from 'fastify';
|
|
import { config } from './config';
|
|
|
|
/**
|
|
* API Key authentication middleware
|
|
* Skips authentication for health check and debug endpoints
|
|
*/
|
|
export async function authMiddleware(request: FastifyRequest, reply: FastifyReply) {
|
|
// Skip auth for health check and debug endpoints
|
|
if (request.url === '/health' || request.url === '/ip-debug') {
|
|
return;
|
|
}
|
|
|
|
const apiKey = request.headers['x-api-key'] || (request.query as Record<string, any>)?.api_key;
|
|
|
|
if (!apiKey) {
|
|
reply.code(401).send({
|
|
error: 'API key required',
|
|
message: 'Please provide API key in X-API-Key header or api_key query parameter'
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (apiKey !== config.api.key) {
|
|
reply.code(403).send({
|
|
error: 'Invalid API key',
|
|
message: 'The provided API key is not valid'
|
|
});
|
|
return;
|
|
}
|
|
}
|