/* * Credentials - the password, copy and 2FA buttons functions/credential.php * draws wherever a credential is listed (the credentials list, assets, * contacts, vendors, licenses, the quick looks). Bound once on document, so * they work in ajax modals too. * * Nothing secret is in the page until it is asked for: the eye and the copy * button fetch the password from agent/ajax.php (get_credential_via_id), the * 2FA button fetches the current code (get_totp_token_via_id) - both check the * module and the client and log the view. A box with data-credential-history * is one of the credential's earlier passwords, fetched the same way. * * After a passkey sign-in that couldn't open the vault, both answer {locked}: * the Unlock window (includes/footer.php) asks for their password, then what * was asked for is tried again. [data-vault-unlock] opens it too. */ (function () { // How long a shown password stays on screen, and how many codes in a row the 2FA button follows on to var SHOW_FOR = 30000; var OTP_ROUNDS = 6; // The Unlock window, and what to do once it's unlocked - try again, or show the page afresh when it has locked parts function unlockVault(then) { var modal = document.getElementById('vaultUnlockModal'); if (!modal) { return; } modal.itflowThen = then; bootstrap.Modal.getOrCreateInstance(modal).show(); setTimeout(function () { var input = modal.querySelector('[name=password]'); if (input) { input.focus(); } }, 200); } var unlockModal = document.getElementById('vaultUnlockModal'); if (unlockModal) { unlockModal.querySelector('[data-vault-unlock-form]').addEventListener('submit', function (e) { e.preventDefault(); var form = this; var input = form.querySelector('[name=password]'); var message = form.querySelector('[data-vault-unlock-message]'); var submit = form.querySelector('[type=submit]'); submit.disabled = true; message.textContent = ''; fetch('/agent/ajax.php', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, credentials: 'same-origin', body: new URLSearchParams({ vault_unlock: 1, csrf_token: unlockModal.dataset.csrf, password: input.value }).toString() }).then(function (res) { return res.json(); }).then(function (result) { submit.disabled = false; if (!result.unlocked) { var alert = document.createElement('div'); alert.className = 'alert alert-danger'; alert.textContent = result.error || 'That didn\'t unlock them - try again.'; message.appendChild(alert); input.select(); return; } input.value = ''; bootstrap.Modal.getOrCreateInstance(unlockModal).hide(); if (document.querySelector('[data-vault-locked]') || typeof unlockModal.itflowThen !== 'function') { window.location.reload(); return; } unlockModal.itflowThen(); }).catch(function () { submit.disabled = false; }); }); } itflowBindOnce('itflowVaultUnlock', 'click', '[data-vault-unlock]', function (e) { e.preventDefault(); unlockVault(null); }); function fetchCredential(box, done) { var params = { get_credential_via_id: 'true', credential_id: box.dataset.credentialId }; if (box.dataset.credentialHistory) { params.history_id = box.dataset.credentialHistory; } itflowGet('/agent/ajax.php', params, function (data) { var credential = null; try { credential = JSON.parse(data); } catch (e) {} if (credential && credential.locked) { unlockVault(function () { fetchCredential(box, done); }); return; } if (credential) { done(credential); } }); } function hidePassword(box) { var value = box.querySelector('.itflow-credential-pw-value'); var mask = box.querySelector('.itflow-credential-pw-mask'); var button = box.querySelector('.itflow-credential-reveal'); clearTimeout(box.itflowHide); value.textContent = ''; value.hidden = true; mask.hidden = false; if (button) { button.querySelector('i').className = 'fas fa-eye'; button.setAttribute('aria-pressed', 'false'); button.title = 'Show password'; } } // The eye: show the password for a while, or hide it again itflowBindOnce('itflowCredentialReveal', 'click', '.itflow-credential-reveal', function (e) { e.preventDefault(); var button = this; var box = button.closest('.itflow-credential-pw'); if (!box) { return; } var value = box.querySelector('.itflow-credential-pw-value'); if (!value.hidden) { hidePassword(box); return; } fetchCredential(box, function (credential) { value.textContent = credential.password || ''; value.hidden = false; box.querySelector('.itflow-credential-pw-mask').hidden = true; button.querySelector('i').className = 'fas fa-eye-slash'; button.setAttribute('aria-pressed', 'true'); button.title = 'Hide password'; clearTimeout(box.itflowHide); box.itflowHide = setTimeout(function () { hidePassword(box); }, SHOW_FOR); }); }); // Copy: fetch the password and copy it, without showing it itflowBindOnce('itflowCredentialCopy', 'click', '.itflow-credential-copy', function (e) { e.preventDefault(); var button = this; var box = button.closest('[data-credential-id]'); if (!box) { return; } fetchCredential(box, function (credential) { itflowCopyText(credential.password || '', button).then( function () { flashTooltip(button, 'Copied!'); }, function () { flashTooltip(button, 'Failed!'); } ); }); }); function resetOtp(button) { clearInterval(button.itflowTimer); delete button.dataset.code; button.classList.remove('is-live', 'is-ending'); button.querySelector('.itflow-credential-otp-code').textContent = '2FA'; button.querySelector('.itflow-credential-otp-time').textContent = ''; button.title = 'Show the 2FA code'; } // 2FA: the first click fetches the code and counts it down, following on to // the next one for a few minutes; clicking it while it shows copies it itflowBindOnce('itflowCredentialOtp', 'click', '.itflow-credential-otp', function (e) { e.preventDefault(); var button = this; if (button.dataset.code) { itflowCopyText(button.dataset.code, button).then( function () { flashTooltip(button, 'Copied!'); }, function () { flashTooltip(button, 'Failed!'); } ); return; } var codeEl = button.querySelector('.itflow-credential-otp-code'); var timeEl = button.querySelector('.itflow-credential-otp-time'); var rounds = 0; var last = 31; function load() { itflowGet('/agent/ajax.php', { get_totp_token_via_id: 'true', credential_id: button.dataset.credentialOtp }, function (data) { var code = ''; try { var answer = JSON.parse(data); if (answer && answer.locked) { resetOtp(button); unlockVault(function () { button.click(); }); return; } code = String(answer || ''); } catch (err) {} if (!/^[0-9]{6,8}$/.test(code)) { resetOtp(button); codeEl.textContent = 'No code'; return; } button.dataset.code = code; codeEl.textContent = code.length === 6 ? code.slice(0, 3) + ' ' + code.slice(3) : code; button.classList.add('is-live'); button.title = 'Click to copy the code'; }); } function tick() { var left = 30 - Math.floor(Date.now() / 1000) % 30; timeEl.textContent = left + 's'; button.classList.toggle('is-ending', left <= 5); // A new period - the next code, for a few rounds, then back to the button if (left > last) { rounds++; if (rounds >= OTP_ROUNDS || !document.body.contains(button)) { resetOtp(button); return; } load(); } last = left; } clearInterval(button.itflowTimer); load(); tick(); button.itflowTimer = setInterval(tick, 1000); }); })();