Files
itflow/js/credentials.js
johnnyq fecd1cabff Credentials makeover: rebuilt credentials list with kind icons, username copy, password show/copy and live 2FA code buttons fetched on demand, where it opens, what it is for (asset, contact, vendors, licenses, services), password age pills and last used, filters for Starred, With 2FA, No 2FA, Not changed in 90 days and Shared with counts, search across notes, second link, tags, asset and contact, open straight to a credential with ?open=ID; new credential quick look with login, age, last viewed by, links with unlink, notes, open share links with turn off, and history; tabbed new/edit form with password generator (passphrase or random), strength meter, 2FA secret that accepts spaces or an otpauth:// link and shows the current code to check, copy buttons on both links, prefill from asset, contact or vendor; new Change password window that dates and logs the change; new Share window from anywhere (contact or email, 1 hour to 1 month, gone after opened once by default, link copied); same login controls on asset and contact pages and quick looks, vendor Logins tab, license page and quick look, service quick look, client overview favourites and search; credential pickers show icons and say when a credential moves from another asset or contact; client overview Passwords due for a change attention item and Active share links limited to live links; rotation report with selectable span, per client counts and oldest first; more export columns; demo data gets 2FA secrets, varied password ages and logins attached to their assets. Fixes passwords being blanked when the vault session had expired, other clients' assets, contacts and tags accepted on save, bulk star using the wrong permission, credential links to assets and contacts not checking the client, the edit or link permission, or which asset or contact an unlink was for, credentials left pointing at deleted assets and contacts, share links left open after a credential was deleted, import carrying values between rows and accepting any file type, 2FA secrets saved with spaces or lowercase giving no code, the service quick look listing credentials without credential access and repeating assets, an asset login with only a username being lost, and the API not dating new passwords. Adds functions/credential.php and js/credentials.js; removes credential_model.php and the old credential show password/OTP scripts. No DB change.
2026-10-14 23:33:29 -04:00

156 lines
5.9 KiB
JavaScript

/*
* 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.
*/
(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;
function fetchCredential(id, done) {
itflowGet('/agent/ajax.php', { get_credential_via_id: 'true', credential_id: id }, function (data) {
var credential = null;
try {
credential = JSON.parse(data);
} catch (e) {}
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.dataset.credentialId, 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.dataset.credentialId, 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 {
code = String(JSON.parse(data) || '');
} 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);
});
})();