Harden password hashing backend selection

This commit is contained in:
TronoSfera 2026-01-19 12:05:28 +03:00
parent dc228618c5
commit 0c6df7949e

View file

@ -35,15 +35,19 @@ ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", "60")) ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", "60"))
# Password hashing context def _build_password_context() -> CryptContext:
try: """Create a password hashing context with a safe backend fallback."""
passlib_bcrypt.bcrypt.set_backend("builtin") try:
passlib_bcrypt.bcrypt_sha256.set_backend("builtin") passlib_bcrypt.bcrypt.set_backend("builtin")
except Exception: passlib_bcrypt.bcrypt_sha256.set_backend("builtin")
# If the builtin backend is unavailable, fall back to the default backend. passlib_bcrypt.bcrypt_sha256.hash("passlib-backend-check")
pass return CryptContext(schemes=["bcrypt_sha256", "bcrypt"], deprecated="auto")
except Exception:
# bcrypt backends can fail with newer bcrypt releases; fall back to pbkdf2.
return CryptContext(schemes=["pbkdf2_sha256"], deprecated="auto")
pwd_context = CryptContext(schemes=["bcrypt_sha256", "bcrypt"], deprecated="auto")
pwd_context = _build_password_context()
def _normalize_bcrypt_password(password: str) -> str: def _normalize_bcrypt_password(password: str) -> str: