22from datetime import datetime , timedelta
33import jwt
44from sqlalchemy .orm import Session
5+ from .models import TOTPAttempt
56
67SECRET_KEY = "change-this-secret-in-prod"
78ALGORITHM = "HS256"
@@ -34,16 +35,17 @@ def check_rate_limit(user_id: int, db: Session) -> tuple[bool, int]:
3435 - is_rate_limited: True if user has exceeded 5 failed attempts in 15 minutes
3536 - minutes_until_reset: Minutes until rate limit resets (0 if not limited)
3637 """
37- from .models import TOTPAttempt
38-
3938 # Calculate time window (15 minutes ago)
4039 time_window = datetime .utcnow () - timedelta (minutes = 15 )
4140
41+ # Refresh session to get latest data
42+ db .expire_all ()
43+
4244 # Query failed attempts within the time window
4345 failed_attempts = db .query (TOTPAttempt ).filter (
4446 TOTPAttempt .user_id == user_id ,
4547 TOTPAttempt .attempted_at > time_window ,
46- TOTPAttempt .success is False
48+ TOTPAttempt .success == False # pylint: disable=singleton-comparison
4749 ).order_by (TOTPAttempt .attempted_at .asc ()).all ()
4850
4951 # Check if rate limit exceeded
@@ -69,8 +71,6 @@ def record_totp_attempt(user_id: int, success: bool, db: Session) -> None:
6971 success: Whether the verification was successful
7072 db: Database session
7173 """
72- from .models import TOTPAttempt
73-
7474 attempt = TOTPAttempt (
7575 user_id = user_id ,
7676 success = success ,
0 commit comments