# modules/auth/auth.py import os import logging import bcrypt from azure.cosmos import CosmosClient from modules.database.sql_db import get_user logger = logging.getLogger(__name__) # Verificar las variables de entorno COSMOS_ENDPOINT = os.getenv("COSMOS_ENDPOINT") COSMOS_KEY = os.getenv("COSMOS_KEY") if not COSMOS_ENDPOINT or not COSMOS_KEY: raise ValueError("Las variables de entorno COSMOS_ENDPOINT y COSMOS_KEY no están configuradas.") cosmos_client = CosmosClient(COSMOS_ENDPOINT, COSMOS_KEY) def authenticate_user(username: str, password: str): """ Autentica un usuario utilizando la base de datos. """ try: user = get_user(username) if user and bcrypt.checkpw(password.encode("utf-8"), user["password"].encode("utf-8")): logger.info(f"Usuario autenticado: {username}") return True, user["role"] logger.warning(f"Credenciales incorrectas para: {username}") return False, None except Exception as e: logger.error(f"Error autenticando a {username}: {e}") return False, None