tinyauth-web/public/index.php

194 lines
7.8 KiB
PHP

<?php
declare(strict_types=1);
require dirname(__DIR__) . '/src/lib.php';
require dirname(__DIR__) . '/src/actions.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);
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();
}
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 ($allowedGroup === '') {
echo '<section class="card card-center">';
echo '<h2>First-time setup</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></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" style="margin:0;padding-left:1.1rem;display:grid;gap:.4rem">';
echo '<li>Saving the config writes a backup first.</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) {
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);
echo '<tr><td><code>' . $name . '</code></td><td>';
if (preg_match(SECRET_PATTERN, $key)) {
$hint = $value === '' ? '(not set)' : '(set, leave blank to keep)';
echo '<input type="password" name="value[' . $name . ']" placeholder="' . h($hint) . '" autocomplete="off">';
} else {
echo '<input type="text" name="value[' . $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" 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 '<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>';
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>';
}