feat(reception): add strict anti-capture expiration to dynamic QR and fix Buenos Aires timezone display (v3.8.49)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from datetime import datetime, timezone
|
||||
from typing import Optional, List
|
||||
from datetime import datetime, timezone, timedelta
|
||||
from typing import Optional, List, Tuple
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||||
|
||||
from app import db
|
||||
@@ -20,6 +20,25 @@ from schemas.reception_dto import (
|
||||
router = APIRouter(prefix="/api/v1/reception", tags=["Reception & Access Control"])
|
||||
|
||||
|
||||
def to_argentina_time(dt_val: Optional[datetime]) -> Tuple[str, str]:
|
||||
"""Convierte un datetime (UTC) a la hora local institucional de Buenos Aires (UTC-3)."""
|
||||
if not dt_val:
|
||||
return "", "--"
|
||||
try:
|
||||
from zoneinfo import ZoneInfo
|
||||
tz_ar = ZoneInfo("America/Argentina/Buenos_Aires")
|
||||
if dt_val.tzinfo is None:
|
||||
dt_utc = dt_val.replace(tzinfo=timezone.utc)
|
||||
else:
|
||||
dt_utc = dt_val.astimezone(timezone.utc)
|
||||
local_dt = dt_utc.astimezone(tz_ar)
|
||||
except Exception:
|
||||
offset = timedelta(hours=-3)
|
||||
local_dt = (dt_val if dt_val.tzinfo is None else dt_val.replace(tzinfo=None)) + offset
|
||||
|
||||
return local_dt.isoformat(), local_dt.strftime("%H:%M hs")
|
||||
|
||||
|
||||
@router.post("/verify-qr", response_model=QRVerificationResponseDTO)
|
||||
def verify_qr_and_register_access(
|
||||
payload: QRVerificationRequestDTO,
|
||||
@@ -68,6 +87,8 @@ def verify_qr_and_register_access(
|
||||
else f"Acceso observado: {reason}"
|
||||
)
|
||||
|
||||
scanned_at_iso, scanned_time_str = to_argentina_time(now_utc)
|
||||
|
||||
return QRVerificationResponseDTO(
|
||||
status=auth_status,
|
||||
message=message,
|
||||
@@ -78,7 +99,8 @@ def verify_qr_and_register_access(
|
||||
matricula_status="REGULAR / ACTIVO" if user.is_active else "SUSPENDIDO / INACTIVO",
|
||||
photo_url=None,
|
||||
todays_classes=classes,
|
||||
scanned_at=now_utc.isoformat()
|
||||
scanned_at=scanned_at_iso,
|
||||
scanned_time=scanned_time_str
|
||||
)
|
||||
|
||||
# Si el código QR es inválido o el usuario no existe
|
||||
@@ -90,6 +112,8 @@ def verify_qr_and_register_access(
|
||||
details={"reason": reason, "raw_snippet": payload.qr_token[:15]}
|
||||
)
|
||||
|
||||
scanned_at_iso, scanned_time_str = to_argentina_time(now_utc)
|
||||
|
||||
return QRVerificationResponseDTO(
|
||||
status="DENEGADO",
|
||||
message=reason,
|
||||
@@ -100,7 +124,8 @@ def verify_qr_and_register_access(
|
||||
matricula_status="NO REGISTRADO",
|
||||
photo_url=None,
|
||||
todays_classes=[],
|
||||
scanned_at=now_utc.isoformat()
|
||||
scanned_at=scanned_at_iso,
|
||||
scanned_time=scanned_time_str
|
||||
)
|
||||
|
||||
|
||||
@@ -112,7 +137,7 @@ def get_recent_access_logs(
|
||||
):
|
||||
"""
|
||||
Retorna los últimos ingresos registrados en el punto de acceso.
|
||||
Permite monitoreo en tiempo real para el personal de Recepción y Seguridad.
|
||||
Permite monitoreo en tiempo real para el personal de Recepción y Seguridad con hora local de Buenos Aires.
|
||||
"""
|
||||
query = AccessLogQR.query
|
||||
if building_id:
|
||||
@@ -122,6 +147,7 @@ def get_recent_access_logs(
|
||||
|
||||
items = []
|
||||
for l in logs:
|
||||
scanned_at_iso, scanned_time_str = to_argentina_time(l.scanned_at)
|
||||
items.append(AccessLogResponseDTO(
|
||||
id=l.id,
|
||||
user_name=l.user.name if l.user else "Usuario no disponible",
|
||||
@@ -129,7 +155,8 @@ def get_recent_access_logs(
|
||||
user_role=l.user.role if l.user else None,
|
||||
building_name=l.building.name if l.building else "Sede no especificada",
|
||||
scanned_by_name=l.scanned_by.name if l.scanned_by else "Recepción",
|
||||
scanned_at=l.scanned_at.isoformat(),
|
||||
scanned_at=scanned_at_iso,
|
||||
scanned_time=scanned_time_str,
|
||||
status=l.status,
|
||||
reason=l.reason
|
||||
))
|
||||
@@ -147,10 +174,16 @@ def get_my_credential_qr_token(
|
||||
"""
|
||||
Genera y entrega el token criptográfico del código QR para la credencial digital
|
||||
del usuario autenticado (estudiante o docente).
|
||||
Soporta rotación dinámica periódica (default 3s) configurada en SystemSetting.
|
||||
Soporta rotación dinámica periódica (default 3s) y ventana de validez anti-captura estricta (default 6s).
|
||||
"""
|
||||
renewal_seconds = int(SystemSetting.get_value('qr_renewal_interval_seconds', 3) or 3)
|
||||
grace_seconds = int(SystemSetting.get_value('qr_token_grace_period_seconds', 60) or 60)
|
||||
# Ventana estricta anti-captura: el token expira pasados pocos segundos (default 6s)
|
||||
# de modo que una captura de pantalla de 10 segundos queda garantizadamente rechazada.
|
||||
configured_grace = SystemSetting.get_value('qr_token_grace_period_seconds', None)
|
||||
if configured_grace is not None:
|
||||
grace_seconds = int(configured_grace)
|
||||
else:
|
||||
grace_seconds = max(renewal_seconds + 3, 6)
|
||||
|
||||
token = QRSecurityService.generate_qr_token(current_user, valid_seconds=grace_seconds)
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user