Enter 6-digit sign-in, MFA setup, portal two-step and share codes in six boxes that accept paste and autofill, turn red on a wrong code and send the form on the sixth digit, with Remember this browser moved above the code on the sign-in page

This commit is contained in:
johnnyq
2026-11-20 19:02:41 -05:00
parent a8c25f1faa
commit 9dfa6016ae
12 changed files with 304 additions and 41 deletions

View File

@@ -214,6 +214,8 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u
- Tabs that don't fit across the screen go into More at the end of the row, instead of running off the edge out of sight or wrapping onto more lines - a list's views (the tickets list at most screen sizes), a record page's tabs (a contact's, an asset's, a location's), a quick look's, a form's, and the kinds in Admin > Categories and Tags on a phone. The tab you're on always stays out, and More shows a red or amber count when a tab in it needs attention, such as Overdue, Unassigned or a contact's open tickets.
- Linking: Link - along the top of a record's page, on its tabs and in quick looks - opens a list right under it instead of a window. What's linked already is ticked: tick to link, untick to unlink, and it's saved when you click Done or anywhere else, with one message for what changed. It covers documents, files, assets, contacts, licenses, credentials, vendors, services, locations, domains and certificates, a contact's assets and credentials and an asset's credentials, a project's and a service's tickets, a ticket's services and its related tickets. A document's or file's Link lists every kind it can be linked to, in groups. Tickets are no longer added to a project by typing their numbers - tick them in the list (open ones and those closed in the last 90 days), or change the project on the ticket.
- Sign-in codes: the 6-digit code - signing in with two-step sign-in, setting it up (yours, or a contact's in the client portal), and the code emailed for a shared link - goes in six boxes, three and three. Paste or let your phone or password manager fill it, "123 456" too, and it goes as soon as the sixth digit is in - no need to click the button. A wrong code turns the boxes red. On the sign-in page Remember this browser now comes before the code, so tick it first.
### Bug Fixes
- Invoices, quotes and recurring invoices: leaving the page in the middle of typing a note asked "Leave page?" - even though the note saves as you go. It now saves on the way out instead.
@@ -421,6 +423,7 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u
- Starter content: `starterPackRecords()`, `starterPackHere()`, `starterPackUsed()`, `starterContentUnused()` and `starterClearUnused()` in `admin/post/starter_content_model.php`, `clear_starter_unused` in `admin/post/starter_content.php` (rechecked on post, with an Undo of the archive) and `admin/modals/starter_content/starter_clear.php`. A tag counts as used by any table with a `tag_id` or `*_tag_id` column (read from information_schema) or a seat count, a project template by its tickets' history line, and a category by `categoryUsage()` plus ticket templates, settings, RMM alerts, AI triage and child categories. `starterPickHtml()` takes `$open`; `js/starter_content.js` counts `data-starter-format="clear"` and sets up a pack in a window as it opens.
- Tab rows: `js/tab_fold.js` (loaded by `includes/footer.php`) folds the tabs that don't fit into a More dropdown, from the end, never the `.active` one - in `.nav.itflow-asset-tabs` (or a `.nav` inside an `.itflow-asset-tabs` card header), `.nav.itflow-asset-pills`, `.nav.itflow-asset-form-tabs` and any `.nav.itflow-tabs-foldable` (added to the kinds in Admin > Categories and Tags and to the mail settings tabs). A tab is a `.nav-link` of the row or the `.nav-item` around one; rows in a window are set up on `shown.bs.modal`, and a row laid out as a column, or not on screen yet, is left as it is. Folded tabs stay in place with `.itflow-tab-folded`, so Bootstrap's tabs and page scripts keep working on them; More is a `.nav-link.dropdown-toggle` (left out by Bootstrap's tabs and by `agent/js/tickets_live.js`), and its items are built on `show.bs.dropdown` - a link for a tab with an href, otherwise a button that clicks the tab. The row gets `.itflow-tabs-fold` (one line, overflow visible, min-width 0, its tabs `nowrap`), and fits again on resize, font load and any change to its tabs (a MutationObserver - counts updated in place, the open tab changing).
- Link picker: `functions/link_picker.php` (`linkPickerButton()`, `linkPickerSource()`, `linkPickerOptions()`, `linkPickerSave()`, `LINK_PICKER_OWN` for the links that aren't `LINK_RELATIONS` pairs), `js/link_picker.js`, and `agent/ajax.php` `link_picker` (GET: the options, linked ones selected) and `link_picker_save` (POST `add[]` and `remove[]`; `flash=1` keeps the message for the page it reloads). It opens the select picker on a hidden multiple `<select>`: in `js/itflow_select.js` a picker's `.anchor` puts the panel under that element, and closing fires `itflow:close`. With no `to`, the options are grouped by kind and their values are `type:id`. The link windows (`agent/modals/link/link.php` and its one-pair wrappers, `contact_link_asset.php`, `contact_link_credential.php`, `asset_link_credential.php`, `project_link_ticket.php`, `service_link_ticket.php`, `ticket_link_service.php`, `ticket_link.php`) and the handlers only they posted to (`link_items`, `link_tickets`, `link_service_tickets`, `link_ticket_services`, `link_ticket_to_project`, `link_contact_to_asset`, `link_contact_to_credential`, `link_asset_to_credential`) are gone; the unlink handlers stay. `js/ajax_modal.js` keeps a window's URL in `data-modal-url`, so a quick look loads again in place after a change, and `js/tab_fold.js` exposes `window.itflowTabFold()`.
- Code boxes: `otpCodeInput()` in `functions/user.php` draws a 6-digit code field (options: id, label, class, autofocus, submit, invalid) and `js/otp_input.js` (loaded by `includes/footer.php`, `client/includes/footer.php`, `login.php`, `agent/user/mfa_enforcement.php` and `guest/share.php`) lays the one see-through input over six `.itflow-otp-slot` boxes - typing, paste and autofill go to the input, the boxes only show it. Non-digits are stripped (a pasted "123 456" fills them all), the caret stays at the end, a value set from script redraws them (the input's own `value` property), and the `is-invalid` class on the input turns them red until the next digit. `data-otp-submit` sends the form with `requestSubmit()` and its first submit button once the sixth digit is in; `window.itflowOtp(input)` does one added later. `login.php` (agent and client steps), `agent/user/includes/mfa_setup.php`, `client/profile.php` and `guest/share.php` use it; `.itflow-mfa-code` is gone.
## [26.11.8]

View File

@@ -32,7 +32,4 @@ $mfa_secret_grouped = trim(chunk_split($mfa_secret, 4, ' '));
<ol class="itflow-mfa-steps small text-start ps-3 mb-2" start="3">
<li>Enter the 6-digit code it shows:</li>
</ol>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-mobile-alt"></i></span>
<input type="text" class="form-control form-control-lg font-monospace text-center itflow-mfa-code" inputmode="numeric" pattern="[0-9]{6}" minlength="6" maxlength="6" name="verify_code" placeholder="000000" autocomplete="one-time-code" required>
</div>
<?= otpCodeInput('verify_code', ['submit' => true]) ?>

View File

@@ -90,6 +90,7 @@ $mfa_secret = $_SESSION['mfa_token'];
<!-- Bootstrap 4 -->
<script src="<?= staticFileUrl('/js/http.js') ?>"></script>
<script src="<?= staticFileUrl('/libs/bootstrap/js/bootstrap.bundle.min.js') ?>"></script>
<script src="<?= staticFileUrl('/js/otp_input.js') ?>"></script>
<script>

View File

@@ -34,6 +34,7 @@
<script src="<?= staticFileUrl('/libs/sweetalert2/js/sweetalert2.min.js') ?>"></script>
<script src="<?= staticFileUrl('/js/confirm_modal.js') ?>"></script>
<script src="<?= staticFileUrl('/js/autosubmit.js') ?>"></script>
<script src="<?= staticFileUrl('/js/otp_input.js') ?>"></script>
<?php if (!empty($portal_load_phone_inputs)) { ?>
<script src="<?= staticFileUrl('/libs/intl-tel-input/js/intlTelInputWithUtils.min.js') ?>"></script>

View File

@@ -307,10 +307,7 @@ $csrf = escapeHtml($_SESSION['csrf_token']);
<ol class="small ps-3 mb-2" start="3">
<li>Enter the 6-digit code it shows:</li>
</ol>
<div class="input-group mb-3">
<span class="input-group-text"><i class="fas fa-fw fa-mobile-alt"></i></span>
<input type="text" class="form-control form-control-lg font-monospace text-center itflow-mfa-code" inputmode="numeric" pattern="[0-9]{6}" minlength="6" maxlength="6" name="verify_code" placeholder="000000" autocomplete="one-time-code" aria-label="6-digit code" required autofocus>
</div>
<?= otpCodeInput('verify_code', ['class' => 'mb-3', 'submit' => true, 'autofocus' => true]) ?>
<div class="d-flex flex-wrap gap-2">
<button type="submit" class="btn btn-primary" name="portal_mfa_enable"><i class="fas fa-check me-2"></i>Turn on</button>
<?php if (!$portal_mfa_setup_needed) { ?>

View File

@@ -6628,12 +6628,99 @@ tr:hover .itflow-files-rename,
width: 12rem;
height: 12rem;
}
.itflow-mfa-code {
letter-spacing: .4em;
/* A 6-digit code in six boxes (otpCodeInput(), js/otp_input.js). The one real field lies
over the boxes, see-through, so typing, pasting and autofill all go to it; the boxes only
show its digits. Before the script runs it is a plain field. */
.itflow-otp.is-ready {
position: relative;
display: flex;
justify-content: center;
gap: .5rem;
width: 100%;
max-width: 22rem;
margin-inline: auto;
}
.itflow-mfa-code::placeholder {
letter-spacing: .4em;
opacity: .35;
.itflow-otp.is-ready > .itflow-otp-input {
position: absolute;
inset: 0;
z-index: 1;
width: 100%;
height: 100%;
padding: 0;
font-size: 16px;
color: transparent;
-webkit-text-fill-color: transparent;
caret-color: transparent;
background: transparent;
border: 0;
box-shadow: none;
outline: 0;
cursor: text;
}
.itflow-otp.is-ready > .itflow-otp-input::selection {
background: transparent;
}
.itflow-otp.is-ready > .itflow-otp-input::placeholder {
color: transparent;
}
/* Autofilled - keep the browser's colour off */
.itflow-otp.is-ready > .itflow-otp-input:-webkit-autofill {
transition: background-color 600000s 0s, color 600000s 0s;
}
.itflow-otp-slot {
display: flex;
flex: 1 1 0;
align-items: center;
justify-content: center;
min-width: 0;
max-width: 3rem;
height: 3.5rem;
font-family: var(--bs-font-monospace);
font-size: 1.6rem;
font-weight: 600;
color: var(--bs-body-color);
background: var(--bs-body-bg);
border: 1px solid var(--bs-border-color);
border-radius: var(--bs-border-radius-lg);
transition: border-color .15s ease-in-out, box-shadow .15s ease-in-out;
}
.itflow-otp-slot.is-filled {
border-color: var(--bs-secondary-color);
}
.itflow-otp-slot.is-active {
border-color: rgba(var(--bs-primary-rgb), .75);
box-shadow: 0 0 0 .2rem rgba(var(--bs-primary-rgb), .25);
}
/* The next digit goes here - a blinking caret */
.itflow-otp-slot.is-caret::after {
content: "";
width: 2px;
height: 1.6rem;
background: var(--bs-body-color);
animation: itflow-otp-caret 1s step-end infinite;
}
@keyframes itflow-otp-caret {
50% { opacity: 0; }
}
/* Three and three, as the codes are read out */
.itflow-otp-sep {
flex: 0 0 .6rem;
align-self: center;
height: 2px;
background: var(--bs-border-color);
border-radius: 1px;
}
.itflow-otp.is-invalid .itflow-otp-slot {
border-color: var(--bs-form-invalid-border-color);
}
.itflow-otp.is-invalid .itflow-otp-slot.is-active {
box-shadow: 0 0 0 .2rem rgba(var(--bs-danger-rgb), .25);
}
@media (prefers-reduced-motion: reduce) {
.itflow-otp-slot.is-caret::after {
animation: none;
}
}
.itflow-mfa-state {
display: inline-flex;

View File

@@ -519,6 +519,38 @@ function userMfaQrSvg($secret, $email) {
return qrCodeSvg($uri, 'class="itflow-mfa-qr" role="img" aria-label="QR code for your authenticator app"');
}
/**
* A 6-digit code, typed into six boxes - signing in with MFA, setting it up,
* and share codes. The one field under the boxes is what the form sends and
* what phones and password managers fill in; js/otp_input.js draws the boxes
* over it (a plain field without it). $options: id, label (its accessible
* name), class (on the wrapper), autofocus, submit (send the form once the
* sixth digit is in), invalid (a code that was just wrong - the boxes go red).
*/
function otpCodeInput($name, array $options = []) {
$attrs = [
'type' => 'text',
'class' => 'form-control form-control-lg font-monospace text-center itflow-otp-input' . (empty($options['invalid']) ? '' : ' is-invalid'),
'name' => $name,
'id' => $options['id'] ?? '',
'inputmode' => 'numeric',
'pattern' => '[0-9]{6}',
'minlength' => '6',
'maxlength' => '6',
'autocomplete' => 'one-time-code',
'placeholder' => '000000',
'aria-label' => $options['label'] ?? '6-digit code',
];
$html = '<input';
foreach ($attrs as $attr => $value) {
if ($value !== '') {
$html .= ' ' . $attr . '="' . escapeHtml($value) . '"';
}
}
$html .= ' required data-otp' . (empty($options['submit']) ? '' : ' data-otp-submit') . (empty($options['autofocus']) ? '' : ' autofocus') . '>';
return '<div class="itflow-otp' . (empty($options['class']) ? '' : ' ' . escapeHtml($options['class'])) . '">' . $html . '</div>';
}
/** An agent's work not finished - [tickets not closed, recurring tickets] assigned to them. */
function userOpenWork($user_id) {
global $mysqli;

View File

@@ -91,11 +91,7 @@ $item_id = intval($_GET['id'] ?? 0);
<div class="alert alert-success py-2 small" role="status" data-share-code-sent hidden>Code sent - it can take a minute to arrive, and works for <?= SHARE_CODE_MINUTES ?> minutes.</div>
<button type="button" class="btn btn-primary w-100 text-bold mb-3" data-share-send-code><i class="fas fa-paper-plane me-2"></i>Email me a code</button>
<div data-share-code-entry hidden>
<label class="form-label" for="shareCode">Code</label>
<div class="input-group mb-3">
<span class="input-group-text"><i class="fas fa-fw fa-hashtag"></i></span>
<input type="text" class="form-control font-monospace" id="shareCode" name="code" inputmode="numeric" pattern="[0-9 ]{6,7}" maxlength="7" autocomplete="one-time-code" placeholder="123456">
</div>
<?= otpCodeInput('code', ['id' => 'shareCode', 'label' => 'The 6-digit code from the email', 'class' => 'mb-3', 'submit' => true]) ?>
<button type="submit" class="btn btn-primary w-100 text-bold"><i class="fas fa-unlock me-2"></i>Continue</button>
<button type="button" class="btn btn-link w-100 btn-sm mt-2" data-share-resend>Send another code</button>
</div>
@@ -255,6 +251,7 @@ $item_id = intval($_GET['id'] ?? 0);
</div>
<script src="<?= staticFileUrl('/js/pretty_content.js') ?>"></script>
<script src="<?= staticFileUrl('/js/otp_input.js') ?>"></script>
<script src="<?= staticFileUrl('/js/guest_share.js') ?>"></script>
<?php

View File

@@ -92,6 +92,7 @@ try {
<script src="<?= staticFileUrl('/js/tag_picker.js') ?>"></script>
<script src="<?= staticFileUrl('/js/link_picker.js') ?>"></script>
<script src="<?= staticFileUrl('/js/tab_fold.js') ?>"></script>
<script src="<?= staticFileUrl('/js/otp_input.js') ?>"></script>
<script src="<?= staticFileUrl('/js/quick_pick.js') ?>"></script>
<script src="<?= staticFileUrl('/js/ajax_modal.js') ?>"></script>
<script src="<?= staticFileUrl('/js/confirm_modal.js') ?>"></script>

View File

@@ -214,6 +214,7 @@
gone(data.reason);
} else if (data.error) {
error('code', data.error);
form.code.classList.add('is-invalid');
form.code.select();
} else {
form.code.value = '';

158
js/otp_input.js Normal file
View File

@@ -0,0 +1,158 @@
/*
* A 6-digit code in six boxes - signing in with MFA, setting it up, and share
* codes (otpCodeInput() in functions/user.php draws the field).
*
* The one real field stays, see-through, over the boxes: it is what the form
* sends, what phones and password managers fill in, and what a screen reader
* reads. The boxes only show its digits, with the next one to type marked.
* Typing always adds at the end and Backspace takes the last one off; a pasted
* code ("123 456" too) fills them all. With data-otp-submit the form is sent
* once the sixth digit is in, as if its first button were pressed.
*
* Fields on the page are done on load; window.itflowOtp(input) does one added
* later. Setting the field's value from script redraws the boxes, and the
* is-invalid class on it turns them red until the next digit.
*/
(function () {
'use strict';
const native = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value');
function init(input) {
if (!input || input.dataset.otpReady) {
return;
}
input.dataset.otpReady = '1';
const length = Number(input.getAttribute('maxlength')) || 6;
const wrap = input.parentNode;
const boxes = [];
for (let i = 0; i < length; i++) {
if (length === 6 && i === 3) {
const sep = document.createElement('span');
sep.className = 'itflow-otp-sep';
sep.setAttribute('aria-hidden', 'true');
wrap.append(sep);
}
const box = document.createElement('span');
box.className = 'itflow-otp-slot';
box.setAttribute('aria-hidden', 'true');
wrap.append(box);
boxes.push(box);
}
wrap.classList.add('is-ready');
let sent = '';
function digits(text) {
return String(text).replace(/\D/g, '').slice(0, length);
}
function value() {
return native.get.call(input);
}
// All of it selected - typing replaces the code
function allSelected() {
const n = value().length;
return n > 0 && input.selectionStart === 0 && input.selectionEnd === n;
}
function render() {
const text = value();
const focused = document.activeElement === input;
const all = focused && allSelected();
const at = Math.min(text.length, length - 1);
boxes.forEach(function (box, i) {
box.textContent = text.charAt(i);
box.classList.toggle('is-filled', i < text.length);
box.classList.toggle('is-active', focused && (all || i === at));
box.classList.toggle('is-caret', focused && !all && i === text.length);
});
wrap.classList.toggle('is-invalid', input.classList.contains('is-invalid'));
}
// The caret stays at the end, unless it's all selected
function toEnd() {
const n = value().length;
if (document.activeElement === input && !allSelected() && (input.selectionStart !== n || input.selectionEnd !== n)) {
input.setSelectionRange(n, n);
}
render();
}
function submit() {
const form = input.form;
const text = value();
if (!('otpSubmit' in input.dataset) || !form || text.length !== length || text === sent || !form.checkValidity()) {
return;
}
sent = text;
const button = form.querySelector('button[type=submit], input[type=submit]');
if (form.requestSubmit) {
form.requestSubmit(button || undefined);
} else if (button) {
button.click();
} else {
form.submit();
}
}
// A value set from script redraws the boxes
Object.defineProperty(input, 'value', {
configurable: true,
get: value,
set: function (text) {
native.set.call(input, text);
if (value().length < length) {
sent = '';
}
render();
}
});
input.addEventListener('input', function () {
const clean = digits(value());
if (clean !== value()) {
native.set.call(input, clean);
}
if (clean.length < length) {
sent = '';
}
input.classList.remove('is-invalid');
toEnd();
submit();
});
input.addEventListener('paste', function (e) {
const pasted = digits((e.clipboardData || window.clipboardData).getData('text'));
e.preventDefault();
if (!pasted) {
return;
}
native.set.call(input, pasted.length >= length || allSelected() ? pasted : digits(value() + pasted));
input.dispatchEvent(new Event('input', { bubbles: true }));
});
['focus', 'click', 'keyup', 'select'].forEach(function (type) {
input.addEventListener(type, toEnd);
});
input.addEventListener('blur', render);
new MutationObserver(render).observe(input, { attributes: true, attributeFilter: ['class'] });
render();
}
window.itflowOtp = init;
function initAll() {
document.querySelectorAll('input[data-otp]').forEach(init);
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initAll);
} else {
initAll();
}
})();

View File

@@ -584,15 +584,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
// Now that we've transferred what we need, it's safe to clear the dual-role pending session.
unset($_SESSION['pending_dual_login']);
$token_field = "
<div class='input-group mb-3'>
<input type='text' inputmode='numeric' pattern='[0-9]*' maxlength='6'
class='form-control' placeholder='Verify your 2FA code'
name='current_code' required autofocus>
<div class='input-group-text'>
<span class='fas fa-key'></span>
</div>
</div>";
$token_field = otpCodeInput('current_code', ['class' => 'mb-3', 'submit' => true, 'autofocus' => true, 'invalid' => $code_given && !$mfa_locked]);
if ($mfa_locked) {
@@ -700,14 +692,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
];
unset($_SESSION['pending_dual_login']);
$token_field = "
<p class='small text-body-secondary mb-2'>Enter the 6-digit code from your authenticator app.</p>
<div class='input-group mb-3'>
<span class='input-group-text'><span class='fas fa-fw fa-mobile-alt'></span></span>
<input type='text' inputmode='numeric' pattern='[0-9]{6}' maxlength='6' minlength='6'
class='form-control' placeholder='6-digit code' aria-label='6-digit code'
name='current_code' autocomplete='one-time-code' required autofocus>
</div>";
$token_field = otpCodeInput('current_code', ['class' => 'mb-3', 'submit' => true, 'autofocus' => true, 'invalid' => !$client_mfa_locked && ($client_code !== '' || isset($_POST['current_code']))]);
if ($client_mfa_locked) {
// As for agents: no log row while locked, so the window can run out
@@ -899,19 +884,21 @@ $show_login_form = (!$show_role_choice && !$show_mfa_form);
<?php endif; ?>
<?php if ($show_mfa_form): ?>
<!-- STEP 3: MFA only -->
<?= $token_field ?>
<!-- STEP 3: MFA only - the code goes in six boxes and signs in on the last digit, so Remember comes first -->
<p class="small text-body-secondary mb-2">Enter the 6-digit code from your authenticator app.</p>
<input type="hidden" name="pending_mfa_token"
value="<?= htmlspecialchars($_SESSION['pending_mfa_login']['token'] ?? '', ENT_QUOTES) ?>">
<div class="mb-3 mb-3">
<div class="mb-3">
<div class="form-check">
<input type="checkbox" class="form-check-input" id="remember_me" name="remember_me">
<label class="form-check-label" for="remember_me">Remember this browser for <?= $config_login_remember_me_expire ?> day<?= $config_login_remember_me_expire === 1 ? '' : 's' ?></label>
</div>
</div>
<?= $token_field ?>
<input type="hidden" name="pending_mfa_token"
value="<?= htmlspecialchars($_SESSION['pending_mfa_login']['token'] ?? '', ENT_QUOTES) ?>">
<button type="submit" class="btn btn-dark w-100 mb-3" name="mfa_login">Verify & Sign In</button>
<?php endif; ?>
@@ -957,6 +944,7 @@ if (!$config_whitelabel_enabled) {
<script src="<?= staticFileUrl('/libs/bootstrap/js/bootstrap.bundle.min.js') ?>"></script>
<script src="<?= staticFileUrl('/libs/adminlte/js/adminlte.min.js') ?>"></script>
<script src="<?= staticFileUrl('/js/login_prevent_resubmit.js') ?>"></script>
<script src="<?= staticFileUrl('/js/otp_input.js') ?>"></script>
</body>
</html>