Files
itflow/admin/modals/identity_provider/identity_provider_edit.php

230 lines
17 KiB
PHP

<?php
/*
* Admin > Identity Providers - one identity provider's window: Add (?kind=, no id) or Edit (?id=).
* Which kind it is is picked from the New menu and stays. How to set it up on the provider's side
* is at the top, with the redirect address to register there. The secret is never put back into
* the page - left empty, it stays as saved. Saved by save_identity_provider
* (admin/post/identity_provider.php).
*/
require_once '../../includes/modal_header.php';
if (!identityProvidersReady()) {
echo json_encode(['error' => 'Identity providers arrive with a database update - run it in Maintenance > Update first.']);
exit;
}
$identity_provider_id = intval($_GET['id'] ?? 0);
$row = $identity_provider_id ? identityProviderRow($identity_provider_id) : null;
if ($identity_provider_id && !$row) {
echo json_encode(['error' => 'That identity provider no longer exists.']);
exit;
}
$kinds = identityProviderKinds();
$kind_key = $row ? (string) $row['identity_provider_kind'] : (string) ($_GET['kind'] ?? '');
if (!isset($kinds[$kind_key])) {
echo json_encode(['error' => $row ? 'This ITFlow doesn\'t know that identity provider\'s kind.' : 'Pick which kind of identity provider to add.']);
exit;
}
$kind = $kinds[$kind_key];
$fields = $kind['fields'];
$value = function ($column, $default = '') use ($row) {
return $row ? (string) ($row["identity_provider_$column"] ?? $default) : $default;
};
$secret_saved = $row && trim((string) $row['identity_provider_client_secret']) !== '';
$redirect_uri = identityProviderRedirectUri($row ?: []);
$form_id = 'identityProvider' . ($identity_provider_id ?: 'New');
$security = $value('security', 'ssl');
ob_start();
?>
<div class="modal-header bg-dark">
<h5 class="modal-title"><i class="<?= escapeHtml($kind['icon']) ?> fa-fw me-2"></i><?= $row ? '<strong>' . escapeHtml($row['identity_provider_name']) . '</strong>' : 'New ' . escapeHtml($kind['name']) . ' Identity Provider' ?></h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="post.php" method="post" autocomplete="off" data-identity-provider-form="<?= escapeHtml($kind_key) ?>">
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
<input type="hidden" name="identity_provider_id" value="<?= $identity_provider_id ?>">
<input type="hidden" name="kind" value="<?= escapeHtml($kind_key) ?>">
<div class="modal-body">
<div class="alert alert-light border small mb-3" data-identity-provider-setup>
<div class="fw-semibold mb-1"><i class="fas fa-fw fa-info-circle me-1"></i>Setting it up</div>
<?php if ($kind_key === 'microsoft') { ?>
In the Microsoft Entra admin center, under App registrations, make a New registration for accounts in any organizational directory (or only yours), with a Web redirect address of the one below. Copy its Application (client) ID here, then under Certificates &amp; secrets make a client secret and copy its Value. The Microsoft Graph User.Read permission it starts with is all it needs.
<?php } elseif ($kind_key === 'google') { ?>
In the Google Cloud console, under APIs &amp; Services &gt; Credentials, create an OAuth client ID for a Web application, with an authorized redirect address of the one below, and copy its Client ID and Client secret here. Make its OAuth consent screen Internal to keep it to your own Google Workspace.
<?php } elseif ($kind_key === 'nextcloud') { ?>
Install the OIDC Identity Provider app (oidc) on your Nextcloud. In its Administration settings, under OpenID Connect, add a client with the redirect address below, of the Confidential type, and copy its client identifier and secret here.
<div class="mt-1"><i class="fas fa-fw fa-exclamation-triangle text-warning me-1"></i>ITFlow goes by the email address Nextcloud gives - make sure its users can't change their own, or one of them could sign in here as another.</div>
<?php } else { ?>
Nothing is set up on the mail server: the password typed on the sign-in page is tried there, in place of the ITFlow password, and the ITFlow two-step code is still asked for. An agent with an administrator role can still use their ITFlow password, for when the mail server is down. Use a server that takes TLS, and list only domains whose mailboxes are on it.
<?php } ?>
<?php if ($kind['oauth']) { ?>
<div class="input-group input-group-sm mt-2">
<span class="input-group-text"><i class="fas fa-fw fa-link"></i></span>
<input type="text" class="form-control font-monospace" id="<?= $form_id ?>Redirect" value="<?= escapeHtml($redirect_uri) ?>" readonly aria-label="Redirect address" data-identity-provider-redirect>
<button type="button" class="btn btn-outline-secondary clipboardjs" data-clipboard-target="#<?= $form_id ?>Redirect" title="Copy"><i class="far fa-fw fa-copy"></i></button>
</div>
<?php } ?>
</div>
<div class="mb-3">
<div class="form-check form-switch">
<input type="checkbox" class="form-check-input" name="enabled" value="1" id="<?= $form_id ?>Enabled" <?= !$row || intval($row['identity_provider_enabled']) ? 'checked' : '' ?>>
<label class="form-check-label" for="<?= $form_id ?>Enabled">On<?= $kind['oauth'] ? ' - its button is on the sign-in page' : ' - it checks the passwords on its email domains' ?></label>
</div>
</div>
<div class="mb-3">
<label class="form-label" for="<?= $form_id ?>Name">Name <strong class="text-danger">*</strong></label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-tag"></i></span>
<input type="text" class="form-control" name="name" id="<?= $form_id ?>Name" maxlength="100" required value="<?= escapeHtml($value('name', $kind['name'])) ?>" placeholder="What you call it">
</div>
<div class="form-text"><?= $kind['oauth'] ? 'Its button on the sign-in page says &quot;Sign in with&quot; and this name.' : 'What the audit log calls it.' ?></div>
</div>
<div class="itflow-form-section">Who it signs in</div>
<div class="mb-3">
<div class="d-flex flex-wrap gap-4">
<div class="form-check form-switch">
<input type="checkbox" class="form-check-input" name="agents" value="1" id="<?= $form_id ?>Agents" <?= $value('agents', '1') === '1' ? 'checked' : '' ?>>
<label class="form-check-label" for="<?= $form_id ?>Agents"><i class="fas fa-fw fa-user-tie text-secondary me-1"></i>Agents</label>
</div>
<div class="form-check form-switch">
<input type="checkbox" class="form-check-input" name="contacts" value="1" id="<?= $form_id ?>Contacts" <?= $value('contacts', '0') === '1' ? 'checked' : '' ?>>
<label class="form-check-label" for="<?= $form_id ?>Contacts"><i class="fas fa-fw fa-user-friends text-secondary me-1"></i>Client contacts</label>
</div>
</div>
<div class="form-text">
Only accounts already in ITFlow - an agent, or a contact with portal access - with the same email address. No one new is added.
<?php if (!$config_client_portal_enable) { ?>The client portal is off, so it signs in agents only until it is on.<?php } ?>
</div>
</div>
<div class="mb-3">
<label class="form-label" for="<?= $form_id ?>Domains">Email domains<?= $kind['domains_optional'] ? '' : ' <strong class="text-danger">*</strong>' ?></label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-at"></i></span>
<input type="text" class="form-control" name="domains" id="<?= $form_id ?>Domains" maxlength="1000" spellcheck="false" <?= $kind['domains_optional'] ? '' : 'required' ?> value="<?= escapeHtml($value('domains')) ?>" placeholder="example.com, example.org">
</div>
<div class="form-text">
<?php if ($kind['domains_optional']) { ?>
The accounts it signs in are the ones on these domains. Empty, it signs in any <?= escapeHtml($kind['name']) ?> account whose address is in ITFlow - <?= escapeHtml($kind_key === 'microsoft' ? 'Microsoft' : 'Google') ?> vouches for it.
<?php } else { ?>
The accounts it signs in are the ones on these domains - <?= $kind_key === 'imap' ? 'list only domains whose mailboxes are all on this server.' : 'a Nextcloud\'s users can set their own email address, so list only domains whose people are all on yours.' ?>
<?php } ?>
</div>
</div>
<?php if ($kind['oauth']) { ?>
<div class="mb-3">
<div class="form-check form-switch">
<input type="checkbox" class="form-check-input" name="required" value="1" id="<?= $form_id ?>Required" <?= $value('required', '0') === '1' ? 'checked' : '' ?>>
<label class="form-check-label" for="<?= $form_id ?>Required">Required - the accounts on its email domains can't sign in with a password</label>
</div>
<div class="form-text ps-5">Passkeys still work, and an agent with an administrator role keeps their password, to get back in when it is down. Needs email domains.</div>
</div>
<?php } ?>
<div class="itflow-form-section"><?= $kind['oauth'] ? 'The app' : 'The mail server' ?></div>
<?php if ($kind_key === 'nextcloud') { ?>
<div class="mb-3">
<label class="form-label" for="<?= $form_id ?>Url">Address <strong class="text-danger">*</strong></label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-globe"></i></span>
<input type="url" class="form-control" name="url" id="<?= $form_id ?>Url" maxlength="500" spellcheck="false" required value="<?= escapeHtml($value('url')) ?>" placeholder="https://cloud.example.com">
</div>
<div class="form-text">Where your Nextcloud is. ITFlow asks it for its OpenID Connect settings when you save.</div>
</div>
<?php } ?>
<?php if (in_array('client_id', $fields, true)) { ?>
<div class="row g-3 mb-3">
<div class="col-md-6">
<label class="form-label" for="<?= $form_id ?>ClientId"><?= $kind_key === 'microsoft' ? 'Application (client) ID' : 'Client ID' ?> <strong class="text-danger">*</strong></label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-fingerprint"></i></span>
<input type="text" class="form-control font-monospace" name="client_id" id="<?= $form_id ?>ClientId" maxlength="255" spellcheck="false" required value="<?= escapeHtml($value('client_id')) ?>"
placeholder="<?= $kind_key === 'microsoft' ? 'e721e3b6-01d6-50e8-7f22-c84d951a52e7' : ($kind_key === 'google' ? '…apps.googleusercontent.com' : 'From its client') ?>">
</div>
</div>
<div class="col-md-6">
<label class="form-label" for="<?= $form_id ?>Secret">Client secret <?= $secret_saved ? '' : '<strong class="text-danger">*</strong>' ?></label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-key"></i></span>
<input type="password" class="form-control" name="client_secret" id="<?= $form_id ?>Secret" maxlength="1000" autocomplete="new-password" <?= $secret_saved ? '' : 'required' ?>
placeholder="<?= $secret_saved ? 'Saved - type a new one to replace it' : ($kind_key === 'microsoft' ? 'Its Value, not its ID' : 'From its client') ?>">
</div>
<?php if ($kind_key === 'microsoft') { ?><div class="form-text">A secret runs out - put the new one here before it does.</div><?php } ?>
</div>
</div>
<?php } ?>
<?php if (in_array('tenant', $fields, true)) { ?>
<div class="mb-3">
<label class="form-label" for="<?= $form_id ?>Tenant">Directory (tenant) ID</label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-building"></i></span>
<input type="text" class="form-control font-monospace" name="tenant" id="<?= $form_id ?>Tenant" maxlength="100" spellcheck="false" value="<?= escapeHtml($value('tenant')) ?>" placeholder="Any organization">
</div>
<div class="form-text">Only for an app registered for your directory alone - its ID, from the app's Overview. Empty takes work accounts from any organization; personal Microsoft accounts never sign in.</div>
</div>
<?php } ?>
<?php if ($kind_key === 'imap') { ?>
<div class="row g-3 mb-3">
<div class="col-md-8">
<label class="form-label" for="<?= $form_id ?>Host">Server <strong class="text-danger">*</strong></label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-server"></i></span>
<input type="text" class="form-control" name="url" id="<?= $form_id ?>Host" maxlength="255" spellcheck="false" required value="<?= escapeHtml($value('url')) ?>" placeholder="mail.example.com">
</div>
</div>
<div class="col-md-4">
<label class="form-label" for="<?= $form_id ?>Port">Port</label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-plug"></i></span>
<input type="number" class="form-control" name="port" id="<?= $form_id ?>Port" min="1" max="65535" value="<?= $row ? intval($row['identity_provider_port']) : '' ?>" placeholder="993">
</div>
</div>
<div class="col-md-6">
<label class="form-label" for="<?= $form_id ?>Security">Security</label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-lock"></i></span>
<select class="form-select select2" name="security" id="<?= $form_id ?>Security">
<?php foreach (['ssl' => 'SSL/TLS', 'starttls' => 'STARTTLS', 'none' => 'None - the password goes unencrypted'] as $option => $label) { ?>
<option value="<?= $option ?>" <?= $security === $option ? 'selected' : '' ?><?= selectOptionData(['icon' => $option === 'none' ? 'fas fa-lock-open' : 'fas fa-lock']) ?>><?= escapeHtml($label) ?></option>
<?php } ?>
</select>
</div>
<div class="form-text">Empty port is 993 for SSL/TLS, 143 otherwise. Its certificate has to be a valid one.</div>
</div>
<div class="col-md-6">
<label class="form-label" for="<?= $form_id ?>Username">It is signed in to with</label>
<div class="input-group">
<span class="input-group-text"><i class="fas fa-fw fa-user"></i></span>
<select class="form-select select2" name="username" id="<?= $form_id ?>Username">
<option value="email" <?= $value('username', 'email') !== 'local' ? 'selected' : '' ?><?= selectOptionData(['icon' => 'fas fa-at', 'description' => 'name@example.com']) ?>>The whole email address</option>
<option value="local" <?= $value('username') === 'local' ? 'selected' : '' ?><?= selectOptionData(['icon' => 'fas fa-user', 'description' => 'name']) ?>>The part before the @</option>
</select>
</div>
</div>
</div>
<?php } ?>
</div>
<div class="modal-footer">
<button type="submit" name="save_identity_provider" class="btn btn-primary fw-bold"><i class="fas fa-check me-2"></i><?= $row ? 'Save' : 'Create' ?></button>
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fas fa-times me-2"></i>Cancel</button>
</div>
</form>
<?php
require_once '../../../includes/modal_footer.php';