Jelajahi Sumber

fix: update datetime handling to use timezone.utc for consistency in AuthService

Manuel 11 bulan lalu
induk
melakukan
5eb0d7abed
2 mengubah file dengan 7 tambahan dan 6 penghapusan
  1. 1 0
      pyproject.toml
  2. 6 6
      services/auth_service.py

+ 1 - 0
pyproject.toml

@@ -47,6 +47,7 @@ ignore = [
     "B008",  # Do not perform function calls in argument defaults
     "B904",  # Within an except clause, raise exceptions with raise ... from err
     "C901",  # too complex
+    "UP017", # Use `datetime.UTC` alias (not available in Python 3.10)
 ]
 
 # Permitir arreglos automáticos

+ 6 - 6
services/auth_service.py

@@ -7,7 +7,7 @@ Follows security best practices for token-based authentication.
 import hmac
 import os
 import secrets
-from datetime import UTC, datetime, timedelta
+from datetime import datetime, timedelta, timezone
 from functools import wraps
 
 import bcrypt
@@ -82,7 +82,7 @@ class AuthService:
         Check if the user is rate limited.
         Returns tuple of (is_allowed, remaining_attempts).
         """
-        now = datetime.now(UTC)
+        now = datetime.now(timezone.utc)
 
         if identifier in cls._login_attempts:
             attempts, lockout_until = cls._login_attempts[identifier]
@@ -105,7 +105,7 @@ class AuthService:
     @classmethod
     def record_failed_attempt(cls, identifier: str) -> None:
         """Record a failed login attempt."""
-        now = datetime.now(UTC)
+        now = datetime.now(timezone.utc)
 
         if identifier in cls._login_attempts:
             attempts, _ = cls._login_attempts[identifier]
@@ -151,14 +151,14 @@ class AuthService:
 
             if password_match:
                 # Update last login
-                user.last_login = datetime.now(UTC)
+                user.last_login = datetime.now(timezone.utc)
                 session.commit()
 
                 return {
                     "id": user.id,
                     "username": user.username,
                     "role": user.role,
-                    "authenticated_at": datetime.now(UTC).isoformat(),
+                    "authenticated_at": datetime.now(timezone.utc).isoformat(),
                 }
 
             return None
@@ -175,7 +175,7 @@ class AuthService:
         Generate a JWT token for authenticated user.
         Includes security claims like expiration and issued-at times.
         """
-        now = datetime.now(UTC)
+        now = datetime.now(timezone.utc)
 
         payload = {
             # Standard JWT claims