fix(auth, db): prevent hash exceptions from triggering lockout, auto-create simulation_jobs and route file_uploads on core engine

This commit is contained in:
adithiyan 2026-10-28 15:06:43 +05:30
parent f74b0807a7
commit eb4270b693
2 changed files with 29 additions and 11 deletions

View file

@ -70,7 +70,14 @@ def login(
record_failed_login_audit(db, request, data.email, user, "Account locked") record_failed_login_audit(db, request, data.email, user, "Account locked")
raise HTTPException(status_code=403, detail="Account locked. Please reset password to unlock.") raise HTTPException(status_code=403, detail="Account locked. Please reset password to unlock.")
if not verify_password(data.password, user.password_hash): try:
password_valid = verify_password(data.password, user.password_hash)
except Exception:
# Hash verification threw an internal error (e.g. corrupted hash, passlib version mismatch).
# This is a server-side issue — do NOT penalise the user's login attempts.
raise HTTPException(status_code=500, detail="Authentication service error. Please contact support.")
if not password_valid:
user.failed_login_attempts += 1 user.failed_login_attempts += 1
is_locked_now = user.failed_login_attempts >= settings.MAX_FAILED_LOGIN is_locked_now = user.failed_login_attempts >= settings.MAX_FAILED_LOGIN
if is_locked_now: if is_locked_now:
@ -255,7 +262,12 @@ def oauth2_token(
record_failed_login_audit(db, request, form_data.username, user, "Account locked") record_failed_login_audit(db, request, form_data.username, user, "Account locked")
raise HTTPException(status_code=403, detail="Account locked. Please reset password to unlock.") raise HTTPException(status_code=403, detail="Account locked. Please reset password to unlock.")
if not verify_password(form_data.password, user.password_hash): try:
password_valid = verify_password(form_data.password, user.password_hash)
except Exception:
raise HTTPException(status_code=500, detail="Authentication service error. Please contact support.")
if not password_valid:
user.failed_login_attempts += 1 user.failed_login_attempts += 1
is_locked_now = user.failed_login_attempts >= settings.MAX_FAILED_LOGIN is_locked_now = user.failed_login_attempts >= settings.MAX_FAILED_LOGIN
if is_locked_now: if is_locked_now:

View file

@ -12,6 +12,7 @@ import app.models.db_base
# Ensure new CMS models are imported so their tables are included in metadata # Ensure new CMS models are imported so their tables are included in metadata
import app.models.StorefrontCmsModel # noqa: F401 import app.models.StorefrontCmsModel # noqa: F401
import app.models.CustomerFeedbackModel # noqa: F401 import app.models.CustomerFeedbackModel # noqa: F401
import swarm.models.simulation_job # noqa: F401
from app.models.RoleModel import Role from app.models.RoleModel import Role
from app.models.DepartmentModel import Department from app.models.DepartmentModel import Department
from app.models.DesignationModel import Designation from app.models.DesignationModel import Designation
@ -72,7 +73,8 @@ def initialize_database():
"departments", "designations", "roles", "permissions", "departments", "designations", "roles", "permissions",
"users", "user_sessions", "audit_logs", "users", "user_sessions", "audit_logs",
"countries", "states", "cities", "settings", "file_uploads", "countries", "states", "cities", "settings", "file_uploads",
"role_permissions", "notification_templates", "user_location_mappings" "role_permissions", "notification_templates", "user_location_mappings",
"simulation_jobs"
] ]
core_metadata_tables = [t for t in Base.metadata.sorted_tables if t.name in core_tables] core_metadata_tables = [t for t in Base.metadata.sorted_tables if t.name in core_tables]
@ -415,16 +417,20 @@ def initialize_database():
except Exception as e: except Exception as e:
print(f"device_type_registry column alter note: {e}") print(f"device_type_registry column alter note: {e}")
# file_uploads table auto-migration (blur_hash column) # file_uploads table auto-migration (blur_hash column on engine_core)
try: try:
conn.execute(text("SELECT blur_hash FROM file_uploads LIMIT 1")) with engine_core.connect() as core_conn:
try:
core_conn.execute(text("SELECT blur_hash FROM file_uploads LIMIT 1"))
except Exception:
print("Auto-Migration: Adding missing 'blur_hash' column to 'file_uploads' table...")
try:
core_conn.execute(text("ALTER TABLE file_uploads ADD COLUMN blur_hash VARCHAR(255) NULL"))
core_conn.commit()
except Exception as e:
print(f"file_uploads blur_hash alter note: {e}")
except Exception: except Exception:
print("Auto-Migration: Adding missing 'blur_hash' column to 'file_uploads' table...") pass
try:
conn.execute(text("ALTER TABLE file_uploads ADD COLUMN blur_hash VARCHAR(255) NULL"))
conn.commit()
except Exception as e:
print(f"file_uploads blur_hash alter note: {e}")
print("All tables compiled and created successfully in their respective databases.") print("All tables compiled and created successfully in their respective databases.")