first commit

This commit is contained in:
2026-04-01 13:48:40 -07:00
commit 61d386a7c7
178 changed files with 3827 additions and 0 deletions

64
app/core/security.py Normal file
View File

@@ -0,0 +1,64 @@
# core/security.py
from passlib.context import CryptContext
from datetime import datetime, timedelta
from typing import Optional
#
import os
import jwt
import logging
logger = logging.getLogger(__name__)
# Configuración de hashing de contraseñas
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
# Configuración de JWT
SECRET_KEY = os.getenv("SECRET_KEY", "xma-secret-key-change-in-production")
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_HOURS = int(os.getenv("ACCESS_TOKEN_EXPIRE_HOURS", "1"))
def get_password_hash(password: str) -> str:
"""Genera hash de contraseña con bcrypt"""
if password.startswith("$2b$") or password.startswith("$2a$"):
raise ValueError("La contrasena ya esta hasheada")
password_bytes = password.encode('utf-8')
if len(password_bytes) > 72:
password = password[:72]
return pwd_context.hash(password)
def verify_password(plain_password: str, hashed_password: str) -> bool:
try:
return pwd_context.verify(plain_password, hashed_password)
except Exception as e:
logger.error(f"Error verifying password: {e}")
return False
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:
"""Crea token JWT"""
to_encode = data.copy()
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(hours=ACCESS_TOKEN_EXPIRE_HOURS)
to_encode.update({"exp": expire, "iat": datetime.utcnow()})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
def decode_access_token(token: str) -> dict:
"""Decodifica token JWT"""
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
return payload
except jwt.ExpiredSignatureError:
raise ValueError("Token expirado")
except jwt.InvalidTokenError:
raise ValueError("Token inválido")