289 lines
14 KiB
PHP
289 lines
14 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require dirname(__DIR__) . '/src/lib.php';
|
|
require dirname(__DIR__) . '/src/help.php';
|
|
require dirname(__DIR__) . '/src/actions.php';
|
|
require dirname(__DIR__) . '/src/paste.php';
|
|
|
|
start_session();
|
|
|
|
if (empty($_SESSION['user'])) {
|
|
page_start('Log in');
|
|
echo '<section class="card card-center">'
|
|
. '<h1>Welcome back</h1>'
|
|
. '<p class="muted">Manage the tinyauth service and its settings. Log in with your tinyauth account.</p>'
|
|
. '<a class="btn btn-primary" href="login.php">Log in with tinyauth</a>'
|
|
. '</section>';
|
|
page_end();
|
|
exit;
|
|
}
|
|
|
|
$groups = $_SESSION['groups'] ?? [];
|
|
$allowedGroup = load_settings()['allowed_group'] ?? '';
|
|
$authorized = is_authorized($groups);
|
|
$modalHtml = null;
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
require_csrf();
|
|
$action = (string) ($_POST['action'] ?? '');
|
|
|
|
if ($allowedGroup === '' && $action === 'setup') {
|
|
handle_setup($groups);
|
|
} elseif (!$authorized) {
|
|
http_response_code(403);
|
|
exit('Forbidden');
|
|
} elseif ($action === 'reset_group') {
|
|
handle_reset_group();
|
|
} elseif (in_array($action, SERVICE_ACTIONS, true)) {
|
|
handle_service($action);
|
|
} elseif ($action === 'save_config') {
|
|
handle_save_config();
|
|
} elseif ($action === 'create_oidc_client') {
|
|
handle_create_oidc_client();
|
|
} elseif ($action === 'create_app_acl') {
|
|
handle_create_app_acl();
|
|
} elseif ($action === 'paste_preview') {
|
|
$modalHtml = paste_preview((string) ($_POST['paste'] ?? ''), csrf_token());
|
|
} elseif ($action === 'paste_apply') {
|
|
handle_paste_apply();
|
|
}
|
|
|
|
if ($modalHtml === null) {
|
|
redirect('index.php');
|
|
}
|
|
}
|
|
|
|
$cfg = config();
|
|
$flashes = take_flashes();
|
|
$lastOutput = $_SESSION['last_output'] ?? '';
|
|
unset($_SESSION['last_output']);
|
|
$token = csrf_token();
|
|
|
|
$userHtml = '<span class="who">' . h($_SESSION['user']) . '</span>'
|
|
. '<a class="btn btn-ghost btn-sm" href="logout.php">Log out</a>';
|
|
page_start('Dashboard', $userHtml);
|
|
|
|
foreach ($flashes as $flash) {
|
|
echo '<div class="flash flash-' . h($flash['type']) . '">' . h($flash['message']) . '</div>';
|
|
}
|
|
if ($modalHtml !== null) {
|
|
echo $modalHtml;
|
|
}
|
|
|
|
if ($allowedGroup === '') {
|
|
echo '<section class="card card-center">';
|
|
echo '<h2>First-time setup ' . gui_help_icon('access_group') . '</h2>';
|
|
echo '<p class="muted">Choose the tinyauth group whose members may use this tool.</p>';
|
|
if ($groups === []) {
|
|
echo '<div class="flash flash-error">Your account has no groups in tinyauth. Add yourself to a group, then reload.</div>';
|
|
} else {
|
|
echo '<form method="post" class="setup-form">';
|
|
echo '<input type="hidden" name="csrf" value="' . h($token) . '">';
|
|
echo '<input type="hidden" name="action" value="setup">';
|
|
echo '<div class="radio-list">';
|
|
foreach ($groups as $group) {
|
|
echo '<label class="radio-row"><input type="radio" name="group" value="' . h($group) . '" required> '
|
|
. h($group) . '</label>';
|
|
}
|
|
echo '</div>';
|
|
echo '<button type="submit" class="btn btn-primary">Save access group</button>';
|
|
echo '</form>';
|
|
}
|
|
echo '</section>';
|
|
page_end();
|
|
exit;
|
|
}
|
|
|
|
if (!$authorized) {
|
|
echo '<section class="card card-center">';
|
|
echo '<h2>Access denied</h2>';
|
|
echo '<p class="muted">You are not in the required tinyauth group <b>' . h($allowedGroup) . '</b>.</p>';
|
|
echo '</section>';
|
|
page_end();
|
|
exit;
|
|
}
|
|
|
|
$perms = [];
|
|
foreach (SERVICE_ACTIONS as $action) {
|
|
$perms[$action] = service_allowed($action);
|
|
}
|
|
$status = $perms['status'] ? service_run('status') : null;
|
|
$state = 'unknown';
|
|
if ($status !== null && preg_match('/Active:\s+(\S+)/', $status['output'], $m)) {
|
|
$state = $m[1];
|
|
}
|
|
$stateClass = match ($state) {
|
|
'active' => 'state-active',
|
|
'failed' => 'state-failed',
|
|
'inactive' => 'state-inactive',
|
|
default => 'state-unknown',
|
|
};
|
|
$service = $cfg['service'];
|
|
|
|
echo '<section class="card">';
|
|
echo '<div class="card-head"><h2>Service <code>' . h($service) . '</code></h2>';
|
|
echo '<span class="state ' . $stateClass . '">' . h($state) . '</span></div>';
|
|
echo '<form method="post" class="btn-row">';
|
|
echo '<input type="hidden" name="csrf" value="' . h($token) . '">';
|
|
foreach (SERVICE_ACTIONS as $action) {
|
|
$disabled = $perms[$action] ? '' : ' disabled';
|
|
echo '<button type="submit" name="action" value="' . h($action) . '" class="btn btn-' . h($action) . '"'
|
|
. $disabled . '>' . h(ucfirst($action)) . '</button>';
|
|
}
|
|
echo '</form>';
|
|
if ($status !== null) {
|
|
echo '<pre>' . h($status['output']) . '</pre>';
|
|
}
|
|
if ($lastOutput !== '') {
|
|
echo '<pre>' . h($lastOutput) . '</pre>';
|
|
}
|
|
echo '</section>';
|
|
|
|
$envPath = $cfg['env_file'];
|
|
$envReadable = is_file($envPath) && is_readable($envPath);
|
|
$envWritable = $envReadable && is_writable($envPath);
|
|
$dataWritable = is_writable(data_dir());
|
|
|
|
echo '<div class="grid-2">';
|
|
echo '<section class="card"><div class="card-head"><h2>Permission checks</h2></div><ul class="checks">';
|
|
foreach (SERVICE_ACTIONS as $action) {
|
|
echo permission_row('sudo: systemctl ' . $action . ' ' . $service, $perms[$action]);
|
|
}
|
|
echo permission_row('Env file readable', $envReadable, $envPath);
|
|
echo permission_row('Env file writable', $envWritable);
|
|
echo permission_row('Data directory writable', $dataWritable);
|
|
echo '</ul>';
|
|
if (in_array(false, $perms, true)) {
|
|
echo '<p class="notice">Missing sudo rules. Install <code>sudoers/tinyauth-web.sudoers</code> '
|
|
. 'from this repo and check it with <code>visudo -cf</code>.</p>';
|
|
}
|
|
echo '<form method="post" class="form-actions"><input type="hidden" name="csrf" value="' . h($token) . '">';
|
|
echo '<span class="muted">Current group: <code>' . h($allowedGroup) . '</code> ' . gui_help_icon('access_group') . '</span>';
|
|
echo '<button type="submit" name="action" value="reset_group" class="btn btn-ghost btn-sm">Change access group</button>';
|
|
echo '</form>';
|
|
echo '</section>';
|
|
|
|
echo '<section class="card"><div class="card-head"><h2>Quick notes</h2></div>';
|
|
echo '<ul class="muted notes">';
|
|
echo '<li>Saving the config writes a backup first. ' . gui_help_icon('backup') . '</li>';
|
|
echo '<li>Changes take effect after you restart the service.</li>';
|
|
echo '<li>Secret values are never shown. Leave a secret blank to keep it.</li>';
|
|
echo '</ul></section>';
|
|
echo '</div>';
|
|
|
|
echo '<section class="card"><div class="card-head"><h2>Config <code>' . h($envPath) . '</code></h2></div>';
|
|
if ($envReadable && ($blank = blank_warning()) !== null) {
|
|
echo '<div class="flash flash-warn">' . h($blank) . ' ' . gui_help_icon('blank') . '</div>';
|
|
}
|
|
if (!$envReadable) {
|
|
echo '<div class="flash flash-error">The env file cannot be read by this user.</div>';
|
|
} else {
|
|
$vars = env_parse((string) file_get_contents($envPath));
|
|
echo '<form method="post"><input type="hidden" name="csrf" value="' . h($token) . '">';
|
|
echo '<input type="hidden" name="action" value="save_config">';
|
|
echo '<div class="table-wrap"><table class="table">';
|
|
echo '<thead><tr><th>Variable</th><th>Value</th><th class="delete">Delete</th></tr></thead><tbody>';
|
|
foreach ($vars as $key => $value) {
|
|
$name = h($key);
|
|
$info = key_info($key);
|
|
$help = help_for_key($key);
|
|
echo '<tr><td><code>' . $name . '</code>' . ($help !== null ? ' ' . help_icon($help) : '');
|
|
if ($info !== null) {
|
|
echo '<div class="key-desc">' . h($info['desc']) . '</div>';
|
|
}
|
|
echo '</td><td>';
|
|
if (preg_match(SECRET_PATTERN, $key)) {
|
|
$hint = $value === '' ? '(not set)' : '(set, leave blank to keep)';
|
|
echo '<input type="password" name="value[' . $name . ']" aria-label="Value for ' . $name . '" placeholder="' . h($hint) . '" autocomplete="off">';
|
|
} else {
|
|
echo '<input type="text" name="value[' . $name . ']" aria-label="Value for ' . $name . '" value="' . h($value) . '">';
|
|
}
|
|
echo '</td><td class="delete"><input type="checkbox" name="delete[]" value="' . $name . '"></td></tr>';
|
|
}
|
|
echo '<tr class="add-row"><td><input type="text" name="new_key" list="known-keys" placeholder="NEW_VARIABLE"></td>';
|
|
echo '<td><input type="text" name="new_value" placeholder="value"></td><td class="delete muted">add</td></tr>';
|
|
echo '</tbody></table></div>';
|
|
echo '<datalist id="known-keys">';
|
|
foreach (known_keys() as $entry) {
|
|
echo '<option value="' . h($entry['key']) . '">' . h($entry['desc']) . '</option>';
|
|
}
|
|
echo '</datalist>';
|
|
echo '<div class="form-actions"><span class="muted">Saving writes a backup first. Restart the service to apply.</span>';
|
|
echo '<button type="submit" class="btn btn-primary"' . ($envWritable ? '' : ' disabled') . '>Save config</button></div>';
|
|
echo '</form>';
|
|
}
|
|
echo '</section>';
|
|
|
|
echo '<details class="card collapsible"><summary><h2>Paste multiple variables</h2>'
|
|
. gui_help_icon('paste') . '<span class="hint">click to open</span></summary>';
|
|
echo '<p class="muted">Paste lines in <code>KEY=VALUE</code> form. You review them before anything is written.</p>';
|
|
echo '<form method="post" class="stack"><input type="hidden" name="csrf" value="' . h($token) . '">';
|
|
echo '<input type="hidden" name="action" value="paste_preview">';
|
|
echo '<textarea name="paste" rows="8" class="paste-box" spellcheck="false" placeholder="TINYAUTH_OIDC_CLIENTS_WIKI_CLIENTID=wiki TINYAUTH_OIDC_CLIENTS_WIKI_NAME=Wiki"></textarea>';
|
|
echo '<div class="form-actions"><span class="muted">Blank lines and lines starting with # are ignored.</span>';
|
|
echo '<button type="submit" class="btn btn-primary">Review</button></div>';
|
|
echo '</form></details>';
|
|
|
|
echo '<div class="grid-2">';
|
|
echo '<details class="card collapsible"><summary><h2>New OIDC client</h2>' . gui_help_icon('oidc_client')
|
|
. '<span class="hint">click to open</span></summary>';
|
|
echo '<p class="muted">Creates the variables for a client that apps use to log in through tinyauth. '
|
|
. 'Leave the secret blank to generate one.</p>';
|
|
echo '<form method="post" class="stack"><input type="hidden" name="csrf" value="' . h($token) . '">';
|
|
echo '<input type="hidden" name="action" value="create_oidc_client">';
|
|
echo '<label>Client name ' . gui_help_icon('client_name') . ' <input type="text" name="client_name" placeholder="SEERR" required></label>';
|
|
echo '<label>Client ID ' . gui_help_icon('client_id') . ' <input type="text" name="client_id" placeholder="defaults to the name, lowercase"></label>';
|
|
echo '<label>Client secret ' . gui_help_icon('client_secret') . ' <input type="password" name="client_secret" autocomplete="off" placeholder="blank = generate"></label>';
|
|
echo '<label>Display name ' . gui_help_icon('display_name') . ' <input type="text" name="display_name" placeholder="defaults to the name"></label>';
|
|
echo '<label>Redirect URIs ' . gui_help_icon('redirect_uris') . ' <input type="text" name="redirect_uris" placeholder="https://app.example.com/api/callback, ..."></label>';
|
|
echo '<button type="submit" class="btn btn-primary">Create client</button>';
|
|
echo '</form></details>';
|
|
|
|
echo '<details class="card collapsible"><summary><h2>New app ACL</h2>' . gui_help_icon('app_acl')
|
|
. '<span class="hint">click to open</span></summary>';
|
|
echo '<p class="muted">Creates the access rules for an app behind tinyauth (forward auth). '
|
|
. 'Fill in what you need; empty fields are skipped.</p>';
|
|
echo '<form method="post" class="stack"><input type="hidden" name="csrf" value="' . h($token) . '">';
|
|
echo '<input type="hidden" name="action" value="create_app_acl">';
|
|
echo '<label>App name ' . gui_help_icon('app_name') . ' <input type="text" name="app_name" placeholder="WIKI" required></label>';
|
|
echo '<label>Domain ' . gui_help_icon('domain') . ' <input type="text" name="domain" placeholder="wiki.example.com"></label>';
|
|
echo '<label>Allowed users ' . gui_help_icon('users_allow') . ' <input type="text" name="users_allow" placeholder="alice, bob"></label>';
|
|
echo '<label>LDAP groups ' . gui_help_icon('ldap_groups') . ' <input type="text" name="ldap_groups" placeholder="wiki-users"></label>';
|
|
echo '<label>OAuth groups ' . gui_help_icon('oauth_groups') . ' <input type="text" name="oauth_groups" placeholder="wiki-users"></label>';
|
|
echo '<button type="submit" class="btn btn-primary">Create app ACL</button>';
|
|
echo '</form></details>';
|
|
echo '</div>';
|
|
|
|
echo '<details class="card collapsible"><summary><h2>Local users</h2>' . gui_help_icon('local_users')
|
|
. '<span class="hint">click to open</span></summary>';
|
|
echo '<p class="muted">Tinyauth stores local users as <code>username:hash</code> pairs in '
|
|
. '<code>TINYAUTH_AUTH_USERS</code> (comma-separated). This tool does not create passwords, because tinyauth hashes them.</p>';
|
|
echo '<ol class="steps">';
|
|
echo '<li>On the server, run <code>tinyauth user create --interactive</code> and enter the username and password when asked. '
|
|
. 'Avoid typing the password on the command line, so it stays out of your shell history.</li>';
|
|
echo '<li>Copy the printed <code>username:hash</code> line.</li>';
|
|
echo '<li>Add it to <code>TINYAUTH_AUTH_USERS</code> in the config above. For several users, separate entries with commas. '
|
|
. 'To add a user, append to the existing value. Do not replace it.</li>';
|
|
echo '<li>Save, then restart tinyauth. Hashes are masked in this tool like other secrets.</li>';
|
|
echo '</ol></details>';
|
|
|
|
echo '<details class="card collapsible reference"><summary><h2>Key reference <span class="muted">(' . count(known_keys()) . ' keys)</span></h2>'
|
|
. gui_help_icon('key_reference') . '<span class="hint">click to open</span></summary>';
|
|
echo '<input type="text" id="key-filter" placeholder="Filter keys..." class="key-filter">';
|
|
echo '<div class="table-wrap"><table class="table" id="key-table"><thead><tr><th>Key</th><th>Description</th><th>Default</th></tr></thead><tbody>';
|
|
foreach (known_keys() as $entry) {
|
|
echo '<tr><td><code>' . h($entry['key']) . '</code></td><td>' . h($entry['desc']) . '</td><td>'
|
|
. ($entry['default'] === '' ? '<span class="muted">none</span>' : '<code>' . h($entry['default']) . '</code>')
|
|
. '</td></tr>';
|
|
}
|
|
echo '</tbody></table></div></details>';
|
|
page_end();
|
|
|
|
function permission_row(string $label, bool $ok, ?string $detail = null): string
|
|
{
|
|
$class = $ok ? 'check-ok' : 'check-missing';
|
|
$mark = $ok ? '✔ ok' : '✘ missing';
|
|
$extra = $detail !== null ? '<br><small class="muted">' . h($detail) . '</small>' : '';
|
|
return '<li class="' . $class . '"><span>' . h($label) . $extra . '</span><span class="mark">' . $mark . '</span></li>';
|
|
}
|