18 lines
569 B
Python
18 lines
569 B
Python
|
|
"""Simple API-key authentication decorator."""
|
||
|
|
import os
|
||
|
|
from functools import wraps
|
||
|
|
from flask import request, jsonify
|
||
|
|
|
||
|
|
|
||
|
|
def require_api_key(f):
|
||
|
|
@wraps(f)
|
||
|
|
def decorated(*args, **kwargs):
|
||
|
|
key = request.headers.get("X-API-Key") or request.args.get("api_key")
|
||
|
|
expected = os.environ.get("API_KEY", "")
|
||
|
|
if not expected:
|
||
|
|
return jsonify({"error": "API key not configured on server"}), 500
|
||
|
|
if key != expected:
|
||
|
|
return jsonify({"error": "Unauthorized"}), 401
|
||
|
|
return f(*args, **kwargs)
|
||
|
|
return decorated
|