Arreglos de seguridad e historial
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user