diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..900264b --- /dev/null +++ b/examples/README.md @@ -0,0 +1,14 @@ +# Examples + +## nginx + +Pick one and adapt the paths, domain, and PHP-FPM socket. Each file is commented. + +| File | Use when | +| --- | --- | +| `nginx/subdomain.conf` | You want `https://tinyauth-web.example.com/` on its own name. | +| `nginx/subpath.conf` | You want `https://www.example.com/tinyauth-web/` inside an existing site. | +| `nginx/subdomain-lan-only.conf` | As above, but only reachable from your LAN or VPN. | + +All of them serve only `public/`, so `config.php` and `data/` are never reachable over HTTP. +After editing, run `nginx -t` before reloading. diff --git a/examples/nginx/subdomain-lan-only.conf b/examples/nginx/subdomain-lan-only.conf new file mode 100644 index 0000000..1571364 --- /dev/null +++ b/examples/nginx/subdomain-lan-only.conf @@ -0,0 +1,37 @@ +# tinyauth-web on a subdomain, reachable only from your LAN and VPN. +# +# This is the subdomain config plus an IP allowlist. Login through tinyauth is still +# required, so the allowlist is an extra layer, not a replacement. +# Replace the example ranges with your own. Test with `nginx -t`, then reload nginx. + +server { + listen 443 ssl; + listen [::]:443 ssl; + http2 on; + server_name tinyauth-web.example.com; + + ssl_certificate /etc/letsencrypt/live/tinyauth-web.example.com/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/tinyauth-web.example.com/privkey.pem; + + allow 192.168.1.0/24; # LAN + allow 100.64.0.0/10; # Tailscale + allow 127.0.0.1; + deny all; + + root /var/www/tinyauth-web/public; + index index.php; + + location / { + try_files $uri $uri/ =404; + } + + location ~ \.php$ { + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_pass unix:/run/php/php8.4-fpm.sock; # match your PHP-FPM socket + } + + location ~ /\. { + deny all; + } +} diff --git a/examples/nginx/subdomain.conf b/examples/nginx/subdomain.conf new file mode 100644 index 0000000..158105e --- /dev/null +++ b/examples/nginx/subdomain.conf @@ -0,0 +1,38 @@ +# tinyauth-web on its own subdomain: https://tinyauth-web.example.com/ +# +# Only public/ is served, so config.php and data/ are never reachable. +# Test with `nginx -t`, then reload nginx. Adjust the paths and the PHP-FPM socket. + +server { + listen 80; + listen [::]:80; + server_name tinyauth-web.example.com; + return 301 https://$host$request_uri; +} + +server { + listen 443 ssl; + listen [::]:443 ssl; + http2 on; + server_name tinyauth-web.example.com; + + ssl_certificate /etc/letsencrypt/live/tinyauth-web.example.com/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/tinyauth-web.example.com/privkey.pem; + + root /var/www/tinyauth-web/public; + index index.php; + + location / { + try_files $uri $uri/ =404; + } + + location ~ \.php$ { + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_pass unix:/run/php/php8.4-fpm.sock; # match your PHP-FPM socket + } + + location ~ /\. { + deny all; + } +} diff --git a/examples/nginx/subpath.conf b/examples/nginx/subpath.conf new file mode 100644 index 0000000..38dc162 --- /dev/null +++ b/examples/nginx/subpath.conf @@ -0,0 +1,26 @@ +# tinyauth-web under an existing site: https://www.example.com/tinyauth-web/ +# +# Put these blocks inside the existing `server { }` for www.example.com. +# The URL path maps to public/. Set redirect_uri in config.php to +# https://www.example.com/tinyauth-web/callback.php +# and register the same URL as a trusted redirect URI in tinyauth. +# Test with `nginx -t`, then reload nginx. Adjust the paths and the PHP-FPM socket. + +location = /tinyauth-web { + return 301 /tinyauth-web/; +} + +location ^~ /tinyauth-web/ { + alias /var/www/tinyauth-web/public/; + index index.php; + + location ~ ^/tinyauth-web/(.+\.php)$ { + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME /var/www/tinyauth-web/public/$1; + fastcgi_pass unix:/run/php/php8.4-fpm.sock; # match your PHP-FPM socket + } + + location ~ /\. { + deny all; + } +} diff --git a/public/assets/app.js b/public/assets/app.js new file mode 100644 index 0000000..2ed3d61 --- /dev/null +++ b/public/assets/app.js @@ -0,0 +1,14 @@ +(function () { + var root = document.documentElement; + var button = document.getElementById('theme-toggle'); + if (!button) { + return; + } + button.addEventListener('click', function () { + var next = root.getAttribute('data-theme') === 'light' ? 'dark' : 'light'; + root.setAttribute('data-theme', next); + try { + localStorage.setItem('tinyauth-web-theme', next); + } catch (e) {} + }); +})(); diff --git a/public/assets/style.css b/public/assets/style.css new file mode 100644 index 0000000..ca03318 --- /dev/null +++ b/public/assets/style.css @@ -0,0 +1,372 @@ +:root { + --bg: #0c0e13; + --bg-glow: #151a28; + --surface: #161a23; + --surface-2: #1d2230; + --border: #2a3142; + --text: #e7e9f0; + --muted: #8d96ab; + --accent: #7c9cff; + --accent-ink: #0c0e13; + --ok: #3fb950; + --bad: #f85149; + --warn: #e3b341; + --radius: 12px; + --shadow: 0 10px 30px rgba(0, 0, 0, 0.35); + color-scheme: dark; +} + +[data-theme="light"] { + --bg: #f4f6fb; + --bg-glow: #e5eaf7; + --surface: #ffffff; + --surface-2: #f0f3fa; + --border: #d9dfeb; + --text: #1a1f2e; + --muted: #5e6880; + --accent: #3557d6; + --accent-ink: #ffffff; + --ok: #1a7f37; + --bad: #b42318; + --warn: #9a6700; + --shadow: 0 10px 30px rgba(20, 30, 60, 0.08); + color-scheme: light; +} + +* { box-sizing: border-box; } + +html, body { min-height: 100%; } + +body { + margin: 0; + font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; + font-size: 15px; + line-height: 1.5; + color: var(--text); + background: radial-gradient(1200px 600px at 10% -10%, var(--bg-glow), transparent 60%), var(--bg); + background-attachment: fixed; +} + +code, pre, input[type="text"], input[type="password"] { + font-family: ui-monospace, "JetBrains Mono", Menlo, Consolas, monospace; +} + +h1, h2 { margin: 0; line-height: 1.2; } +h2 { font-size: 1.05rem; letter-spacing: 0.02em; } +a { color: var(--accent); } + +/* Top bar */ +.topbar { + position: sticky; + top: 0; + z-index: 10; + display: flex; + align-items: center; + justify-content: space-between; + gap: 1rem; + padding: 0.75rem 1.5rem; + background: color-mix(in srgb, var(--surface) 85%, transparent); + backdrop-filter: blur(10px); + border-bottom: 1px solid var(--border); +} + +.brand { + display: flex; + align-items: center; + gap: 0.5rem; + font-weight: 700; + letter-spacing: 0.01em; +} + +.logo { + display: inline-grid; + place-items: center; + width: 1.9rem; + height: 1.9rem; + border-radius: 8px; + background: linear-gradient(135deg, var(--accent), #b07cff); + color: var(--accent-ink); + font-size: 0.85rem; +} + +.topbar-right { + display: flex; + align-items: center; + gap: 0.6rem; +} + +.who { + color: var(--muted); + font-size: 0.9rem; +} + +main { + max-width: 1040px; + margin: 0 auto; + padding: 1.5rem; + display: grid; + gap: 1.25rem; +} + +.footer { + text-align: center; + color: var(--muted); + font-size: 0.8rem; + padding: 1.5rem; +} + +/* Cards */ +.card { + background: var(--surface); + border: 1px solid var(--border); + border-radius: var(--radius); + box-shadow: var(--shadow); + padding: 1.25rem 1.4rem; +} + +.card-head { + display: flex; + align-items: center; + justify-content: space-between; + gap: 1rem; + margin-bottom: 1rem; + flex-wrap: wrap; +} + +.card-center { + max-width: 420px; + margin: 6vh auto 0; + text-align: center; + display: grid; + gap: 1rem; + justify-items: center; +} + +.muted { color: var(--muted); } + +.grid-2 { + display: grid; + grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); + gap: 1.25rem; +} + +/* Service state */ +.state { + display: inline-flex; + align-items: center; + gap: 0.45rem; + padding: 0.25rem 0.7rem; + border-radius: 999px; + font-size: 0.85rem; + font-weight: 600; + background: var(--surface-2); + color: var(--muted); + border: 1px solid var(--border); +} + +.state::before { + content: ""; + width: 0.55rem; + height: 0.55rem; + border-radius: 50%; + background: currentColor; +} + +.state-active { color: var(--ok); } +.state-failed { color: var(--bad); } +.state-inactive { color: var(--muted); } +.state-unknown { color: var(--warn); } + +/* Buttons */ +.btn-row { + display: flex; + flex-wrap: wrap; + gap: 0.6rem; +} + +.btn { + display: inline-flex; + align-items: center; + gap: 0.4rem; + padding: 0.5rem 1rem; + border-radius: 9px; + border: 1px solid var(--border); + background: var(--surface-2); + color: var(--text); + font: inherit; + font-weight: 600; + cursor: pointer; + text-decoration: none; + transition: transform 0.08s ease, filter 0.15s ease, border-color 0.15s ease; +} + +.btn:hover:not(:disabled) { filter: brightness(1.12); border-color: var(--muted); } +.btn:active:not(:disabled) { transform: translateY(1px); } +.btn:disabled { opacity: 0.4; cursor: not-allowed; } +.btn-sm { padding: 0.3rem 0.7rem; font-size: 0.85rem; } + +.btn-primary { + background: linear-gradient(135deg, var(--accent), #b07cff); + border-color: transparent; + color: var(--accent-ink); +} + +.btn-start { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 45%, var(--border)); } +.btn-stop { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 45%, var(--border)); } +.btn-restart { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 45%, var(--border)); } +.btn-status { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); } +.btn-ghost { background: transparent; } + +/* Flash messages */ +.flash { + padding: 0.7rem 1rem; + border-radius: 10px; + border: 1px solid var(--border); + background: var(--surface-2); +} + +.flash-ok { border-color: color-mix(in srgb, var(--ok) 50%, transparent); color: var(--ok); } +.flash-error { border-color: color-mix(in srgb, var(--bad) 50%, transparent); color: var(--bad); } +.flash-info { border-color: color-mix(in srgb, var(--accent) 50%, transparent); color: var(--accent); } + +/* Output */ +pre { + margin: 1rem 0 0; + padding: 0.9rem 1rem; + border-radius: 10px; + background: var(--bg); + border: 1px solid var(--border); + color: var(--text); + font-size: 0.85rem; + overflow: auto; + white-space: pre-wrap; + max-height: 22rem; +} + +/* Permission checks */ +.checks { + list-style: none; + margin: 0; + padding: 0; + display: grid; + gap: 0.45rem; +} + +.checks li { + display: flex; + justify-content: space-between; + gap: 1rem; + padding: 0.5rem 0.75rem; + border-radius: 9px; + background: var(--surface-2); + border: 1px solid var(--border); + font-size: 0.9rem; +} + +.check-ok .mark { color: var(--ok); } +.check-missing .mark { color: var(--bad); } +.mark { font-weight: 700; white-space: nowrap; } + +.notice { + margin-top: 1rem; + padding: 0.7rem 0.9rem; + border-radius: 10px; + border: 1px dashed var(--warn); + color: var(--warn); + font-size: 0.9rem; +} + +/* Forms and tables */ +.table-wrap { overflow-x: auto; } + +table.table { + width: 100%; + border-collapse: separate; + border-spacing: 0; +} + +.table th, .table td { + text-align: left; + padding: 0.55rem 0.7rem; + border-bottom: 1px solid var(--border); + vertical-align: middle; +} + +.table th { + color: var(--muted); + font-size: 0.78rem; + text-transform: uppercase; + letter-spacing: 0.06em; + font-weight: 600; +} + +.table tr:last-child td { border-bottom: none; } +.table td.delete { text-align: center; width: 4rem; } +.table td.delete input { accent-color: var(--bad); width: 1rem; height: 1rem; } + +input[type="text"], input[type="password"] { + width: 100%; + padding: 0.5rem 0.65rem; + border-radius: 8px; + border: 1px solid var(--border); + background: var(--bg); + color: var(--text); + font-size: 0.9rem; + transition: border-color 0.15s ease, box-shadow 0.15s ease; +} + +input[type="text"]:focus, input[type="password"]:focus { + outline: none; + border-color: var(--accent); + box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); +} + +input[type="text"]::placeholder, input[type="password"]::placeholder { + color: var(--muted); +} + +.table tr.add-row td { background: var(--surface-2); } + +.form-actions { + display: flex; + align-items: center; + justify-content: space-between; + gap: 1rem; + margin-top: 1rem; + flex-wrap: wrap; +} + +/* Setup form */ +.radio-list { + display: grid; + gap: 0.5rem; + margin: 1rem 0; +} + +.radio-row { + display: flex; + align-items: center; + gap: 0.6rem; + padding: 0.6rem 0.8rem; + border-radius: 9px; + border: 1px solid var(--border); + background: var(--surface-2); + cursor: pointer; +} + +.radio-row:hover { border-color: var(--muted); } +.radio-row input { accent-color: var(--accent); } + +code { + padding: 0.1rem 0.35rem; + border-radius: 6px; + background: var(--surface-2); + border: 1px solid var(--border); + font-size: 0.88em; +} + +@media (max-width: 640px) { + .topbar { padding: 0.7rem 1rem; } + main { padding: 1rem; } + .who { display: none; } +} diff --git a/public/callback.php b/public/callback.php index b626cfc..5ed47de 100644 --- a/public/callback.php +++ b/public/callback.php @@ -47,6 +47,7 @@ try { redirect('index.php'); } catch (RuntimeException $e) { page_start('Login error'); - echo '

Login failed

' . h($e->getMessage()) . '

Try again

'; + echo '

Login failed

' . h($e->getMessage()) + . '
Try again
'; page_end(); } diff --git a/public/index.php b/public/index.php index 1f9e92c..522b0ef 100644 --- a/public/index.php +++ b/public/index.php @@ -7,8 +7,12 @@ require dirname(__DIR__) . '/src/actions.php'; start_session(); if (empty($_SESSION['user'])) { - page_start('tinyauth-web'); - echo '

tinyauth-web

Log in with tinyauth

'; + page_start('Log in'); + echo '
' + . '

Welcome back

' + . '

Manage the tinyauth service and its settings. Log in with your tinyauth account.

' + . 'Log in with tinyauth' + . '
'; page_end(); exit; } @@ -42,24 +46,32 @@ $lastOutput = $_SESSION['last_output'] ?? ''; unset($_SESSION['last_output']); $token = csrf_token(); -page_start('tinyauth-web'); -echo '

tinyauth-web

' . h($_SESSION['user']) . ' · log out
'; +$userHtml = '' . h($_SESSION['user']) . '' + . 'Log out'; +page_start('Dashboard', $userHtml); + foreach ($flashes as $flash) { - echo '

' . h($flash['message']) . '

'; + echo '
' . h($flash['message']) . '
'; } if ($allowedGroup === '') { - echo '

First-time setup

'; - echo '

Choose the tinyauth group whose members may use this tool.

'; + echo '
'; + echo '

First-time setup

'; + echo '

Choose the tinyauth group whose members may use this tool.

'; if ($groups === []) { - echo '

Your account has no groups in tinyauth. Add yourself to a group first, then reload.

'; + echo '
Your account has no groups in tinyauth. Add yourself to a group, then reload.
'; } else { - echo '
'; + echo ''; + echo ''; echo ''; + echo '
'; foreach ($groups as $group) { - echo '
'; + echo ''; } - echo '

'; + echo '
'; + echo ''; + echo ''; } echo '
'; page_end(); @@ -67,7 +79,10 @@ if ($allowedGroup === '') { } if (!$authorized) { - echo '

You are not in the required tinyauth group ' . h($allowedGroup) . '.

'; + echo '
'; + echo '

Access denied

'; + echo '

You are not in the required tinyauth group ' . h($allowedGroup) . '.

'; + echo '
'; page_end(); exit; } @@ -81,15 +96,23 @@ $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 '

Service: ' . h($service) . '

'; -echo '

State: ' . h($state) . '

'; -echo '
'; +echo '
'; +echo '

Service ' . h($service) . '

'; +echo '' . h($state) . '
'; +echo ''; echo ''; foreach (SERVICE_ACTIONS as $action) { $disabled = $perms[$action] ? '' : ' disabled'; - echo ''; + echo ''; } echo ''; if ($status !== null) { @@ -105,29 +128,42 @@ $envReadable = is_file($envPath) && is_readable($envPath); $envWritable = $envReadable && is_writable($envPath); $dataWritable = is_writable(data_dir()); -echo '

Permission checks

'; +echo '
'; +echo '

Permission checks

    '; foreach (SERVICE_ACTIONS as $action) { - echo '
'; + echo permission_row('sudo: systemctl ' . $action . ' ' . $service, $perms[$action]); } -echo ''; -echo ''; -echo ''; -echo '
sudo: systemctl ' . h($action) . ' ' . h($service) . '' . permission_mark($perms[$action]) . '
Env file readable (' . h($envPath) . ')' . permission_mark($envReadable) . '
Env file writable' . permission_mark($envWritable) . '
Data directory writable' . permission_mark($dataWritable) . '
'; +echo permission_row('Env file readable', $envReadable, $envPath); +echo permission_row('Env file writable', $envWritable); +echo permission_row('Data directory writable', $dataWritable); +echo ''; if (in_array(false, $perms, true)) { - echo '

Missing sudo rules: install sudoers/tinyauth-web.sudoers from this repo (check with visudo -cf).

'; + echo '

Missing sudo rules. Install sudoers/tinyauth-web.sudoers ' + . 'from this repo and check it with visudo -cf.

'; } -echo '
'; -echo '
'; +echo '
'; +echo 'Current group: ' . h($allowedGroup) . ''; +echo ''; +echo '
'; echo '
'; -echo '

Config: ' . h($envPath) . '

'; +echo '

Quick notes

'; +echo '
    '; +echo '
  • Saving the config writes a backup first.
  • '; +echo '
  • Changes take effect after you restart the service.
  • '; +echo '
  • Secret values are never shown. Leave a secret blank to keep it.
  • '; +echo '
'; +echo ''; + +echo '

Config ' . h($envPath) . '

'; if (!$envReadable) { - echo '

The env file cannot be read by this user.

'; + echo '
The env file cannot be read by this user.
'; } else { $vars = env_parse((string) file_get_contents($envPath)); echo '
'; echo ''; - echo ''; + echo '
VariableValueDelete
'; + echo ''; foreach ($vars as $key => $value) { $name = h($key); echo ''; + echo ''; } - echo ''; - echo ''; - echo '
VariableValueDelete
' . $name . ''; @@ -137,19 +173,22 @@ if (!$envReadable) { } else { echo ''; } - echo '
add
'; - echo '

'; - echo 'Saving writes a backup first. Restart tinyauth to apply changes.

'; + echo ''; + echo 'add'; + echo ''; + echo '
Saving writes a backup first. Restart the service to apply.'; + echo '
'; echo '
'; } echo '
'; page_end(); -function permission_mark(bool $ok): string +function permission_row(string $label, bool $ok, ?string $detail = null): string { - return $ok ? '✔ ok' : '✘ missing'; + $class = $ok ? 'check-ok' : 'check-missing'; + $mark = $ok ? '✔ ok' : '✘ missing'; + $extra = $detail !== null ? '
' . h($detail) . '' : ''; + return '
  • ' . h($label) . $extra . '' . $mark . '
  • '; } diff --git a/public/login.php b/public/login.php index d18f645..a53b0b8 100644 --- a/public/login.php +++ b/public/login.php @@ -20,6 +20,7 @@ try { ])); } catch (RuntimeException $e) { page_start('Login error'); - echo '

    Cannot reach tinyauth

    ' . h($e->getMessage()) . '

    '; + echo '

    Cannot reach tinyauth

    ' + . h($e->getMessage()) . '
    '; page_end(); } diff --git a/src/lib.php b/src/lib.php index f431f51..d1f5acc 100644 --- a/src/lib.php +++ b/src/lib.php @@ -208,28 +208,21 @@ function env_apply(string $content, array $changes, array $deletes): string return rtrim(implode("\n", $out), "\n") . "\n"; } -function page_start(string $title): void +function page_start(string $title, string $userHtml = ''): void { echo '' . '' - . '' . h($title) . '
    '; + . '' . h($title) . ' · tinyauth-web' + . '' + . '' + . '' + . '
    tinyauth-web
    ' + . '
    ' . $userHtml + . '' + . '
    '; } function page_end(): void { - echo '
    '; + echo '
    tinyauth-web
    '; }