Files
fckbot/rag/auth.py
Markov Andrey 59feffc190 Update 9 files
- /rag/config_models.py
- /rag/utils/config_loader.py
- /rag/auth.py
- /rag/rag_server.py
- /rag/rag_api.py
- /rag/rag_orchestrator.py
- /rag/__init__.py
- /bots/xmpp/client.py
- /bots/rag_client.py
2026-06-30 14:27:34 +00:00

52 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""
Аутентификация для RAG-сервера.
Проверяет API-ключ в заголовке X-API-Key.
"""
import logging
from fastapi import HTTPException, Security
from fastapi.security import APIKeyHeader
from starlette.status import HTTP_403_FORBIDDEN
from .config_models import AppConfig
logger = logging.getLogger(__name__)
api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
# Глобальная ссылка на конфиг (устанавливается из rag_server)
_config: AppConfig = None
def set_auth_config(config: AppConfig):
"""Устанавливает конфиг для аутентификации."""
global _config
_config = config
def verify_api_key(api_key: str = Security(api_key_header)) -> str:
"""
Проверяет API-ключ. Возвращает ключ при успехе, иначе 403.
"""
if _config is None:
raise RuntimeError("Auth config not set")
if not _config.rag_api_key:
# Если ключ не задан пропускаем (но предупреждение уже выведено)
logger.warning("API-ключ не настроен, аутентификация отключена!")
return "insecure"
if not api_key:
raise HTTPException(
status_code=HTTP_403_FORBIDDEN,
detail="API-ключ отсутствует. Передайте его в заголовке X-API-Key."
)
if api_key != _config.rag_api_key:
raise HTTPException(
status_code=HTTP_403_FORBIDDEN,
detail="Неверный API-ключ."
)
return api_key