Arreglos de seguridad e historial

This commit is contained in:
Mireya Cueto Garrido
2026-03-27 12:46:45 +01:00
parent e19e971cd6
commit 111acc632e
4 changed files with 69 additions and 24 deletions
+24
View File
@@ -1,5 +1,9 @@
import secrets
from passlib.context import CryptContext
from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from api.database.mongodb import users_collection
from bson import ObjectId
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
@@ -14,3 +18,23 @@ def verify_password(plain_password: str, hashed_password: str) -> bool:
def generate_token() -> str:
return secrets.token_hex(32) # 64 caracteres seguros
security_scheme = HTTPBearer()
async def get_current_user(
credentials: HTTPAuthorizationCredentials = Depends(security_scheme),
):
token = credentials.credentials
user = await users_collection.find_one({"token": token})
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Token inválido o usuario no autenticado",
)
# devolvemos el documento tal cual (dict)
user["id"] = str(user["_id"])
return user