fix(redis): gepoolten async-Redis-Client statt Connect/Close pro Request
Security Audit / Python Dependency Audit (push) Canceled after 0s
Security Audit / Node.js Dependency Audit (push) Canceled after 0s

TOTP-Login und Kiosk-Nonce-Check öffneten/schlossen bisher pro Request eine
neue aioredis-Verbindung. Neuer get_async_redis()-Pool in core/redis.py wird
von beiden Stellen genutzt, sauberer Shutdown im FastAPI-Lifespan.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015Ahyx6D3r7G1EuAc42nezn
This commit is contained in:
2026-09-02 22:29:08 +02:00
co-authored by Claude Sonnet 5
parent 52ecd9e5ce
commit c733ddfe40
5 changed files with 50 additions and 21 deletions
+12 -16
View File
@@ -301,9 +301,8 @@ async def totp_login(
):
"""Zweiter Login-Schritt: partial_token + TOTP-Code → volle Tokens."""
import pyotp
import redis.asyncio as aioredis
from uuid import UUID
from app.core.config import settings
from app.core.redis import get_async_redis
from app.core.security import decode_partial_token
from app.models.user import User
from jwt import PyJWTError as JWTError
@@ -319,22 +318,19 @@ async def totp_login(
if not user.totp_enabled or not user.totp_secret:
raise HTTPException(400, "2FA nicht aktiv")
redis_client = aioredis.from_url(settings.redis_url, decode_responses=True)
try:
# M-5: Lockout-Check vor TOTP-Verifikation
await _check_totp_lockout(user_id, redis_client)
redis_client = get_async_redis()
# M-5: Lockout-Check vor TOTP-Verifikation
await _check_totp_lockout(user_id, redis_client)
plain_secret = _totp_plain(user)
totp = pyotp.TOTP(plain_secret or "")
if not totp.verify(data.code, valid_window=1):
# M-5: Fehlversuch zählen
await _record_totp_failure(user_id, redis_client)
raise HTTPException(400, "Ungültiger Code")
plain_secret = _totp_plain(user)
totp = pyotp.TOTP(plain_secret or "")
if not totp.verify(data.code, valid_window=1):
# M-5: Fehlversuch zählen
await _record_totp_failure(user_id, redis_client)
raise HTTPException(400, "Ungültiger Code")
# M-5: Erfolg → Fehlversuche zurücksetzen
await _clear_totp_failures(user_id, redis_client)
finally:
await redis_client.aclose()
# M-5: Erfolg → Fehlversuche zurücksetzen
await _clear_totp_failures(user_id, redis_client)
from datetime import datetime, timezone
user.last_login = datetime.now(timezone.utc)