Passkeys: agents and client portal contacts can add a passkey with their password and sign in with it instead of their email and password, counting as two-step sign-in; a passkey that supports PRF also opens an agent's credential vault, otherwise credentials stay locked until they type their password once; admins can turn passkeys off in Security settings, and resetting MFA, turning off a contact's two-step sign-in, archiving an agent and the incident password reset remove them; database 4.2.2 adds user_passkeys, users.user_passkey_handle and settings.config_login_passkeys, and lbuchs/WebAuthn 2.2.0 is bundled in libs/webauthn

This commit is contained in:
johnnyq
2026-11-28 23:31:17 -05:00
parent 4a2933cac9
commit d9c1c752bd
50 changed files with 4679 additions and 46 deletions

View File

@@ -0,0 +1,49 @@
<?php
/*
* ITFlow - Database update to version 4.2.2 (from 4.2.1)
* Included by admin/database_updates.php - do not access directly
*
* Signing in with a passkey (functions/passkey.php), for agents and client portal contacts:
* user_passkeys - each passkey a user added: its name, the credential's id (and a hash of it, to
* find it by), its public key, the authenticator's signature count, whether it is synced
* between devices, and for an agent a copy of the vault key sealed by the passkey itself
* (passkey_vault, '' when the passkey can't open the vault)
* users.user_passkey_handle - the random id a user's passkeys are made for, the same for each
* settings.config_login_passkeys - whether passkeys can be used to sign in (on)
*
* Re-runnable: the table is made only when it isn't there, and each column only where it isn't.
*/
defined('FROM_DB_UPDATER') || die("Direct file access is not allowed");
mysqli_query($mysqli, "CREATE TABLE IF NOT EXISTS `user_passkeys` (
`passkey_id` int(11) NOT NULL AUTO_INCREMENT,
`passkey_user_id` int(11) NOT NULL,
`passkey_name` varchar(200) NOT NULL,
`passkey_credential_id` varchar(1400) NOT NULL,
`passkey_credential_hash` char(64) NOT NULL,
`passkey_public_key` text NOT NULL,
`passkey_sign_count` int(10) unsigned NOT NULL DEFAULT 0,
`passkey_aaguid` char(36) NOT NULL DEFAULT '',
`passkey_transports` varchar(100) NOT NULL DEFAULT '',
`passkey_synced` tinyint(1) NOT NULL DEFAULT 0,
`passkey_vault` varchar(255) NOT NULL DEFAULT '',
`passkey_created_at` datetime NOT NULL DEFAULT current_timestamp(),
`passkey_used_at` datetime DEFAULT NULL,
PRIMARY KEY (`passkey_id`),
UNIQUE KEY `passkey_credential_hash` (`passkey_credential_hash`),
KEY `passkey_user_id` (`passkey_user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci");
$itflow_passkey_columns = [
['users', 'user_passkey_handle', "varchar(64) DEFAULT NULL AFTER `user_token`"],
['settings', 'config_login_passkeys', "tinyint(1) NOT NULL DEFAULT 1 AFTER `config_login_remember_me_expire`"],
];
foreach ($itflow_passkey_columns as [$itflow_table, $itflow_column, $itflow_definition]) {
$itflow_column_exists = mysqli_query($mysqli, "SELECT 1 FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '$itflow_table' AND COLUMN_NAME = '$itflow_column' LIMIT 1");
if (!$itflow_column_exists || mysqli_num_rows($itflow_column_exists) === 0) {
mysqli_query($mysqli, "ALTER TABLE `$itflow_table` ADD `$itflow_column` $itflow_definition");
}
}

View File

@@ -23,6 +23,9 @@ $form_id = 'userForm' . ($is_new ? 'New' : $form_user_id);
$form_role_id = intval($form['user_role_id'] ?? 0);
$form_roles = userRoles($form_role_id);
$form_has_mfa = trim((string) ($form['user_token'] ?? '')) !== '';
// Their passkeys count as MFA - resetting it takes them away too (functions/passkey.php)
$form_passkeys = !empty($form_user_id) && passkeysReady() ? passkeyCount($form_user_id) : 0;
$form_has_mfa = $form_has_mfa || $form_passkeys > 0;
$form_has_avatar = trim((string) ($form['user_avatar'] ?? '')) !== '';
$form_access = $form_user_id ? userClientAccess([$form_user_id])[$form_user_id] : ['allow' => [], 'deny' => []];
$form_invites = $is_new && userInvitesReady();
@@ -170,7 +173,7 @@ $value = function ($key) use ($form) {
<div class="form-check mt-2">
<input class="form-check-input" type="checkbox" name="reset_mfa" value="1" id="<?= $form_id ?>ResetMfa">
<label class="form-check-label" for="<?= $form_id ?>ResetMfa">Reset it</label>
<div class="form-text mt-0">For a lost or replaced phone - their authenticator app is forgotten, and so are their remembered browsers.</div>
<div class="form-text mt-0">For a lost or replaced phone - their authenticator app is forgotten, and so are their remembered browsers<?= $form_passkeys ? ' and their ' . ($form_passkeys === 1 ? 'passkey' : "$form_passkeys passkeys") : '' ?>.</div>
</div>
<?php } ?>
</div>

View File

@@ -99,6 +99,10 @@ if (isset($_GET['portal_user_reset_mfa'])) {
$row = portalUserForRequest($_GET['portal_user_reset_mfa']);
portalMfaReset($row['user_id']);
// Their passkeys count as two-step sign-in, and go too (functions/passkey.php)
if (passkeysReady()) {
passkeysForget($row['user_id']);
}
logAudit("Contact", "Edit", "$session_name turned off two-step sign-in for " . escapeSql($row['contact_name']) . " in the client portal", $row['client_id'], $row['contact_id']);
portalMailAccountChange($row['user_email'], $row['contact_name'], 'mfa_off_by_us');
@@ -198,6 +202,9 @@ if (isset($_POST['bulk_portal_reset_mfa'])) {
continue;
}
portalMfaReset($user_id);
if (passkeysReady()) {
passkeysForget($user_id);
}
logAudit("Contact", "Edit", "$session_name turned off two-step sign-in for " . escapeSql($row['contact_name']) . " in the client portal", $row['client_id'], $row['contact_id']);
portalMailAccountChange($row['user_email'], $row['contact_name'], 'mfa_off_by_us');
$count++;

View File

@@ -3,8 +3,9 @@
/*
* Admin > Settings > Security (admin/settings_security.php):
* edit_security_settings saves the sign-in page's message and login key, how
* long a browser is remembered, when admins hear of failed sign-ins, whether
* the API takes a key in the address, and shared links. A login key required with none set - which would leave
* long a browser is remembered, whether passkeys sign people in, when admins
* hear of failed sign-ins, whether the API takes a key in the address, and
* shared links. A login key required with none set - which would leave
* agents no way in but the database - is refused, as is a key that couldn't
* be typed into an address, or a time out of range. Nothing is saved then.
* A choice not offered settles to the usual one.
@@ -16,8 +17,10 @@ if (isset($_POST['edit_security_settings'])) {
validateCSRFToken();
$before = settingsCurrent(['config_login_message', 'config_login_key_required', 'config_login_key_secret', 'config_login_remember_me_expire',
'config_login_alert_failures', 'config_api_key_in_address', 'config_share_max_hours', 'config_share_require_protection']);
$before = settingsCurrent(array_merge(['config_login_message', 'config_login_key_required', 'config_login_key_secret', 'config_login_remember_me_expire',
'config_login_alert_failures', 'config_api_key_in_address', 'config_share_max_hours', 'config_share_require_protection'],
// Passkeys, once the database has them (functions/passkey.php)
passkeysReady() ? ['config_login_passkeys'] : []));
// What was sent, and what is saved for anything that wasn't - a switch not sent is off
$in = $_POST + array_map(function ($value) { return (string) $value; }, $before);
@@ -32,6 +35,9 @@ if (isset($_POST['edit_security_settings'])) {
'config_share_max_hours' => intval($in['config_share_max_hours']),
'config_share_require_protection' => empty($_POST['config_share_require_protection']) ? 0 : 1,
];
if (array_key_exists('config_login_passkeys', $before)) {
$after['config_login_passkeys'] = empty($_POST['config_login_passkeys']) ? 0 : 1;
}
$errors = [];
if ($after['config_login_key_secret'] !== '' && !preg_match('/^\w{3,99}$/', $after['config_login_key_secret'])) {

View File

@@ -285,10 +285,14 @@ if (isset($_POST['edit_user'])) {
$changes[] = 'password changed';
userNotifyChange($name, $email, 'an administrator set a new password');
}
if (!empty($_POST['reset_mfa']) && trim((string) $row['user_token']) !== '') {
if (!empty($_POST['reset_mfa']) && (trim((string) $row['user_token']) !== '' || (passkeysReady() && passkeyCount($user_id)))) {
userResetMfa($user_id);
$passkeys_removed = passkeysReady() ? passkeysForget($user_id) : 0;
if ($passkeys_removed) {
$changes[] = 'passkeys removed';
}
$changes[] = 'MFA reset';
userNotifyChange($name, $email, 'an administrator turned off multi-factor authentication' . ($force_mfa ? ' - you will set it up again at your next sign in' : ''));
userNotifyChange($name, $email, 'an administrator turned off multi-factor authentication' . ($passkeys_removed ? ' and removed your passkeys' : '') . ($force_mfa ? ' - you will set it up again at your next sign in' : ''));
}
if (strcasecmp($email, $row['user_email']) !== 0) {
$changes[] = 'email changed';
@@ -388,9 +392,11 @@ if (isset($_GET['reset_user_mfa'])) {
$user_id = intval($row['user_id']);
userResetMfa($user_id);
userNotifyChange($row['user_name'], $row['user_email'], 'an administrator turned off multi-factor authentication' . (intval($row['user_config_force_mfa']) ? ' - you will set it up again at your next sign in' : ''));
// Their passkeys count as MFA, and go too (functions/passkey.php)
$passkeys_removed = passkeysReady() ? passkeysForget($user_id) : 0;
userNotifyChange($row['user_name'], $row['user_email'], 'an administrator turned off multi-factor authentication' . ($passkeys_removed ? ' and removed your passkeys' : '') . (intval($row['user_config_force_mfa']) ? ' - you will set it up again at your next sign in' : ''));
logAudit("User", "Edit", "$session_name reset MFA for user " . escapeSql($row['user_name']), 0, $user_id);
logAudit("User", "Edit", "$session_name reset MFA for user " . escapeSql($row['user_name']) . ($passkeys_removed ? " and removed their passkeys" : ''), 0, $user_id);
flashAlert("MFA reset for <strong>" . $row['user_name'] . "</strong>" . (intval($row['user_config_force_mfa']) ? ' - they set it up again at their next sign in' : ' - they sign in with their password alone until they turn it on'));
@@ -418,6 +424,9 @@ if (isset($_POST['archive_user'])) {
$scrambled = escapeSql(password_hash(randomString(), PASSWORD_DEFAULT));
mysqli_query($mysqli, "UPDATE users SET user_name = '$archived_name', user_password = '$scrambled', user_status = 0, user_specific_encryption_ciphertext = '', user_archived_at = NOW() WHERE user_id = $user_id");
mysqli_query($mysqli, "DELETE FROM remember_tokens WHERE remember_token_user_id = $user_id");
if (passkeysReady()) {
passkeysForget($user_id);
}
logAudit("User", "Archive", "$session_name archived user " . escapeSql($row['user_name']), 0, $user_id);
@@ -653,12 +662,15 @@ if (isset($_POST['ir_reset_user_password'])) {
while ($row = mysqli_fetch_assoc($sql_users)) {
$new_password = generateReadablePassword(4);
userSetPassword($row['user_id'], $new_password);
// A remembered browser would skip their MFA code - in an incident, it must not
// A remembered browser would skip their MFA code, and a passkey their password - in an incident, neither must
mysqli_query($mysqli, "DELETE FROM remember_tokens WHERE remember_token_user_id = " . intval($row['user_id']));
if (passkeysReady()) {
passkeysForget($row['user_id']);
}
$reset[] = $row + ['password' => $new_password];
}
logAudit("User", "Edit", "$session_name reset the passwords of " . count($reset) . " agent(s) and forgot their remembered browsers (incident response)");
logAudit("User", "Edit", "$session_name reset the passwords of " . count($reset) . " agent(s) and forgot their remembered browsers and passkeys (incident response)");
// Shown once, not kept - a plain page to read out or copy, and nothing else on it
header('Cache-Control: no-store');

View File

@@ -3,8 +3,8 @@
/*
* Admin > Settings > Security - the sign-in page (its message, and a login
* key agents need in its address), how long two-step sign-in remembers a
* device, when admins hear of failed sign-ins, whether the API takes a key in
* the address, and shared links. Saved by
* device, whether passkeys sign people in, when admins hear of failed sign-ins,
* whether the API takes a key in the address, and shared links. Saved by
* edit_security_settings (admin/post/settings_security.php).
*/
@@ -13,6 +13,10 @@ require_once "includes/inc_all_admin.php";
// Read here rather than with every page's settings, so a page still opens before the update that adds it
$config_login_alert_failures = signInAlertLimit();
$config_api_key_in_address = apiKeyInAddressAccepted() ? 1 : 0;
// Passkeys, once the database has them (functions/passkey.php)
$passkeys_ready = passkeysReady();
$config_login_passkeys = passkeysOn() ? 1 : 0;
$passkey_address = passkeyRp()['address'];
$login_url = rtrim('https://' . preg_replace('#^https?://#', '', (string) $config_base_url), '/') . '/login.php?key=';
$login_key_shown = (string) settingsValue('config_login_key_secret', $config_login_key_secret);
@@ -44,6 +48,12 @@ echo settingsPageStart('security');
'suffix' => 'days', 'help' => 'Each agent can see and forget their remembered browsers in their own Security settings.']) ?>
<?= settingsSectionEnd() ?>
<?php if ($passkeys_ready) { ?>
<?= settingsSectionStart('passkeys', 'Passkeys', 'A passkey signs an agent or a client contact in with their fingerprint, face or PIN instead of their password, and counts as two-step sign-in. Each adds their own from their account, with their password. Passkeys are made for ' . escapeHtml($passkey_address) . ' - moving ITFlow to another address means adding them again.') ?>
<?= settingsSwitch('config_login_passkeys', $config_login_passkeys, ['help' => 'Off, passkeys already added are kept, but don\'t sign anyone in until it is on again.']) ?>
<?= settingsSectionEnd() ?>
<?php } ?>
<?= settingsSectionStart('failed', 'Failed sign-ins', 'Counted over ' . SIGN_IN_WINDOW_MINUTES . ' minutes, from one address or against one account. An address is turned away after ' . SIGN_IN_LOCKOUT . ', whatever is set here.') ?>
<?= settingsSelect('config_login_alert_failures', intval($config_login_alert_failures), signInAlertOptions(), ['icon' => 'fa-bell',
'help' => 'Each admin gets a notification and an email.']) ?>

View File

@@ -32,6 +32,8 @@ if (($_GET['canned_date'] ?? '') === 'custom' && empty($_GET['dtf'])) {
}
$roles = userRoles();
// Who has passkeys - resetting two-step sign-in takes them away too (functions/passkey.php)
$passkey_counts = passkeyCounts();
$role_filter = isset($roles[intval($_GET['role'] ?? 0)]) ? intval($_GET['role']) : 0;
// The search is for the side being looked at - agents and their invites, or portal users - and leaves the other side's counts alone
@@ -435,6 +437,7 @@ $csrf = $_SESSION['csrf_token'];
$is_on = $off_why === '';
$is_local = $row['user_auth_method'] === 'local';
$has_mfa = trim((string) $row['user_token']) !== '';
$has_passkeys = !empty($passkey_counts[$user_id]);
$can_turn_on = !$is_on && empty($row['client_archived_at']) && empty($row['contact_archived_at']);
$name = escapeHtml($row['contact_name']);
$contact_url = "/agent/contact.php?client_id=$client_id&amp;contact_id=$contact_id";
@@ -515,9 +518,9 @@ $csrf = $_SESSION['csrf_token'];
</a>
<?php } ?>
<?php } ?>
<?php if ($has_mfa) { ?>
<?php if ($has_mfa || $has_passkeys) { ?>
<a class="dropdown-item confirm-link" href="post.php?portal_user_reset_mfa=<?= $user_id ?>&csrf_token=<?= $csrf ?>"
data-confirm-text="<?= $name ?> signs in with their password alone until they set two-step sign-in up again, and is emailed that it was turned off. For a lost or replaced phone."
data-confirm-text="<?= $name ?> signs in with their password alone until they set two-step sign-in up again<?= $has_passkeys ? ', and their passkeys are removed' : '' ?>, and is emailed that it was turned off. For a lost or replaced phone."
data-confirm-button="Turn off two-step">
<i class="fas fa-fw fa-mobile-alt text-secondary me-2"></i>Turn Off Two-step Sign-in
</a>
@@ -694,6 +697,7 @@ $csrf = $_SESSION['csrf_token'];
$is_active = intval($row['user_status']) === 1 && !$is_archived;
$is_admin = intval($row['role_is_admin']) === 1;
$has_mfa = trim((string) $row['user_token']) !== '';
$has_passkeys = !empty($passkey_counts[$user_id]);
$display_name = $is_archived ? trim(str_replace(' (archived)', '', $row['user_name'])) : $row['user_name'];
$name = escapeHtml($display_name);
$quick_url = "modals/user/user.php?id=$user_id";
@@ -788,9 +792,9 @@ $csrf = $_SESSION['csrf_token'];
<a class="dropdown-item ajax-modal" href="#" data-modal-size="lg" data-modal-url="modals/user/user_edit.php?id=<?= $user_id ?>">
<i class="fas fa-fw fa-user-edit text-secondary me-2"></i>Edit
</a>
<?php if ($has_mfa) { ?>
<?php if ($has_mfa || $has_passkeys) { ?>
<a class="dropdown-item confirm-link" href="post.php?reset_user_mfa=<?= $user_id ?>&csrf_token=<?= $csrf ?>"
data-confirm-text="<?= $name ?> signs in with their password alone<?= intval($row['user_config_force_mfa']) ? ' until they set MFA up again, which they are asked to at their next sign in' : ' until they turn MFA on again' ?>. For a lost or replaced phone."
data-confirm-text="<?= $name ?> signs in with their password alone<?= intval($row['user_config_force_mfa']) ? ' until they set MFA up again, which they are asked to at their next sign in' : ' until they turn MFA on again' ?><?= $has_passkeys ? ' - their passkeys are removed too' : '' ?>. For a lost or replaced phone."
data-confirm-button="Reset MFA">
<i class="fas fa-fw fa-mobile-alt text-secondary me-2"></i>Reset MFA
</a>