feat: enhance logging for authentication events

This commit is contained in:
Lorenzo Venerandi
2026-03-09 17:54:47 +01:00
parent 40f1051d1f
commit 4442bcc406
2 changed files with 5 additions and 0 deletions

View File

@@ -258,6 +258,9 @@ def override_config_from_env(config: Config = None):
try: try:
field_type = config.__dataclass_fields__[field].type field_type = config.__dataclass_fields__[field].type
env_value = os.environ[env_var] env_value = os.environ[env_var]
# If password is overridden, it's no longer auto-generated
if field == "dashboard_password":
config.dashboard_password_generated = False
if field_type == int: if field_type == int:
setattr(config, field, int(env_value)) setattr(config, field, int(env_value))
elif field_type == float: elif field_type == float:

View File

@@ -73,6 +73,7 @@ async def authenticate(request: Request, body: AuthRequest):
if hmac.compare_digest(body.fingerprint, expected): if hmac.compare_digest(body.fingerprint, expected):
# Success — clear failed attempts # Success — clear failed attempts
_auth_attempts.pop(ip, None) _auth_attempts.pop(ip, None)
get_app_logger().info(f"[AUTH] Successful login from {ip}")
token = secrets.token_hex(32) token = secrets.token_hex(32)
_auth_tokens.add(token) _auth_tokens.add(token)
response = JSONResponse(content={"authenticated": True}) response = JSONResponse(content={"authenticated": True})
@@ -85,6 +86,7 @@ async def authenticate(request: Request, body: AuthRequest):
return response return response
# Failed attempt — track and possibly lock out # Failed attempt — track and possibly lock out
get_app_logger().warning(f"[AUTH] Failed login attempt from {ip}")
if not record: if not record:
record = {"attempts": 0, "locked_until": 0, "lockouts": 0} record = {"attempts": 0, "locked_until": 0, "lockouts": 0}
_auth_attempts[ip] = record _auth_attempts[ip] = record