Add identity providers for agents and client contacts: Admin, Identity Providers under Integrations adds Microsoft 365, Google Workspace and Nextcloud (OIDC Identity Provider app) sign-in buttons and IMAP mail-password sign-in, each for agents, client contacts or both and for the email domains listed; no account is created by signing in; Microsoft, Google and Nextcloud stand in for ITFlow two-step sign-in while IMAP still asks for the code; a provider can be required, turning off password sign-in on its domains with passkeys and administrator passwords still working; credentials stay locked after a provider or mail password sign-in until the ITFlow password is typed; the client portal Microsoft Entra single sign-on moves to a Microsoft 365 provider keeping its redirect address; database 4.2.5 adds identity_providers and drops the old Entra settings; 26.12.3 changelog entries

This commit is contained in:
johnnyq
2026-11-29 20:13:10 -05:00
parent 626f472990
commit fffa5680db
32 changed files with 1671 additions and 340 deletions

183
login.php
View File

@@ -105,7 +105,14 @@ $config_whitelabel_enabled = intval($row['config_whitelabel_enabled']);
$config_login_key_required = $row['config_login_key_required'];
$config_login_key_secret = $row['config_login_key_secret'];
$azure_client_id = $row['config_azure_client_id'] ?? null;
// Signing in through an identity provider - Microsoft 365, Google, Nextcloud (functions/identity_provider.php, login_provider.php).
// Without the login key the page is the contacts', so only theirs show - the agents' sign-in stays out of sight
$login_providers = identityProviderButtons();
if ($config_login_key_required && !hash_equals((string) $config_login_key_secret, is_string($_GET['key'] ?? null) ? $_GET['key'] : '')) {
$login_providers = array_values(array_filter($login_providers, function ($login_provider) {
return identityProviderFor($login_provider, 2);
}));
}
// Signing in with a passkey (functions/passkey.php, login_passkey.php) - once the database has them
$config_login_passkeys = intval($row['config_login_passkeys'] ?? 0);
@@ -145,7 +152,10 @@ function pendingExpired($sess, $ttl_seconds = 120) {
}
// POST handling
if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_POST['role_choice']) || isset($_POST['mfa_login']) || isset($_POST['passkey_login']))) {
// Back from an identity provider, which login_provider.php has checked - a GET, as the provider's redirect was
$is_provider_return = $_SERVER['REQUEST_METHOD'] === 'GET' && is_string($_GET['provider_login'] ?? null) && $_GET['provider_login'] !== '';
if (($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_POST['role_choice']) || isset($_POST['mfa_login']) || isset($_POST['passkey_login']))) || $is_provider_return) {
$role_choice = $_POST['role_choice'] ?? null;
@@ -154,6 +164,16 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
$is_mfa_step = isset($_POST['mfa_login']);
// After login_passkey.php checked a passkey - which counts as two-step sign-in
$is_passkey_step = isset($_POST['passkey_login']) && !$is_login_step && !$is_mfa_step && !$is_role_step;
// After login_provider.php: an identity provider signed them in - its own two-step sign-in stands in for ours
$is_provider_step = $is_provider_return && !$is_login_step && !$is_mfa_step && !$is_role_step && !$is_passkey_step;
// Signed in by a provider - straight back from it, or choosing agent or client after it (functions/identity_provider.php)
$via_provider = null;
// The IMAP provider that checked the password, when one did - the password is the mail server's, not ours.
// By account type, as one may be an agent's and not a client's; $via_imap is the signed-in one's
$via_imap = null;
$via_imap_types = [];
// A provider on the account's domains that is required, so its password doesn't work here
$required_provider = null;
// -----------------------------------
// STEP 2: ROLE CHOICE (no email/pass)
@@ -173,6 +193,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
} else {
$email = escapeSql($sess['email'] ?? '');
$email_raw = (string) ($sess['email'] ?? '');
// Agent or client, after a provider signed them in - or after the mail server checked their password
if (($sess['via'] ?? '') === 'provider') {
$via_provider = $sess['provider'] ?? null;
}
$via_imap_types = (array) ($sess['via_imap'] ?? []);
}
}
@@ -196,6 +221,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
$email_raw = (string) ($sess['email'] ?? '');
// A client portal user's code, or an agent's
$role_choice = ($sess['type'] ?? 'agent') === 'client' ? 'client' : 'agent';
// The mail server checked their password (an IMAP provider) - for the one account this code is for
$via_imap_types = !empty($sess['via_imap']) ? [1 => $sess['via_imap'], 2 => $sess['via_imap']] : [];
}
}
@@ -222,6 +249,29 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
}
}
// -----------------------------------
// AN IDENTITY PROVIDER, checked by login_provider.php (no email/pass)
// -----------------------------------
if ($is_provider_step && empty($response)) {
$posted_token = (string) $_GET['provider_login'];
$sess = $_SESSION['pending_provider_login'] ?? null;
unset($_SESSION['pending_provider_login']);
if (pendingExpired($sess) || empty($sess['token']) || !hash_equals($sess['token'], $posted_token)) {
header("HTTP/1.1 401 Unauthorized");
$response = "
<div class='alert alert-danger'>
Your sign in expired. Please sign in again.
</div>";
} else {
$email = escapeSql($sess['email'] ?? '');
$email_raw = (string) ($sess['email'] ?? '');
$via_provider = ['id' => intval($sess['provider_id'] ?? 0), 'name' => (string) ($sess['provider_name'] ?? 'an identity provider')];
$provider_accounts = ['agent' => intval($sess['agent_user_id'] ?? 0), 'client' => intval($sess['client_user_id'] ?? 0)];
}
}
// -----------------------------------
// STEP 1: INITIAL CREDENTIALS
// -----------------------------------
@@ -274,6 +324,12 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
$allowed_client_id = isset($sess['client_user_id']) ? intval($sess['client_user_id']) : null;
}
// The accounts the provider can sign in - the other kind, or one it isn't for, is left out (-1 matches no one)
if ($is_provider_step) {
$allowed_agent_id = $provider_accounts['agent'] ?: -1;
$allowed_client_id = $provider_accounts['client'] ?: -1;
}
if ($is_mfa_step || $is_passkey_step) {
$sess = $_SESSION[$is_passkey_step ? 'pending_passkey_login' : 'pending_mfa_login'] ?? null;
// Only the account whose password (or passkey) was checked - the other kind is left out altogether (-1 matches no one)
@@ -291,8 +347,24 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
$ut = intval($r['user_type']);
if ($is_login_step) {
// Only Step 1 checks password
if (!password_verify($password, $r['user_password'])) {
// Only Step 1 checks password - the mail server's, for an account an IMAP provider signs in
$imap = identityProviderImapFor($email_raw, $ut);
if ($imap) {
[$imap_ok] = identityProviderImapCheck($imap, $email_raw, $password);
if ($imap_ok && ($ut === 1 || ($r['user_auth_method'] ?? '') === 'local')) {
$via_imap_types[$ut] = ['id' => intval($imap['identity_provider_id']), 'name' => (string) $imap['identity_provider_name']];
identityProviderUsed($via_imap_types[$ut]['id']);
} elseif ($ut !== 1 || !identityProviderAdminRole($r['user_role_id'] ?? 0) || !password_verify($password, $r['user_password'])) {
// An administrator's own password still works - for when the mail server is down
continue;
}
} elseif (!password_verify($password, $r['user_password'])) {
continue;
}
// A required provider on its domains turns its password off - an administrator keeps theirs
$required = isset($via_imap_types[$ut]) ? null : identityProviderRequiredFor($email_raw, $ut, intval($r['user_role_id'] ?? 0));
if ($required) {
$required_provider = $required;
continue;
}
} else {
@@ -315,7 +387,18 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
}
}
if ($agentRow === null && $clientRow === null) {
if ($agentRow === null && $clientRow === null && $required_provider) {
header("HTTP/1.1 401 Unauthorized");
// The password was right, but this account signs in through its provider
$failed_why = signInFailureReason($email_raw ?? '');
signInLogFailure($failed_why['type'], 'Failed', escapeSql('Failed sign in as ' . ($email_raw ?? '') . ' - it signs in with ' . $required_provider['identity_provider_name'] . ', not a password'), $failed_why['user_id'], $failed_why['client_id']);
$response = "
<div class='alert alert-warning'>
Sign in with " . escapeHtml($required_provider['identity_provider_name']) . " - your account doesn't sign in with a password here.
</div>";
} elseif ($agentRow === null && $clientRow === null) {
header("HTTP/1.1 401 Unauthorized");
// Why, and whose account it was - so it counts against the account too
@@ -344,6 +427,20 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
// Show role choice screen
$show_role_choice = true;
// Straight back from a provider - stash who it can be, as for a password; the vault stays locked
if ($is_provider_step) {
$_SESSION['pending_dual_login'] = [
'email' => $email,
'agent_user_id' => intval($agentRow['user_id']),
'client_user_id' => intval($clientRow['contact_user_id'] ?? 0) ?: intval($clientRow['user_id'] ?? 0),
'agent_vault' => false,
'via' => 'provider',
'provider' => $via_provider,
'token' => bin2hex(random_bytes(32)),
'created' => time()
];
}
// If this is the first time (Step 1), we need to stash allowed ids and open the agent's vault
// WITHOUT storing password.
if ($is_login_step) {
@@ -359,6 +456,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
'agent_user_id' => intval($agentRow['user_id']),
'client_user_id' => intval($clientRow['contact_user_id'] ?? 0) ?: intval($clientRow['user_id'] ?? 0),
'agent_vault' => $agent_vault,
'via_imap' => $via_imap_types,
'token' => $pending_token,
'created' => time()
];
@@ -379,6 +477,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
// Proceed if selected
if ($selectedRow !== null && $selectedType !== null) {
// Whether the mail server checked this account's password (an IMAP provider)
$via_imap = $via_imap_types[$selectedType] ?? null;
// Cache pending sessions BEFORE unsetting anything (needed for role->MFA and MFA success)
$pending_dual = $_SESSION['pending_dual_login'] ?? null;
$pending_mfa = $_SESSION['pending_mfa_login'] ?? null;
@@ -476,6 +577,17 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
$extended_log = "with a passkey ($passkey_name)";
}
// So is an identity provider - its own two-step sign-in stands in for ours
if ($via_provider) {
$mfa_is_complete = true;
$extended_log = 'with ' . escapeSql($via_provider['name']);
}
// The mail server checked the password - the two-step code is still ours to ask
if ($via_imap && $mfa_is_complete) {
$extended_log = trim('with their mail password (' . escapeSql($via_imap['name']) . ") $extended_log");
}
if ($mfa_is_complete) {
// Remember me token creation
@@ -529,10 +641,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
$_SESSION['logged'] = true;
// A later password change ends this session (userSessionStamp())
$_SESSION['user_session_stamp'] = userSessionStamp($selectedRow['user_password'] ?? '');
// A passkey sign-in is two-step already - MFA enforcement leaves it be (includes/load_user_session.php)
$_SESSION['signed_in_with'] = $is_passkey_step ? 'passkey' : 'password';
// A passkey or provider sign-in is two-step already - MFA enforcement leaves it be (includes/load_user_session.php)
$_SESSION['signed_in_with'] = $via_provider ? 'provider' : ($is_passkey_step ? 'passkey' : ($via_imap ? 'imap' : 'password'));
$_SESSION['signed_in_via'] = ($via_provider ?: $via_imap)['name'] ?? '';
$mfa_to_set_up = $force_mfa == 1 && $token == NULL && !$is_passkey_step;
$mfa_to_set_up = $force_mfa == 1 && $token == NULL && !$is_passkey_step && !$via_provider;
if ($mfa_to_set_up) {
$config_start_page = "user/mfa_enforcement.php";
}
@@ -540,7 +653,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
// The vault session WITHOUT PASSWORD IN SESSION - opened at step 1, while the password was here (vaultSignIn()).
// After the role choice or two-step code it is already there; signing straight in, it is opened now.
// With a passkey it was opened by login_passkey.php, when the passkey could - or is unlocked later (vaultUnlock()).
if (!$is_mfa_step && !$is_role_step && !$is_passkey_step) {
// After a provider it stays locked - they never typed their password here
if (!$is_mfa_step && !$is_role_step && !$is_passkey_step && !$via_provider) {
vaultSignIn($user_id, $user_encryption_ciphertext, $password);
}
@@ -582,6 +696,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
'email' => $user_email,
'agent_user_id' => $user_id,
'agent_vault' => $agent_vault,
'via_imap' => $via_imap,
'token' => $pending_mfa_token,
// Keep the original issue time on a retry so the pending
// window actually expires, instead of being pushed forward
@@ -655,15 +770,20 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
// a remembered browser, or a code from their app. Wrong codes are
// counted per account, as an agent's are.
$client_mfa_secret = trim((string) ($selectedRow['user_token'] ?? ''));
// A passkey is two-step sign-in in itself
$client_mfa_done = $client_mfa_secret === '' || $is_passkey_step;
$client_mfa_how = $is_passkey_step ? " with a passkey ($passkey_name)" : '';
// A passkey is two-step sign-in in itself, and so is an identity provider
$client_mfa_done = $client_mfa_secret === '' || $is_passkey_step || $via_provider;
$client_mfa_how = $via_provider ? ' with ' . escapeSql($via_provider['name']) : ($is_passkey_step ? " with a passkey ($passkey_name)" : '');
if ($via_imap) {
$client_mfa_how = ' with their mail password (' . escapeSql($via_imap['name']) . ')' . $client_mfa_how;
}
// A provider signs in a contact set to sign in only that way, too (user_auth_method azure)
$client_can_sign_in = $user_auth_method === 'local' || ($via_provider && $user_auth_method === 'azure');
$client_mfa_locked = false;
$client_code = preg_replace('/\s+/', '', (string) ($_POST['current_code'] ?? ''));
if (!preg_match('/^[0-9]{6}$/', $client_code)) {
$client_code = '';
}
if ($client_id && $contact_id && $user_auth_method === 'local' && !$client_mfa_done) {
if ($client_id && $contact_id && $client_can_sign_in && !$client_mfa_done) {
if (isset($_COOKIE['rememberme'])) {
$remember_tokens = mysqli_query($mysqli, "SELECT remember_token_token FROM remember_tokens
WHERE remember_token_user_id = $user_id AND remember_token_created_at > (NOW() - INTERVAL $config_login_remember_me_expire DAY)");
@@ -690,7 +810,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
}
}
if ($client_id && $contact_id && $user_auth_method === 'local' && !$client_mfa_done) {
if ($client_id && $contact_id && $client_can_sign_in && !$client_mfa_done) {
// Ask for the code - keeping only who it is for, never the password
$pending_mfa_token = bin2hex(random_bytes(32));
@@ -698,6 +818,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
'type' => 'client',
'email' => $user_email,
'client_user_id' => $user_id,
'via_imap' => $via_imap,
'token' => $pending_mfa_token,
'created' => ($is_mfa_step && !empty($pending_mfa['created'])) ? intval($pending_mfa['created']) : time(),
];
@@ -714,7 +835,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
$response = "<div class='alert alert-danger'>That code is not right - try the newest one in your app.</div>";
}
} elseif ($client_id && $contact_id && $user_auth_method === 'local') {
} elseif ($client_id && $contact_id && $client_can_sign_in) {
// Remember this browser for two-step sign-in, when asked
if ($client_mfa_secret !== '' && isset($_POST['remember_me']) && $is_mfa_step) {
@@ -732,8 +853,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
$_SESSION['user_id'] = $user_id;
$_SESSION['user_type'] = 2;
$_SESSION['contact_id'] = $contact_id;
$_SESSION['login_method'] = "local";
$_SESSION['signed_in_with'] = $is_passkey_step ? 'passkey' : 'password';
$_SESSION['login_method'] = $via_provider ? 'provider' : 'local';
$_SESSION['signed_in_with'] = $via_provider ? 'provider' : ($is_passkey_step ? 'passkey' : ($via_imap ? 'imap' : 'password'));
$_SESSION['signed_in_via'] = ($via_provider ?: $via_imap)['name'] ?? '';
// A later password change ends this session (userSessionStamp())
$_SESSION['user_session_stamp'] = userSessionStamp($selectedRow['user_password'] ?? '');
@@ -759,7 +881,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_POST['login']) || isset($_
} else {
// The account is there, but can't sign in here with a password
signInLogFailure('Client Login', 'Failed', "Failed sign in as $email - " . ($user_auth_method === 'azure' ? 'the account signs in with Microsoft 365' : 'the account has no contact or client'), $user_id, $client_id ?? 0);
signInLogFailure('Client Login', 'Failed', "Failed sign in as $email - " . ($user_auth_method === 'azure' ? 'the account signs in with an identity provider, not a password' : 'the account has no contact or client'), $user_id, $client_id ?? 0);
header("HTTP/1.1 401 Unauthorized");
$response = "
@@ -920,6 +1042,24 @@ $show_login_form = (!$show_role_choice && !$show_mfa_form);
</form>
<?php if ($show_login_form && $login_providers) {
// Carried through the provider and back - the agents' sign-in key, and where they were going
$provider_carry = array_filter(['key' => is_string($_GET['key'] ?? null) ? $_GET['key'] : '', 'last_visited' => is_string($_GET['last_visited'] ?? null) ? $_GET['last_visited'] : '']); ?>
<!-- Signing in through an identity provider (login_provider.php) -->
<div data-login-providers>
<div class="d-flex align-items-center gap-2 mb-3 small text-body-secondary">
<hr class="flex-grow-1 my-0"><span>or</span><hr class="flex-grow-1 my-0">
</div>
<?php foreach ($login_providers as $login_provider) {
$login_provider_kind = identityProviderKind($login_provider);
$login_provider_url = ($login_provider['identity_provider_redirect_path'] ?: '/login_provider.php') . '?' . http_build_query(['provider' => intval($login_provider['identity_provider_id'])] + $provider_carry); ?>
<a class="btn btn-outline-secondary w-100 mb-2" href="<?= escapeHtml($login_provider_url) ?>">
<i class="<?= escapeHtml($login_provider_kind['icon']) ?> fa-fw me-2"></i><?= escapeHtml(count($login_providers) > 1 || $login_provider_kind['name'] !== $login_provider['identity_provider_name'] ? 'Sign in with ' . $login_provider['identity_provider_name'] : $login_provider_kind['button']) ?>
</a>
<?php } ?>
</div>
<?php } ?>
<?php if ($show_login_form && $config_login_passkeys === 1) { ?>
<!-- Sign in with a passkey (js/passkey.js, login_passkey.php) - shown once the browser is known to do passkeys -->
<div data-passkey-sign-in data-endpoint="login_passkey.php" hidden>
@@ -948,13 +1088,6 @@ $show_login_form = (!$show_role_choice && !$show_mfa_form);
<a href="client/signup.php" data-login-signup><i class="fas fa-user-plus me-1"></i>Create an account</a>
<?php } ?>
</div>
<?php if (!empty($azure_client_id)) { ?>
<div class="col text-center mt-2">
<a href="client/login_microsoft.php">
<button type="button" class="btn btn-secondary">Login with Microsoft Entra</button>
</a>
</div>
<?php } ?>
<?php } ?>
<?php if (kbPublicOn()) { ?>