Files
CHIEFSOFT\ameye 987b7d6383 mercore starter
2025-06-22 20:45:07 -04:00

26 lines
817 B
Python

from functools import wraps
from flask import request, jsonify
from app.utils.logger import logger
from app.config import Config
# Load valid API key from environment variables (fallback for testing)
VALID_API_KEY = Config.VALID_API_KEY
def require_api_key(f):
"""Decorator to enforce API key authentication."""
@wraps(f)
def decorated_function(*args, **kwargs):
api_key = request.headers.get("X-API-KEY")
if not api_key:
logger.error("Unauthorized access: Missing API key.")
return jsonify({"message": "Invalid request"}), 400
if api_key != VALID_API_KEY:
logger.error("Unauthorized access: Invalid API key.")
return jsonify({"message": "Invalid request"}), 400
return f(*args, **kwargs)
return decorated_function