feat(security): soportar headers flexibles, query y body json para la API Key de Moodle
This commit is contained in:
@@ -2390,14 +2390,21 @@ def _verify_moodle_api_key_or_admin() -> bool:
|
|||||||
from app.models.setting import SystemSetting
|
from app.models.setting import SystemSetting
|
||||||
import secrets
|
import secrets
|
||||||
|
|
||||||
# Verificar Header de API Key (X-API-Key o Authorization: ApiKey ...)
|
# Extraer API Key de header, query param o body JSON
|
||||||
api_key_header = request.headers.get('X-API-Key') or request.headers.get('X-Moodle-Api-Key')
|
api_key = (
|
||||||
if not api_key_header and request.headers.get('Authorization', '').startswith('ApiKey '):
|
request.headers.get('X-API-Key')
|
||||||
api_key_header = request.headers.get('Authorization', '').replace('ApiKey ', '').strip()
|
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', '')
|
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
|
return True
|
||||||
|
|
||||||
acting_user = getattr(g, 'jwt_user', None)
|
acting_user = getattr(g, 'jwt_user', None)
|
||||||
|
|||||||
Reference in New Issue
Block a user