diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8eb5299c1..fc562afb6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,7 +6,7 @@ This file documents all notable changes made to ITFlow.
### Upgrading to 26.12.3
-Update from Maintenance > Update as usual - this release has three database changes: 4.2.2 adds a table for passkeys and a setting to turn them on or off, 4.2.3 lets a document keep the Markdown it was written in, and 4.2.4 lets a ticket keep its pictures as files. Nothing else is required: passkeys are on, and no one has one until they add it. Pictures already inside tickets move to files in the background afterwards - the new Ticket Pictures job in Maintenance > Cron does a batch a minute, so an install with years of emailed screenshots may take a while, and tickets work as usual meanwhile. A passkey is made for your ITFlow's address (the one in config.php) and only works there, over HTTPS - if you move ITFlow to a new address, everyone adds theirs again.
+Update from Maintenance > Update as usual - this release has four database changes: 4.2.2 adds a table for passkeys and a setting to turn them on or off, 4.2.3 lets a document keep the Markdown it was written in, 4.2.4 lets a ticket keep its pictures as files, and 4.2.5 adds identity providers. The client portal's Microsoft 365 single sign-on becomes the first identity provider, for client contacts, and keeps its redirect address, so nothing changes in Microsoft Entra. Nothing else is required: passkeys are on, and no one has one until they add it. Pictures already inside tickets move to files in the background afterwards - the new Ticket Pictures job in Maintenance > Cron does a batch a minute, so an install with years of emailed screenshots may take a while, and tickets work as usual meanwhile. A passkey is made for your ITFlow's address (the one in config.php) and only works there, over HTTPS - if you move ITFlow to a new address, everyone adds theirs again.
### New Features & Updates
@@ -29,6 +29,11 @@ Update from Maintenance > Update as usual - this release has three database chan
- Pictures already inside tickets move to files in the background - the new Ticket Pictures job in Maintenance > Cron does a batch every minute until every ticket is done, then keeps an eye on new ones.
- Taking a picture out of a reply or the ticket's details when editing it, or redacting it, deletes it - it no longer opens from an old link. Redact now takes a picture selected on its own, as well as text.
- A picture from an internal note put in a reply the client sees, or from a ticket merged into another, is copied, so it shows for the client and stays when the other is deleted.
+- Identity providers: agents and client contacts can sign in with their Microsoft 365, Google Workspace or Nextcloud account, each a button on the sign-in page, or with their mail password through an IMAP server. Set them up in the new Admin > Identity Providers, under Integrations: each is for agents, client contacts or both, and for the email domains you list. Only people already in ITFlow sign in - an agent, or a contact with portal access, with the same email address - and no one new is added.
+- Microsoft 365, Google Workspace and Nextcloud do their own sign-in checks, so ITFlow doesn't ask for its two-step code after them, and they meet a role's or the client portal's requirement for it. A mail password (IMAP) is still asked for the code.
+- An identity provider can be required: the accounts on its email domains then can't sign in with a password, and the sign-in page says which button to use. Passkeys still work, and an agent with an administrator role keeps their password, to get back in if the provider is down.
+- An agent who signs in with an identity provider or their mail password finds their credentials locked, as after some passkeys: the first username or password they open asks for their ITFlow password, once, until they sign out.
+- Admin > Settings > Single Sign-on is gone: the client portal's Microsoft 365 sign-in is now a Microsoft 365 identity provider. A contact's Login of Only with an identity provider (once called With their Microsoft account) signs in only that way, with no password, and is offered when a provider is on for client contacts.
### Bug Fixes
@@ -59,6 +64,10 @@ Update from Maintenance > Update as usual - this release has three database chan
- A list table with .itflow-list-stack (css/itflow_custom.css) stacks below 768px: each row a grid card with the tick box, first cell and last (the menu) across the top and every other cell under them labelled from its data-label, and the head a wrapping row of its sort links. Used on agent/ticket_list.php, agent/credentials.php, agent/assets.php and agent/contacts.php (contacts and invites); the ticket row's phone-only line of status, priority and client is gone, as the card shows them.
- agent/modals/document/document_view.php uses .itflow-prose and linkGroupsHtml()'s compact mode, which takes $open_up_to (5 by default, 0 for the quick view: always closed). Its Edit goes to agent/document_edit.php, so agent/modals/document/document_edit.php, which only it opened, is gone.
- A document's editor is .tinymce-document (agent/modals/document/document_form.php; .tinymce-page on agent/document_edit.php): js/app.js itflowDocumentWriter, the document editor with its own toolbar and itflowProseContentStyle() - .itflow-prose as literal CSS, as the editor is an iframe. The page's editor saves on Ctrl+S (addShortcut), the fields and Markdown box through a keydown on the page; the Markdown Preview has .itflow-prose; Markdown pasted into the editor (js/document_markdown.js) takes .tinymce-document too. Other editors (.tinymce) are as they were.
+- Database 4.2.5: identity_providers (kind microsoft, google, nextcloud or imap; name, on, agents, contacts, domains, required, client id and secret, tenant, url, port, security, username, redirect_path, used_at). settings.config_azure_client_id and config_azure_client_secret are copied to a microsoft provider for contacts with redirect_path /client/login_microsoft.php, then dropped; users.user_auth_method azure now means only through an identity provider.
+- functions/identity_provider.php: the kinds, who a provider covers (identityProviderCovers(), identityProviderFor(), identityProviderRequiredFor(), identityProviderImapFor()), the OpenID Connect code flow with PKCE S256 (identityProviderEndpoints() - Microsoft and Google fixed, Nextcloud from its discovery document; identityProviderAuthorizeUrl(), identityProviderIdentity()) and the IMAP check (identityProviderImapCheck(), DirectoryTree ImapEngine, certificate checked). Microsoft's address is Graph /me userPrincipalName (guests refused), Google's needs email_verified.
+- login_provider.php starts a sign-in (?provider=N, a single-use state in the session, ten minutes) and takes the code back, then hands the account to login.php as ?provider_login= with $_SESSION['pending_provider_login'], which signs in through its usual success path - role choice included - with $_SESSION['signed_in_with'] = 'provider' (left out of MFA enforcement as a passkey is) and signed_in_via the provider's name. client/login_microsoft.php serves it for the migrated app's redirect address. IMAP is checked in login.php's password step ('imap'); an administrator's own password still works there.
+- Admin: admin/identity_providers.php (list), admin/modals/identity_provider/identity_provider_edit.php (Add and Edit), admin/post/identity_provider.php (save_identity_provider, turn_identity_provider_on / off, delete_identity_provider - locked in the demo). The identity settings page and edit_identity_provider are gone. portalSignInMethods() and identityProviderContactsWording() take an email, to offer only the providers that sign it in; vaultLockedReason() says why the vault is locked.
## [26.12.2]
diff --git a/admin/database_updates/4.2.5.php b/admin/database_updates/4.2.5.php
new file mode 100644
index 000000000..65674690c
--- /dev/null
+++ b/admin/database_updates/4.2.5.php
@@ -0,0 +1,62 @@
+ Identity Providers): agents and
+ * client contacts signing in with Microsoft 365, Google Workspace, Nextcloud or their mail
+ * password (IMAP). identity_providers - each one's kind and name, whether it is on, who it signs
+ * in (agents, contacts) and on which email domains, whether it is required, its app's ID and
+ * secret, its tenant (Microsoft), address (Nextcloud, or the IMAP server with its port, security
+ * and username style), the path it sends people back to, and when it was last used.
+ *
+ * The Microsoft Entra app the client portal signed in with (settings.config_azure_client_id and
+ * config_azure_client_secret) becomes the first provider, for client contacts on any domain, still
+ * sending them back to /client/login_microsoft.php - the address registered with it - and the two
+ * settings go. Contacts set to sign in with Microsoft keep that (users.user_auth_method azure),
+ * now meaning through an identity provider, without a password. Re-runnable.
+ */
+
+defined('FROM_DB_UPDATER') || die("Direct file access is not allowed");
+
+ mysqli_query($mysqli, "CREATE TABLE IF NOT EXISTS `identity_providers` (
+ `identity_provider_id` int(11) NOT NULL AUTO_INCREMENT,
+ `identity_provider_kind` varchar(20) NOT NULL,
+ `identity_provider_name` varchar(100) NOT NULL,
+ `identity_provider_enabled` tinyint(1) NOT NULL DEFAULT 1,
+ `identity_provider_agents` tinyint(1) NOT NULL DEFAULT 0,
+ `identity_provider_contacts` tinyint(1) NOT NULL DEFAULT 0,
+ `identity_provider_domains` varchar(1000) NOT NULL DEFAULT '',
+ `identity_provider_required` tinyint(1) NOT NULL DEFAULT 0,
+ `identity_provider_client_id` varchar(255) NOT NULL DEFAULT '',
+ `identity_provider_client_secret` varchar(1000) NOT NULL DEFAULT '',
+ `identity_provider_tenant` varchar(100) NOT NULL DEFAULT '',
+ `identity_provider_url` varchar(500) NOT NULL DEFAULT '',
+ `identity_provider_port` int(11) NOT NULL DEFAULT 0,
+ `identity_provider_security` varchar(10) NOT NULL DEFAULT '',
+ `identity_provider_username` varchar(10) NOT NULL DEFAULT '',
+ `identity_provider_redirect_path` varchar(100) NOT NULL DEFAULT '/login_provider.php',
+ `identity_provider_used_at` datetime DEFAULT NULL,
+ `identity_provider_created_at` datetime NOT NULL DEFAULT current_timestamp(),
+ `identity_provider_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
+ PRIMARY KEY (`identity_provider_id`)
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci");
+
+ // The client portal's Microsoft Entra app, as the first provider - once
+ $itflow_azure_column = mysqli_query($mysqli, "SELECT 1 FROM information_schema.COLUMNS
+ WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'settings' AND COLUMN_NAME = 'config_azure_client_id' LIMIT 1");
+ if ($itflow_azure_column && mysqli_num_rows($itflow_azure_column) > 0) {
+ $itflow_azure = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT config_azure_client_id, config_azure_client_secret FROM settings WHERE company_id = 1"));
+ $itflow_azure_id = trim((string) ($itflow_azure['config_azure_client_id'] ?? ''));
+ $itflow_azure_secret = trim((string) ($itflow_azure['config_azure_client_secret'] ?? ''));
+ $itflow_providers = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM identity_providers"))[0] ?? 0);
+ if ($itflow_azure_id !== '' && $itflow_providers === 0) {
+ mysqli_query($mysqli, "INSERT INTO identity_providers SET identity_provider_kind = 'microsoft', identity_provider_name = 'Microsoft 365',
+ identity_provider_enabled = 1, identity_provider_agents = 0, identity_provider_contacts = 1,
+ identity_provider_client_id = '" . mysqli_real_escape_string($mysqli, $itflow_azure_id) . "',
+ identity_provider_client_secret = '" . mysqli_real_escape_string($mysqli, $itflow_azure_secret) . "',
+ identity_provider_redirect_path = '/client/login_microsoft.php'");
+ }
+ mysqli_query($mysqli, "ALTER TABLE `settings` DROP COLUMN `config_azure_client_id`, DROP COLUMN `config_azure_client_secret`");
+ }
diff --git a/admin/identity_providers.php b/admin/identity_providers.php
index 6f2ad488e..3a251743b 100644
--- a/admin/identity_providers.php
+++ b/admin/identity_providers.php
@@ -1,56 +1,205 @@
Settings > Single sign-on - client contacts signing in to the
- * portal with their Microsoft 365 account (client/login_microsoft.php),
- * through an app registered in Microsoft Entra. The secret is never put back
- * into the page: left empty it stays as saved. Saved by
- * edit_identity_provider (admin/post/identity_provider.php).
+ * Admin > Identity Providers - the ways agents and client contacts sign in besides their ITFlow
+ * password (functions/identity_provider.php): Microsoft 365, Google Workspace and Nextcloud, each
+ * a button on the sign-in page, and IMAP, which checks the password typed there with a mail
+ * server. Each row says what it is, who it signs in and on which email domains, whether it is
+ * required, and when it was last used. New is a menu of the kinds; Add and Edit are one window
+ * (modals/identity_provider/identity_provider_edit.php); the rest are
+ * admin/post/identity_provider.php.
*/
+// Default Column Sortby Filter
+$sort = "identity_provider_name";
+$order = "ASC";
+
require_once "includes/inc_all_admin.php";
-$redirect_uri = 'https://' . preg_replace('#^https?://#', '', rtrim((string) $config_base_url, '/')) . '/client/login_microsoft.php';
-$secret_saved = trim((string) $config_azure_client_secret) !== '';
+// The table arrives with the 4.2.5 database update
+if (!identityProvidersReady()) { ?>
+
Identity Providers arrive with a database update - run it in Maintenance > Update first.
+ "identity_provider_name $order",
+ 'identity_provider_kind' => "identity_provider_kind $order, identity_provider_name ASC",
+ 'identity_provider_used_at' => "identity_provider_used_at IS NULL, identity_provider_used_at $order, identity_provider_name ASC",
+];
+if (!isset($sortable_columns[$sort])) {
+ $sort = 'identity_provider_name';
+}
+
+$sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS * FROM identity_providers
+ WHERE (identity_provider_name LIKE '%$q%' OR identity_provider_domains LIKE '%$q%' OR identity_provider_kind LIKE '%$q%' OR identity_provider_url LIKE '%$q%')
+ ORDER BY {$sortable_columns[$sort]}, identity_provider_id ASC
+ LIMIT $record_from, $record_to");
+
+$num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
+
+$csrf = $_SESSION['csrf_token'];
+$add_url = 'modals/identity_provider/identity_provider_edit.php?kind=';
?>
-