feat(security): soportar headers flexibles, query y body json para la API Key de Moodle

This commit is contained in:
2026-10-16 16:22:19 -03:00
parent 1560bc2a67
commit ac1e94b927
+13 -6
View File
@@ -2390,14 +2390,21 @@ def _verify_moodle_api_key_or_admin() -> bool:
from app.models.setting import SystemSetting
import secrets
# Verificar Header de API Key (X-API-Key o Authorization: ApiKey ...)
api_key_header = request.headers.get('X-API-Key') or request.headers.get('X-Moodle-Api-Key')
if not api_key_header and request.headers.get('Authorization', '').startswith('ApiKey '):
api_key_header = request.headers.get('Authorization', '').replace('ApiKey ', '').strip()
# Extraer API Key de header, query param o body JSON
api_key = (
request.headers.get('X-API-Key')
or request.headers.get('x-api-key')
or request.headers.get('X-Moodle-Api-Key')
or request.headers.get('x-moodle-api-key')
or request.args.get('api_key')
or (request.get_json(silent=True) or {}).get('api_key')
)
if not api_key and request.headers.get('Authorization', '').startswith('ApiKey '):
api_key = request.headers.get('Authorization', '').replace('ApiKey ', '').strip()
if api_key_header:
if api_key:
saved_key = SystemSetting.get_decrypted_value('moodle_api_key', '')
if saved_key and secrets.compare_digest(api_key_header.strip(), saved_key.strip()):
if saved_key and secrets.compare_digest(str(api_key).strip(), saved_key.strip()):
return True
acting_user = getattr(g, 'jwt_user', None)