[fix] Corrections ruff CI (UP017/UP041/UP012/I001/F401/FURB162) : backend/auth/user_store.py
This commit is contained in:
@@ -8,7 +8,7 @@ import logging
|
|||||||
import shutil
|
import shutil
|
||||||
import threading
|
import threading
|
||||||
import uuid
|
import uuid
|
||||||
from datetime import datetime, timedelta, timezone
|
from datetime import UTC, datetime, timedelta
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from .password import hash_password
|
from .password import hash_password
|
||||||
@@ -97,8 +97,8 @@ def create_user(
|
|||||||
"active": True,
|
"active": True,
|
||||||
"language": "fr", # default UI language
|
"language": "fr", # default UI language
|
||||||
"avatar": None, # profile picture data-URL (#113)
|
"avatar": None, # profile picture data-URL (#113)
|
||||||
"created_at": datetime.now(timezone.utc).isoformat(),
|
"created_at": datetime.now(UTC).isoformat(),
|
||||||
"password_changed_at": datetime.now(timezone.utc).timestamp(),
|
"password_changed_at": datetime.now(UTC).timestamp(),
|
||||||
"last_login": None,
|
"last_login": None,
|
||||||
"failed_attempts": 0,
|
"failed_attempts": 0,
|
||||||
"locked_until": None,
|
"locked_until": None,
|
||||||
@@ -126,7 +126,7 @@ def update_user(username: str, updates: dict) -> dict:
|
|||||||
if "password" in safe_updates:
|
if "password" in safe_updates:
|
||||||
safe_updates["password_hash"] = hash_password(safe_updates.pop("password"))
|
safe_updates["password_hash"] = hash_password(safe_updates.pop("password"))
|
||||||
# BUG-028: invalidate every token issued before this change.
|
# BUG-028: invalidate every token issued before this change.
|
||||||
safe_updates["password_changed_at"] = datetime.now(timezone.utc).timestamp()
|
safe_updates["password_changed_at"] = datetime.now(UTC).timestamp()
|
||||||
|
|
||||||
data["users"][username].update(safe_updates)
|
data["users"][username].update(safe_updates)
|
||||||
_write(data)
|
_write(data)
|
||||||
@@ -148,7 +148,7 @@ def delete_user(username: str):
|
|||||||
def record_login_success(username: str):
|
def record_login_success(username: str):
|
||||||
"""Record a successful login: update last_login, reset failed attempts."""
|
"""Record a successful login: update last_login, reset failed attempts."""
|
||||||
update_user(username, {
|
update_user(username, {
|
||||||
"last_login": datetime.now(timezone.utc).isoformat(),
|
"last_login": datetime.now(UTC).isoformat(),
|
||||||
"failed_attempts": 0,
|
"failed_attempts": 0,
|
||||||
"locked_until": None,
|
"locked_until": None,
|
||||||
})
|
})
|
||||||
@@ -171,7 +171,7 @@ def record_login_failure(username: str) -> int:
|
|||||||
# Lock after 5 failed attempts (15 minutes)
|
# Lock after 5 failed attempts (15 minutes)
|
||||||
if attempts >= 5:
|
if attempts >= 5:
|
||||||
locked_until = (
|
locked_until = (
|
||||||
datetime.now(timezone.utc) + timedelta(minutes=15)
|
datetime.now(UTC) + timedelta(minutes=15)
|
||||||
).isoformat()
|
).isoformat()
|
||||||
updates["locked_until"] = locked_until
|
updates["locked_until"] = locked_until
|
||||||
logger.warning(f"Account '{username}' locked after {attempts} failed attempts")
|
logger.warning(f"Account '{username}' locked after {attempts} failed attempts")
|
||||||
@@ -189,7 +189,7 @@ def is_locked(username: str) -> bool:
|
|||||||
if not user or not user.get("locked_until"):
|
if not user or not user.get("locked_until"):
|
||||||
return False
|
return False
|
||||||
locked_until = datetime.fromisoformat(user["locked_until"])
|
locked_until = datetime.fromisoformat(user["locked_until"])
|
||||||
if datetime.now(timezone.utc) > locked_until:
|
if datetime.now(UTC) > locked_until:
|
||||||
# Lock expired — unlock
|
# Lock expired — unlock
|
||||||
update_user(username, {"locked_until": None, "failed_attempts": 0})
|
update_user(username, {"locked_until": None, "failed_attempts": 0})
|
||||||
return False
|
return False
|
||||||
|
|||||||
Reference in New Issue
Block a user