From 498a1c8335652c0883ff747defe7a23ee378451a Mon Sep 17 00:00:00 2001 From: "Juan M. Ley" Date: Sun, 26 Apr 2026 16:42:47 -0600 Subject: [PATCH] another change Co-authored-by: Copilot --- src/infrastructure/api/users/auth_service.py | 32 ++++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/infrastructure/api/users/auth_service.py b/src/infrastructure/api/users/auth_service.py index 5d2cd58..2e0b5be 100644 --- a/src/infrastructure/api/users/auth_service.py +++ b/src/infrastructure/api/users/auth_service.py @@ -1,4 +1,5 @@ import jwt +import hashlib from datetime import datetime, timedelta from passlib.context import CryptContext from typing import Optional, Dict @@ -16,19 +17,32 @@ class AuthService: self.algorithm = ConfSettings.jwt_algorithm self.expiration_hours = ConfSettings.jwt_expiration_hours - def hash_password(self, password: str) -> str: + def _hash_password_sha256(self, password: str) -> str: """ - Hashea una contraseña usando bcrypt + Hashea una contraseña con SHA256 para reducir su tamaño Args: password: Contraseña en texto plano Returns: - Hash bcrypt de la contraseña + Hash SHA256 en hexadecimal (64 caracteres, siempre < 72 bytes) """ - # Bcrypt tiene límite de 72 bytes - password_truncated = password[:72] - return pwd_context.hash(password_truncated) + return hashlib.sha256(password.encode()).hexdigest() + + def hash_password(self, password: str) -> str: + """ + Hashea una contraseña usando SHA256 + bcrypt + + Args: + password: Contraseña en texto plano + + Returns: + Hash bcrypt de la contraseña (SHA256 + bcrypt) + """ + # Primero hash SHA256 (64 chars hex, nunca > 72 bytes) + # Luego bcrypt para mayor seguridad + sha256_hash = self._hash_password_sha256(password) + return pwd_context.hash(sha256_hash) def verify_password(self, plain_password: str, hashed_password: str) -> bool: """ @@ -41,9 +55,9 @@ class AuthService: Returns: True si la contraseña es correcta, False en caso contrario """ - # Bcrypt tiene límite de 72 bytes - plain_password_truncated = plain_password[:72] - return pwd_context.verify(plain_password_truncated, hashed_password) + # Aplicar mismo proceso: SHA256 primero, luego verificar con bcrypt + sha256_hash = self._hash_password_sha256(plain_password) + return pwd_context.verify(sha256_hash, hashed_password) def create_access_token(self, user_id: int, email: str) -> str: """