|
@@ -6,24 +6,25 @@ import { logger } from '../../utils/logger';
|
|
|
*/
|
|
*/
|
|
|
export function authMiddleware(apiKey: string) {
|
|
export function authMiddleware(apiKey: string) {
|
|
|
return (req: Request, res: Response, next: NextFunction): void => {
|
|
return (req: Request, res: Response, next: NextFunction): void => {
|
|
|
|
|
+ const clientIp = req.ip || req.socket.remoteAddress || 'unknown';
|
|
|
const authHeader = req.headers.authorization;
|
|
const authHeader = req.headers.authorization;
|
|
|
|
|
|
|
|
if (!authHeader) {
|
|
if (!authHeader) {
|
|
|
- logger.warn('Authentication failed: No authorization header');
|
|
|
|
|
|
|
+ logger.warn(`Authentication failed: No authorization header from ${clientIp}`);
|
|
|
res.status(401).json({ error: 'No authorization header provided' });
|
|
res.status(401).json({ error: 'No authorization header provided' });
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const parts = authHeader.split(' ');
|
|
const parts = authHeader.split(' ');
|
|
|
if (parts.length !== 2 || parts[0] !== 'Bearer') {
|
|
if (parts.length !== 2 || parts[0] !== 'Bearer') {
|
|
|
- logger.warn('Authentication failed: Invalid authorization format');
|
|
|
|
|
|
|
+ logger.warn(`Authentication failed: Invalid authorization format from ${clientIp}`);
|
|
|
res.status(401).json({ error: 'Invalid authorization format. Use: Bearer <token>' });
|
|
res.status(401).json({ error: 'Invalid authorization format. Use: Bearer <token>' });
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const token = parts[1];
|
|
const token = parts[1];
|
|
|
if (token !== apiKey) {
|
|
if (token !== apiKey) {
|
|
|
- logger.warn('Authentication failed: Invalid token');
|
|
|
|
|
|
|
+ logger.warn(`Authentication failed: Invalid token from ${clientIp}`);
|
|
|
res.status(401).json({ error: 'Invalid API key' });
|
|
res.status(401).json({ error: 'Invalid API key' });
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|