feat(security): add biometric (face id / fingerprint) and pin unlock protection for payment qr
This commit is contained in:
@@ -6337,3 +6337,32 @@ body.dark-mode .admin-dash-bar-track {
|
|||||||
margin-top: 8px;
|
margin-top: 8px;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ==========================================================================
|
||||||
|
SEGURIDAD BIOMÉTRICA Y DESBLOQUEO DE CARNÉ QR
|
||||||
|
========================================================================== */
|
||||||
|
.switch-toggle input:checked + .switch-slider {
|
||||||
|
background-color: #0284c7 !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.switch-slider:before {
|
||||||
|
position: absolute;
|
||||||
|
content: "";
|
||||||
|
height: 20px;
|
||||||
|
width: 20px;
|
||||||
|
left: 3px;
|
||||||
|
bottom: 3px;
|
||||||
|
background-color: white;
|
||||||
|
border-radius: 50%;
|
||||||
|
transition: transform 0.25s ease;
|
||||||
|
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
|
||||||
|
}
|
||||||
|
|
||||||
|
.switch-toggle input:checked + .switch-slider:before {
|
||||||
|
transform: translateX(22px);
|
||||||
|
}
|
||||||
|
|
||||||
|
#inputQrPin:focus {
|
||||||
|
border-color: #0284c7 !important;
|
||||||
|
box-shadow: 0 0 0 3px rgba(2, 132, 199, 0.25) !important;
|
||||||
|
}
|
||||||
|
|||||||
+132
-1
@@ -2186,11 +2186,15 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div style="display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end;">
|
<div style="display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; align-items: center;">
|
||||||
<button class="qr-toggle-btn" onclick="openQrModal()">
|
<button class="qr-toggle-btn" onclick="openQrModal()">
|
||||||
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect width="18" height="18" x="3" y="3" rx="2"/><path d="M7 7h.01"/><path d="M17 7h.01"/><path d="M7 17h.01"/><path d="M17 17h.01"/></svg>
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect width="18" height="18" x="3" y="3" rx="2"/><path d="M7 7h.01"/><path d="M17 7h.01"/><path d="M7 17h.01"/><path d="M17 17h.01"/></svg>
|
||||||
<span id="qrBtnText">Mi QR</span>
|
<span id="qrBtnText">Mi QR</span>
|
||||||
</button>
|
</button>
|
||||||
|
<button id="btnQrSecurityLock" class="qr-toggle-btn" style="background: rgba(255, 255, 255, 0.15); border-color: rgba(255, 255, 255, 0.3); padding: 7px 9px;" onclick="openQrSecuritySettings()" title="Seguridad y bloqueo biométrico de mi carné QR">
|
||||||
|
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>
|
||||||
|
<span id="lblQrSecurityBadge" style="font-size: 0.68rem; font-weight: 800;">Seguridad</span>
|
||||||
|
</button>
|
||||||
<button id="btnCardTransfer" class="qr-toggle-btn" style="background: rgba(16, 185, 129, 0.25); border-color: rgba(52, 211, 153, 0.4);" onclick="openTransferModal()">
|
<button id="btnCardTransfer" class="qr-toggle-btn" style="background: rgba(16, 185, 129, 0.25); border-color: rgba(52, 211, 153, 0.4);" onclick="openTransferModal()">
|
||||||
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="17 1 21 5 17 9"/><path d="M3 11V9a4 4 0 0 1 4-4h14"/><polyline points="7 23 3 19 7 15"/><path d="M21 13v2a4 4 0 0 1-4 4H3"/></svg>
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="17 1 21 5 17 9"/><path d="M3 11V9a4 4 0 0 1 4-4h14"/><polyline points="7 23 3 19 7 15"/><path d="M21 13v2a4 4 0 0 1-4 4H3"/></svg>
|
||||||
<span id="transferBtnText">Transferir</span>
|
<span id="transferBtnText">Transferir</span>
|
||||||
@@ -2365,6 +2369,117 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- MODAL DE SEGURIDAD / DESBLOQUEO DE CARNÉ QR (BIOMETRÍA / PIN) -->
|
||||||
|
<div class="modal-qr-backdrop" id="modalQrPinPrompt" style="display: none; z-index: 10001;" onclick="cancelQrSecurityPrompt(event)">
|
||||||
|
<div class="modal-qr-card" onclick="event.stopPropagation()" style="max-width: 360px; text-align: center; border-radius: 24px; padding: 26px 20px;">
|
||||||
|
<div style="width: 60px; height: 60px; border-radius: 20px; background: #e0f2fe; color: #0284c7; display: flex; align-items: center; justify-content: center; margin: 0 auto 16px; box-shadow: 0 4px 14px rgba(2, 132, 199, 0.2);">
|
||||||
|
<svg width="30" height="30" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round">
|
||||||
|
<rect width="18" height="11" x="3" y="11" rx="2" ry="2"/>
|
||||||
|
<path d="M7 11V7a5 5 0 0 1 10 0v4"/>
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h3 style="font-size: 1.15rem; font-weight: 900; color: var(--text-main, #0f172a); margin: 0 0 6px 0;">Carné QR Protegido</h3>
|
||||||
|
<p style="font-size: 0.8rem; color: var(--text-muted, #64748b); margin: 0 0 18px 0; line-height: 1.4;">
|
||||||
|
Verifica tu identidad para mostrar tu código de pago en la soda.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<!-- Botón para reintentar biometría (Face ID / Huella) si el dispositivo la soporta -->
|
||||||
|
<div id="qrBioRetryContainer" style="display: none; margin-bottom: 16px;">
|
||||||
|
<button type="button" onclick="retryQrBiometrics()" style="width: 100%; height: 46px; border-radius: 12px; background: #f0f9ff; border: 1.5px solid #0284c7; color: #0284c7; font-weight: 800; font-size: 0.92rem; display: flex; align-items: center; justify-content: center; gap: 8px; cursor: pointer; box-shadow: 0 2px 6px rgba(2, 132, 199, 0.15);">
|
||||||
|
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||||
|
<path d="M12 2a10 10 0 0 0-10 10c0 3.2 1.5 6 3.8 7.8"/>
|
||||||
|
<path d="M12 6a6 6 0 0 0-6 6c0 1.9.9 3.6 2.3 4.7"/>
|
||||||
|
<path d="M12 10a2 2 0 0 0-2 2c0 .6.3 1.2.8 1.6"/>
|
||||||
|
<path d="M12 22a10 10 0 0 0 10-10c0-3.2-1.5-6-3.8-7.8"/>
|
||||||
|
</svg>
|
||||||
|
<span>Usar Huella o Face ID</span>
|
||||||
|
</button>
|
||||||
|
<div style="font-size: 0.72rem; color: #94a3b8; margin-top: 8px;">— o ingresa tu PIN de 4 dígitos —</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Inputs de PIN (4 dígitos) -->
|
||||||
|
<div style="margin-bottom: 18px;">
|
||||||
|
<label for="inputQrPin" style="display: block; font-size: 0.78rem; font-weight: 800; color: var(--text-main, #334155); margin-bottom: 8px;">
|
||||||
|
PIN de Seguridad:
|
||||||
|
</label>
|
||||||
|
<input type="password" inputmode="numeric" maxlength="4" id="inputQrPin" placeholder="••••" autocomplete="off" onkeyup="handleQrPinInput(event)" style="width: 150px; height: 50px; text-align: center; font-size: 1.8rem; font-weight: 900; letter-spacing: 12px; border-radius: 12px; border: 2px solid #cbd5e1; outline: none; margin: 0 auto; display: block; box-sizing: border-box; color: #0f172a; background: white;">
|
||||||
|
<div id="msgQrPinError" style="display: none; color: #dc2626; font-size: 0.78rem; font-weight: 800; margin-top: 8px;">
|
||||||
|
PIN incorrecto. Intenta de nuevo.
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Botones de Acción -->
|
||||||
|
<div style="display: flex; gap: 8px;">
|
||||||
|
<button type="button" onclick="cancelQrSecurityPrompt()" style="flex: 1; height: 44px; border-radius: 12px; background: #f1f5f9; border: none; color: #475569; font-weight: 800; font-size: 0.9rem; cursor: pointer;">
|
||||||
|
Cancelar
|
||||||
|
</button>
|
||||||
|
<button type="button" onclick="submitQrSecurityPin()" style="flex: 1; height: 44px; border-radius: 12px; background: linear-gradient(135deg, #0284c7, #0369a1); border: none; color: white; font-weight: 800; font-size: 0.9rem; cursor: pointer; box-shadow: 0 4px 12px rgba(2, 132, 199, 0.35);">
|
||||||
|
Desbloquear
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- MODAL DE AJUSTES DE SEGURIDAD BIOMÉTRICA DEL CARNÉ QR -->
|
||||||
|
<div class="modal-qr-backdrop" id="modalQrSecuritySettings" style="display: none; z-index: 10000;" onclick="closeQrSecuritySettings(event)">
|
||||||
|
<div class="modal-qr-card" onclick="event.stopPropagation()" style="max-width: 420px; text-align: left; border-radius: 22px; padding: 22px;">
|
||||||
|
<div style="display: flex; justify-content: space-between; align-items: center; border-bottom: 1.5px solid var(--border, #e2e8f0); padding-bottom: 12px; margin-bottom: 16px;">
|
||||||
|
<div style="display: flex; align-items: center; gap: 10px;">
|
||||||
|
<div style="width: 38px; height: 38px; border-radius: 10px; background: #e0f2fe; color: #0284c7; display: flex; align-items: center; justify-content: center; flex-shrink: 0;">
|
||||||
|
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round">
|
||||||
|
<path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/>
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<h3 style="margin: 0; font-size: 1.05rem; font-weight: 800; color: var(--text-main, #0f172a);">Seguridad del Carné QR</h3>
|
||||||
|
<span style="font-size: 0.72rem; color: var(--text-muted, #64748b);">Protege tu código de pago en este teléfono</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<button type="button" onclick="closeQrSecuritySettings()" style="background: none; border: none; font-size: 1.2rem; cursor: pointer; color: #64748b;">✕</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Switch de Activación -->
|
||||||
|
<div style="background: #f8fafc; border: 1.5px solid #e2e8f0; border-radius: 14px; padding: 14px; margin-bottom: 14px; display: flex; justify-content: space-between; align-items: center;">
|
||||||
|
<div>
|
||||||
|
<strong style="font-size: 0.88rem; color: #0f172a; display: block;">Bloqueo con Huella / Face ID</strong>
|
||||||
|
<span style="font-size: 0.73rem; color: #64748b; line-height: 1.35; display: block; margin-top: 2px;">
|
||||||
|
Pide autenticación biométrica cada vez que quieras abrir el código QR de pago.
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<label class="switch-toggle" style="position: relative; display: inline-block; width: 48px; height: 26px; flex-shrink: 0; margin-left: 10px;">
|
||||||
|
<input type="checkbox" id="chkQrBiometricsEnabled" onchange="toggleQrBiometricSetting(this.checked)" style="opacity: 0; width: 0; height: 0;">
|
||||||
|
<span class="switch-slider" style="position: absolute; cursor: pointer; inset: 0; background-color: #cbd5e1; border-radius: 34px; transition: .3s;"></span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Estado de compatibilidad del dispositivo -->
|
||||||
|
<div id="qrBioDeviceStatus" style="font-size: 0.76rem; padding: 8px 12px; border-radius: 10px; margin-bottom: 14px; display: flex; align-items: center; gap: 8px; font-weight: 700;">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- PIN de Respaldo -->
|
||||||
|
<div style="background: #ffffff; border: 1.5px solid #cbd5e1; border-radius: 14px; padding: 14px; margin-bottom: 16px;">
|
||||||
|
<strong style="font-size: 0.84rem; color: #1e293b; display: block; margin-bottom: 4px;">PIN de Respaldo (4 dígitos)</strong>
|
||||||
|
<span style="font-size: 0.72rem; color: #64748b; display: block; margin-bottom: 10px;">
|
||||||
|
Se utilizará si el sensor biométrico no te reconoce o si el dispositivo no tiene lector.
|
||||||
|
</span>
|
||||||
|
<div style="display: flex; gap: 8px;">
|
||||||
|
<input type="password" inputmode="numeric" maxlength="4" id="inputSettingsPinBackup" placeholder="1234" style="flex: 1; padding: 8px 12px; border-radius: 8px; border: 1.5px solid #94a3b8; font-size: 1.1rem; font-weight: 900; letter-spacing: 4px; text-align: center; box-sizing: border-box;">
|
||||||
|
<button type="button" onclick="saveSettingsPinBackup()" style="padding: 8px 16px; border-radius: 8px; background: #0284c7; color: white; border: none; font-weight: 800; font-size: 0.85rem; cursor: pointer; white-space: nowrap;">
|
||||||
|
Guardar PIN
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div id="msgSettingsPinSaved" style="display: none; color: #16a34a; font-size: 0.74rem; font-weight: 800; margin-top: 6px;">
|
||||||
|
✅ PIN guardado con éxito
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button type="button" onclick="closeQrSecuritySettings()" style="width: 100%; height: 44px; border-radius: 12px; background: #f1f5f9; border: none; font-weight: 800; font-size: 0.9rem; color: #475569; cursor: pointer;">
|
||||||
|
Listo
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- MODAL: CARRITO Y PRE-ORDEN -->
|
<!-- MODAL: CARRITO Y PRE-ORDEN -->
|
||||||
<div class="modal-qr-backdrop" id="modalCart" style="display: none;" onclick="closeCartModal(event)">
|
<div class="modal-qr-backdrop" id="modalCart" style="display: none;" onclick="closeCartModal(event)">
|
||||||
<div class="modal-qr-card" onclick="event.stopPropagation()" style="max-width: 420px; text-align: left;">
|
<div class="modal-qr-card" onclick="event.stopPropagation()" style="max-width: 420px; text-align: left;">
|
||||||
@@ -2513,6 +2628,22 @@
|
|||||||
<input type="checkbox" id="chkParentAllowTransfer" onchange="onToggleAllowTransfer(this.checked)" style="width: 22px; height: 22px; cursor: pointer; accent-color: #0284c7;" checked>
|
<input type="checkbox" id="chkParentAllowTransfer" onchange="onToggleAllowTransfer(this.checked)" style="width: 22px; height: 22px; cursor: pointer; accent-color: #0284c7;" checked>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- CONTROL PARENTAL: BLOQUEO BIOMÉTRICO / PIN DEL CARNÉ QR -->
|
||||||
|
<div style="background: #f0fdf4; border: 1.5px solid #86efac; border-radius: 12px; padding: 12px 14px; margin-bottom: 16px; display: flex; justify-content: space-between; align-items: center;">
|
||||||
|
<div style="display: flex; align-items: center; gap: 10px;">
|
||||||
|
<div style="width: 32px; height: 32px; border-radius: 8px; background: #dcfce7; color: #16a34a; display: flex; align-items: center; justify-content: center; flex-shrink: 0;">
|
||||||
|
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<strong style="font-size: 0.85rem; color: #166534; display: block;">Bloqueo Huella / Face ID / PIN</strong>
|
||||||
|
<span style="font-size: 0.72rem; color: #475569;">Exigir autenticación antes de mostrar el carné QR</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<button type="button" onclick="openQrSecuritySettings()" style="background: #16a34a; color: white; border: none; padding: 7px 14px; border-radius: 8px; font-weight: 800; font-size: 0.78rem; cursor: pointer; white-space: nowrap; box-shadow: 0 2px 6px rgba(22, 163, 74, 0.25);">
|
||||||
|
Configurar
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- RESTABLECER PIN Y CONTRASEÑA ESCOLAR DEL ALUMNO -->
|
<!-- RESTABLECER PIN Y CONTRASEÑA ESCOLAR DEL ALUMNO -->
|
||||||
<div style="background: #f8fafc; border: 1.5px dashed #cbd5e1; border-radius: 14px; padding: 14px; margin-bottom: 16px;">
|
<div style="background: #f8fafc; border: 1.5px dashed #cbd5e1; border-radius: 14px; padding: 14px; margin-bottom: 16px;">
|
||||||
<div style="display: flex; align-items: center; gap: 8px; margin-bottom: 8px;">
|
<div style="display: flex; align-items: center; gap: 8px; margin-bottom: 8px;">
|
||||||
|
|||||||
+334
-1
@@ -888,6 +888,7 @@ function updateStudentUI() {
|
|||||||
// QR Modal info
|
// QR Modal info
|
||||||
document.getElementById('qrDisplayImg').src = `/api/qr-image/${encodeURIComponent(currentStudent.qr_token)}`;
|
document.getElementById('qrDisplayImg').src = `/api/qr-image/${encodeURIComponent(currentStudent.qr_token)}`;
|
||||||
document.getElementById('qrTokenDisplay').textContent = currentStudent.qr_token;
|
document.getElementById('qrTokenDisplay').textContent = currentStudent.qr_token;
|
||||||
|
updateQrSecurityBadgeUI();
|
||||||
|
|
||||||
// Panel de Padres sliders y valores
|
// Panel de Padres sliders y valores
|
||||||
document.getElementById('lblParentDailyLimit').textContent = `₡${currentStudent.limite_diario_colones.toLocaleString('es-CR')}`;
|
document.getElementById('lblParentDailyLimit').textContent = `₡${currentStudent.limite_diario_colones.toLocaleString('es-CR')}`;
|
||||||
@@ -1525,7 +1526,7 @@ async function submitPreOrder() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ==========================================
|
// ==========================================
|
||||||
// MODAL QR ESCOLAR
|
// MODAL QR ESCOLAR Y SEGURIDAD BIOMÉTRICA / PIN
|
||||||
// ==========================================
|
// ==========================================
|
||||||
|
|
||||||
function openQrModal() {
|
function openQrModal() {
|
||||||
@@ -1554,10 +1555,342 @@ function openQrModal() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Si la protección biométrica o PIN está activa, autenticar primero
|
||||||
|
if (isQrSecurityActiveForCurrentStudent()) {
|
||||||
|
triggerQrSecurityUnlock(() => {
|
||||||
|
showQrModalActual();
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
showQrModalActual();
|
||||||
|
}
|
||||||
|
|
||||||
|
function showQrModalActual() {
|
||||||
if (window.sounds) window.sounds.playScanChirp();
|
if (window.sounds) window.sounds.playScanChirp();
|
||||||
document.getElementById('modalQr').style.display = 'flex';
|
document.getElementById('modalQr').style.display = 'flex';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------------
|
||||||
|
// MÓDULO DE AUTENTICACIÓN BIOMÉTRICA (FACE ID / HUELLA / PIN)
|
||||||
|
// --------------------------------------------------------------------------
|
||||||
|
let pendingQrUnlockSuccessCallback = null;
|
||||||
|
|
||||||
|
function getQrSecurityKey() {
|
||||||
|
const estId = currentStudent ? (currentStudent.id || currentStudent.codigo_estudiante || 'default') : 'default';
|
||||||
|
return 'sibopay_qr_security_' + estId;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isQrSecurityActiveForCurrentStudent() {
|
||||||
|
const key = getQrSecurityKey();
|
||||||
|
return localStorage.getItem(key) === 'true';
|
||||||
|
}
|
||||||
|
|
||||||
|
function getQrSecurityPin() {
|
||||||
|
const estId = currentStudent ? (currentStudent.id || currentStudent.codigo_estudiante || 'default') : 'default';
|
||||||
|
const customPin = localStorage.getItem('sibopay_qr_pin_' + estId);
|
||||||
|
if (customPin) return customPin;
|
||||||
|
if (currentStudent && currentStudent.pin_seguridad) return String(currentStudent.pin_seguridad);
|
||||||
|
return '1234';
|
||||||
|
}
|
||||||
|
|
||||||
|
async function checkDeviceBiometricsSupport() {
|
||||||
|
if (window.PublicKeyCredential && typeof PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable === 'function') {
|
||||||
|
try {
|
||||||
|
const isAvailable = await PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable();
|
||||||
|
return Boolean(isAvailable);
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function registerPlatformBiometrics(userIdent) {
|
||||||
|
if (!window.PublicKeyCredential) {
|
||||||
|
throw new Error('Biometría no soportada en este entorno');
|
||||||
|
}
|
||||||
|
const challenge = new Uint8Array(32);
|
||||||
|
window.crypto.getRandomValues(challenge);
|
||||||
|
const userId = new Uint8Array(16);
|
||||||
|
window.crypto.getRandomValues(userId);
|
||||||
|
|
||||||
|
const createOptions = {
|
||||||
|
publicKey: {
|
||||||
|
challenge,
|
||||||
|
rp: {
|
||||||
|
name: 'SiboPay Carné Digital',
|
||||||
|
id: window.location.hostname
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
id: userId,
|
||||||
|
name: userIdent || 'estudiante',
|
||||||
|
displayName: userIdent || 'Estudiante SiboPay'
|
||||||
|
},
|
||||||
|
pubKeyCredParams: [
|
||||||
|
{ alg: -7, type: 'public-key' },
|
||||||
|
{ alg: -257, type: 'public-key' }
|
||||||
|
],
|
||||||
|
authenticatorSelection: {
|
||||||
|
authenticatorAttachment: 'platform',
|
||||||
|
userVerification: 'required'
|
||||||
|
},
|
||||||
|
timeout: 60000
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const cred = await navigator.credentials.create(createOptions);
|
||||||
|
if (!cred) throw new Error('No se generó credencial biométrica');
|
||||||
|
|
||||||
|
const rawId = Array.from(new Uint8Array(cred.rawId)).map(b => String.fromCharCode(b)).join('');
|
||||||
|
return btoa(rawId);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function verifyPlatformBiometrics(credIdBase64) {
|
||||||
|
if (!window.PublicKeyCredential) {
|
||||||
|
throw new Error('Biometría no disponible');
|
||||||
|
}
|
||||||
|
const challenge = new Uint8Array(32);
|
||||||
|
window.crypto.getRandomValues(challenge);
|
||||||
|
|
||||||
|
const getOptions = {
|
||||||
|
publicKey: {
|
||||||
|
challenge,
|
||||||
|
timeout: 60000,
|
||||||
|
userVerification: 'required',
|
||||||
|
rpId: window.location.hostname
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (credIdBase64) {
|
||||||
|
try {
|
||||||
|
const rawIdStr = atob(credIdBase64);
|
||||||
|
const rawIdArr = new Uint8Array(rawIdStr.length);
|
||||||
|
for (let i = 0; i < rawIdStr.length; i++) {
|
||||||
|
rawIdArr[i] = rawIdStr.charCodeAt(i);
|
||||||
|
}
|
||||||
|
getOptions.publicKey.allowCredentials = [{
|
||||||
|
type: 'public-key',
|
||||||
|
id: rawIdArr
|
||||||
|
}];
|
||||||
|
} catch (err) {
|
||||||
|
console.warn('Error parseando credencial biométrica previa:', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const assertion = await navigator.credentials.get(getOptions);
|
||||||
|
return Boolean(assertion);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function triggerQrSecurityUnlock(onSuccess) {
|
||||||
|
pendingQrUnlockSuccessCallback = onSuccess;
|
||||||
|
const estId = currentStudent ? (currentStudent.id || currentStudent.codigo_estudiante || 'default') : 'default';
|
||||||
|
const credId = localStorage.getItem('sibopay_bio_cred_' + estId);
|
||||||
|
|
||||||
|
const bioAvailable = await checkDeviceBiometricsSupport();
|
||||||
|
const retryContainer = document.getElementById('qrBioRetryContainer');
|
||||||
|
if (retryContainer) {
|
||||||
|
retryContainer.style.display = bioAvailable ? 'block' : 'none';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Intentar inmediatamente autenticación biométrica nativa si está disponible
|
||||||
|
if (bioAvailable && credId) {
|
||||||
|
try {
|
||||||
|
const verified = await verifyPlatformBiometrics(credId);
|
||||||
|
if (verified) {
|
||||||
|
if (window.sounds) window.sounds.playSuccess();
|
||||||
|
if (typeof pendingQrUnlockSuccessCallback === 'function') {
|
||||||
|
pendingQrUnlockSuccessCallback();
|
||||||
|
pendingQrUnlockSuccessCallback = null;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (bioErr) {
|
||||||
|
console.warn('Autenticación biométrica no completada o cancelada, solicitando PIN:', bioErr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si no hay biometría o falló/canceló, abrir modal de PIN
|
||||||
|
showQrPinPromptModal();
|
||||||
|
}
|
||||||
|
|
||||||
|
function showQrPinPromptModal() {
|
||||||
|
const modal = document.getElementById('modalQrPinPrompt');
|
||||||
|
if (!modal) return;
|
||||||
|
const pinInput = document.getElementById('inputQrPin');
|
||||||
|
if (pinInput) pinInput.value = '';
|
||||||
|
const err = document.getElementById('msgQrPinError');
|
||||||
|
if (err) err.style.display = 'none';
|
||||||
|
|
||||||
|
modal.style.display = 'flex';
|
||||||
|
setTimeout(() => {
|
||||||
|
if (pinInput) pinInput.focus();
|
||||||
|
}, 150);
|
||||||
|
}
|
||||||
|
|
||||||
|
function cancelQrSecurityPrompt(e) {
|
||||||
|
if (e && e.target !== e.currentTarget && e.currentTarget.id === 'modalQrPinPrompt') return;
|
||||||
|
const modal = document.getElementById('modalQrPinPrompt');
|
||||||
|
if (modal) modal.style.display = 'none';
|
||||||
|
pendingQrUnlockSuccessCallback = null;
|
||||||
|
resetPwaNavActive();
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleQrPinInput(e) {
|
||||||
|
const input = document.getElementById('inputQrPin');
|
||||||
|
if (!input) return;
|
||||||
|
const pin = input.value.trim();
|
||||||
|
if (pin.length === 4) {
|
||||||
|
submitQrSecurityPin();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function submitQrSecurityPin() {
|
||||||
|
const input = document.getElementById('inputQrPin');
|
||||||
|
const enteredPin = input ? input.value.trim() : '';
|
||||||
|
const validPin = getQrSecurityPin();
|
||||||
|
|
||||||
|
if (enteredPin === validPin) {
|
||||||
|
const modal = document.getElementById('modalQrPinPrompt');
|
||||||
|
if (modal) modal.style.display = 'none';
|
||||||
|
if (window.sounds) window.sounds.playSuccess();
|
||||||
|
if (typeof pendingQrUnlockSuccessCallback === 'function') {
|
||||||
|
pendingQrUnlockSuccessCallback();
|
||||||
|
pendingQrUnlockSuccessCallback = null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (window.sounds) window.sounds.playError();
|
||||||
|
const err = document.getElementById('msgQrPinError');
|
||||||
|
if (err) {
|
||||||
|
err.style.display = 'block';
|
||||||
|
err.textContent = '❌ PIN incorrecto. Intenta de nuevo.';
|
||||||
|
}
|
||||||
|
if (input) {
|
||||||
|
input.value = '';
|
||||||
|
input.focus();
|
||||||
|
input.style.borderColor = '#ef4444';
|
||||||
|
setTimeout(() => { input.style.borderColor = '#cbd5e1'; }, 1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function retryQrBiometrics() {
|
||||||
|
const estId = currentStudent ? (currentStudent.id || currentStudent.codigo_estudiante || 'default') : 'default';
|
||||||
|
const credId = localStorage.getItem('sibopay_bio_cred_' + estId);
|
||||||
|
try {
|
||||||
|
const verified = await verifyPlatformBiometrics(credId);
|
||||||
|
if (verified) {
|
||||||
|
const modal = document.getElementById('modalQrPinPrompt');
|
||||||
|
if (modal) modal.style.display = 'none';
|
||||||
|
if (window.sounds) window.sounds.playSuccess();
|
||||||
|
if (typeof pendingQrUnlockSuccessCallback === 'function') {
|
||||||
|
pendingQrUnlockSuccessCallback();
|
||||||
|
pendingQrUnlockSuccessCallback = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.warn('Reintento biométrico cancelado:', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function openQrSecuritySettings() {
|
||||||
|
const modal = document.getElementById('modalQrSecuritySettings');
|
||||||
|
if (!modal) return;
|
||||||
|
const isEnabled = isQrSecurityActiveForCurrentStudent();
|
||||||
|
const chk = document.getElementById('chkQrBiometricsEnabled');
|
||||||
|
if (chk) chk.checked = isEnabled;
|
||||||
|
|
||||||
|
const pinBackupInput = document.getElementById('inputSettingsPinBackup');
|
||||||
|
if (pinBackupInput) pinBackupInput.value = getQrSecurityPin();
|
||||||
|
|
||||||
|
const statusBox = document.getElementById('qrBioDeviceStatus');
|
||||||
|
if (statusBox) {
|
||||||
|
const bioAvailable = await checkDeviceBiometricsSupport();
|
||||||
|
if (bioAvailable) {
|
||||||
|
statusBox.style.background = '#ecfdf5';
|
||||||
|
statusBox.style.color = '#065f46';
|
||||||
|
statusBox.style.border = '1px solid #a7f3d0';
|
||||||
|
statusBox.innerHTML = '✨ <strong>Sensor biométrico detectado:</strong> Compatible con Face ID, Huella dactilar o Windows Hello.';
|
||||||
|
} else {
|
||||||
|
statusBox.style.background = '#fffbeb';
|
||||||
|
statusBox.style.color = '#92400e';
|
||||||
|
statusBox.style.border = '1px solid #fde68a';
|
||||||
|
statusBox.innerHTML = '🔒 <strong>Modo PIN Seguro:</strong> Tu carné estará protegido por tu PIN de 4 dígitos (sin sensor biométrico en este navegador).';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
modal.style.display = 'flex';
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeQrSecuritySettings(e) {
|
||||||
|
if (e && e.target !== e.currentTarget && e.currentTarget.id === 'modalQrSecuritySettings') return;
|
||||||
|
const modal = document.getElementById('modalQrSecuritySettings');
|
||||||
|
if (modal) modal.style.display = 'none';
|
||||||
|
updateQrSecurityBadgeUI();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function toggleQrBiometricSetting(enabled) {
|
||||||
|
const estId = currentStudent ? (currentStudent.id || currentStudent.codigo_estudiante || 'default') : 'default';
|
||||||
|
const key = getQrSecurityKey();
|
||||||
|
|
||||||
|
if (enabled) {
|
||||||
|
const bioAvailable = await checkDeviceBiometricsSupport();
|
||||||
|
if (bioAvailable) {
|
||||||
|
try {
|
||||||
|
const studentIdent = currentStudent ? (currentStudent.codigo_estudiante || currentStudent.nombre_completo) : 'estudiante';
|
||||||
|
const credId = await registerPlatformBiometrics(studentIdent);
|
||||||
|
localStorage.setItem('sibopay_bio_cred_' + estId, credId);
|
||||||
|
} catch (regErr) {
|
||||||
|
console.warn('Registro biométrico omitido o cancelado por el usuario:', regErr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
localStorage.setItem(key, 'true');
|
||||||
|
} else {
|
||||||
|
localStorage.setItem(key, 'false');
|
||||||
|
}
|
||||||
|
updateQrSecurityBadgeUI();
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveSettingsPinBackup() {
|
||||||
|
const input = document.getElementById('inputSettingsPinBackup');
|
||||||
|
const pin = input ? input.value.trim() : '';
|
||||||
|
if (!pin || pin.length < 4) {
|
||||||
|
alert('El PIN debe tener 4 dígitos numéricos.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const estId = currentStudent ? (currentStudent.id || currentStudent.codigo_estudiante || 'default') : 'default';
|
||||||
|
localStorage.setItem('sibopay_qr_pin_' + estId, pin);
|
||||||
|
|
||||||
|
const msg = document.getElementById('msgSettingsPinSaved');
|
||||||
|
if (msg) {
|
||||||
|
msg.style.display = 'block';
|
||||||
|
setTimeout(() => { msg.style.display = 'none'; }, 2500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateQrSecurityBadgeUI() {
|
||||||
|
const isEnabled = isQrSecurityActiveForCurrentStudent();
|
||||||
|
const btn = document.getElementById('btnQrSecurityLock');
|
||||||
|
const badge = document.getElementById('lblQrSecurityBadge');
|
||||||
|
const qrBtnText = document.getElementById('qrBtnText');
|
||||||
|
|
||||||
|
if (badge) {
|
||||||
|
badge.textContent = isEnabled ? 'Protegido' : 'Seguridad';
|
||||||
|
}
|
||||||
|
if (btn) {
|
||||||
|
if (isEnabled) {
|
||||||
|
btn.style.background = 'rgba(16, 185, 129, 0.3)';
|
||||||
|
btn.style.borderColor = 'rgba(52, 211, 153, 0.6)';
|
||||||
|
} else {
|
||||||
|
btn.style.background = 'rgba(255, 255, 255, 0.15)';
|
||||||
|
btn.style.borderColor = 'rgba(255, 255, 255, 0.3)';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (qrBtnText) {
|
||||||
|
qrBtnText.textContent = isEnabled ? '🔒 Mi QR' : 'Mi QR';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function resetPwaNavActive() {
|
function resetPwaNavActive() {
|
||||||
const menuBtn = document.getElementById('pwaNavMenu');
|
const menuBtn = document.getElementById('pwaNavMenu');
|
||||||
if (menuBtn) {
|
if (menuBtn) {
|
||||||
|
|||||||
Reference in New Issue
Block a user