From 6e1e902a76a56e151e121a216e2f206a22747dab Mon Sep 17 00:00:00 2001 From: Clarth Date: Tue, 1 Dec 2026 16:43:03 -0500 Subject: [PATCH] Paste multiple variables with review popup, tooltips with plain/expert/example, collapsible sections --- README.md | 3 + public/assets/app.js | 51 ++++++++ public/assets/style.css | 141 +++++++++++++++++++++ public/index.php | 74 +++++++---- src/help.php | 272 ++++++++++++++++++++++++++++++++++++++++ src/lib.php | 11 +- src/paste.php | 185 +++++++++++++++++++++++++++ 7 files changed, 711 insertions(+), 26 deletions(-) create mode 100644 src/help.php create mode 100644 src/paste.php diff --git a/README.md b/README.md index d558c4c..b4a0ecf 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,9 @@ A small PHP web UI to edit the tinyauth environment file, and to start, stop, re - **New app ACL:** Fills in `TINYAUTH_APPS__*` for forward-auth apps. Set the domain and whichever users or groups should have access. - **Local users:** Tinyauth hashes passwords, so create users on the server with `tinyauth user create --interactive`. Add the printed `username:hash` line to `TINYAUTH_AUTH_USERS` (comma-separated), then restart. Hashes are masked here. - **Blank values:** Saving or leaving a blank required value is allowed, but the page warns about every blank variable. Blank values can break tinyauth, so fill them in or delete them. +- **Paste multiple variables:** Paste `KEY=VALUE` lines in one box, then click **Review**. A popup lists what's new and what already exists. For existing keys, choose **Keep** or **Update**. Any malformed or duplicated line, or an unfilled ``, fails the whole paste and the popup says why. Nothing is written until you click **Apply**. +- **Help icons (?):** Hover or focus a **?** to see a plain-language explanation, an expert explanation, and an example. The most common keys and the tool's forms have them. +- **Collapsible sections:** Paste, the client and app forms, local users, and the key reference start closed. Click the heading to open. Every change writes a backup to `data/backups/` first. diff --git a/public/assets/app.js b/public/assets/app.js index d4c027a..db35e25 100644 --- a/public/assets/app.js +++ b/public/assets/app.js @@ -1,5 +1,6 @@ (function () { var root = document.documentElement; + var button = document.getElementById('theme-toggle'); if (button) { button.addEventListener('click', function () { @@ -21,4 +22,54 @@ }); }); } + + document.querySelectorAll('dialog[data-autoopen]').forEach(function (dialog) { + if (typeof dialog.showModal === 'function') { + dialog.showModal(); + } + }); + document.querySelectorAll('[data-close]').forEach(function (btn) { + btn.addEventListener('click', function () { + var dialog = btn.closest('dialog'); + if (dialog) { + dialog.close(); + } + }); + }); + + var float = document.createElement('div'); + float.className = 'tip-float'; + document.body.appendChild(float); + + function place(icon) { + var rect = icon.getBoundingClientRect(); + float.style.display = 'block'; + var left = Math.min(rect.left, window.innerWidth - float.offsetWidth - 12); + float.style.left = Math.max(12, left) + 'px'; + var top = rect.bottom + 8; + if (top + float.offsetHeight > window.innerHeight - 12) { + top = Math.max(12, rect.top - float.offsetHeight - 8); + } + float.style.top = top + 'px'; + } + + function show(icon) { + var tip = icon.querySelector('.tip'); + if (!tip) { + return; + } + float.innerHTML = tip.innerHTML; + place(icon); + } + + function hide() { + float.style.display = 'none'; + } + + document.querySelectorAll('.help').forEach(function (icon) { + icon.addEventListener('mouseenter', function () { show(icon); }); + icon.addEventListener('mouseleave', hide); + icon.addEventListener('focus', function () { show(icon); }); + icon.addEventListener('blur', hide); + }); })(); diff --git a/public/assets/style.css b/public/assets/style.css index 0d5ede9..215e0ab 100644 --- a/public/assets/style.css +++ b/public/assets/style.css @@ -419,3 +419,144 @@ details.reference[open] summary { margin-bottom: 1rem; } display: grid; gap: 0.4rem; } + +/* Tooltips */ +.help { + display: inline-grid; + place-items: center; + width: 1.1rem; + height: 1.1rem; + margin-left: 0.3rem; + border-radius: 50%; + border: 1px solid var(--muted); + color: var(--muted); + font-size: 0.7rem; + font-weight: 700; + line-height: 1; + cursor: help; + vertical-align: middle; + text-transform: none; + letter-spacing: 0; +} + +.help:hover, .help:focus { color: var(--accent); border-color: var(--accent); outline: none; } + +.tip { display: none; } + +.tip-float { + display: none; + position: fixed; + z-index: 100; + width: min(360px, calc(100vw - 24px)); + padding: 0.75rem 0.85rem; + border-radius: 10px; + background: var(--surface-2); + border: 1px solid var(--border); + box-shadow: var(--shadow); + color: var(--text); + font-size: 0.82rem; + font-weight: 400; + line-height: 1.45; + pointer-events: none; +} + +.tip-float .tip-row { display: block; margin-top: 0.35rem; } +.tip-float .tip-row:first-child { margin-top: 0; } +.tip-float code { font-size: 0.8rem; } + +/* Collapsible cards */ +details.collapsible > summary { + display: flex; + align-items: center; + gap: 0.5rem; + cursor: pointer; + list-style: none; + flex-wrap: wrap; +} + +details.collapsible > summary::-webkit-details-marker { display: none; } + +details.collapsible > summary::before { + content: "▸"; + color: var(--muted); + width: 0.9rem; + transition: transform 0.15s ease; +} + +details.collapsible[open] > summary::before { content: "▾"; } + +details.collapsible > summary h2 { display: inline; } + +details.collapsible .hint { + margin-left: auto; + color: var(--muted); + font-size: 0.8rem; +} + +details.collapsible[open] .hint { display: none; } +details.collapsible[open] > summary { margin-bottom: 1rem; } + +/* Paste box */ +.paste-box { + width: 100%; + min-height: 9rem; + padding: 0.7rem 0.8rem; + border-radius: 10px; + border: 1px solid var(--border); + background: var(--bg); + color: var(--text); + font-family: ui-monospace, Menlo, Consolas, monospace; + font-size: 0.85rem; + resize: vertical; +} + +.paste-box:focus { + outline: none; + border-color: var(--accent); + box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); +} + +/* Review popup */ +dialog.modal { + width: min(680px, 94vw); + max-height: 88vh; + overflow: auto; + padding: 1.25rem 1.4rem; + border: 1px solid var(--border); + border-radius: var(--radius); + background: var(--surface); + color: var(--text); + box-shadow: var(--shadow); +} + +dialog.modal::backdrop { background: rgba(5, 7, 12, 0.6); } +dialog.modal h3 { margin: 1rem 0 0.4rem; font-size: 0.95rem; } +dialog.modal .card-head { margin-bottom: 0.6rem; } + +.issues { + margin: 0.5rem 0; + padding: 0.7rem 0.9rem 0.7rem 2rem; + border-radius: 10px; + border: 1px solid color-mix(in srgb, var(--bad) 50%, transparent); + background: color-mix(in srgb, var(--bad) 10%, transparent); + color: var(--text); + display: grid; + gap: 0.35rem; + font-size: 0.9rem; +} + +.issues.warn { + border-color: color-mix(in srgb, var(--warn) 55%, transparent); + background: color-mix(in srgb, var(--warn) 10%, transparent); +} + +.dup-choice label { + display: inline-flex; + align-items: center; + gap: 0.3rem; + margin-right: 0.8rem; + font-size: 0.85rem; + cursor: pointer; +} + +.dup-choice input { accent-color: var(--accent); } diff --git a/public/index.php b/public/index.php index 67d1e7c..61f8b08 100644 --- a/public/index.php +++ b/public/index.php @@ -2,7 +2,9 @@ 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(); @@ -20,6 +22,7 @@ if (empty($_SESSION['user'])) { $groups = $_SESSION['groups'] ?? []; $allowedGroup = load_settings()['allowed_group'] ?? ''; $authorized = is_authorized($groups); +$modalHtml = null; if ($_SERVER['REQUEST_METHOD'] === 'POST') { require_csrf(); @@ -40,8 +43,15 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { 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'); } - redirect('index.php'); } $cfg = config(); @@ -57,10 +67,13 @@ page_start('Dashboard', $userHtml); foreach ($flashes as $flash) { echo '
' . h($flash['message']) . '
'; } +if ($modalHtml !== null) { + echo $modalHtml; +} if ($allowedGroup === '') { echo '
'; - echo '

First-time setup

'; + echo '

First-time setup ' . gui_help_icon('access_group') . '

'; 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, then reload.
'; @@ -146,14 +159,14 @@ if (in_array(false, $perms, true)) { . 'from this repo and check it with visudo -cf.

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

Quick notes

'; echo '
    '; -echo '
  • Saving the config writes a backup first.
  • '; +echo '
  • Saving the config writes a backup first. ' . gui_help_icon('backup') . '
  • '; echo '
  • Changes take effect after you restart the service.
  • '; echo '
  • Secret values are never shown. Leave a secret blank to keep it.
  • '; echo '
'; @@ -161,7 +174,7 @@ echo ''; echo '

Config ' . h($envPath) . '

'; if ($envReadable && ($blank = blank_warning()) !== null) { - echo '
' . h($blank) . '
'; + echo '
' . h($blank) . ' ' . gui_help_icon('blank') . '
'; } if (!$envReadable) { echo '
The env file cannot be read by this user.
'; @@ -174,7 +187,8 @@ if (!$envReadable) { foreach ($vars as $key => $value) { $name = h($key); $info = key_info($key); - echo '' . $name . ''; + $help = help_for_key($key); + echo '' . $name . '' . ($help !== null ? ' ' . help_icon($help) : ''); if ($info !== null) { echo '
' . h($info['desc']) . '
'; } @@ -201,35 +215,48 @@ if (!$envReadable) { } echo '
'; +echo '

Paste multiple variables

' + . gui_help_icon('paste') . 'click to open
'; +echo '

Paste lines in KEY=VALUE form. You review them before anything is written.

'; +echo '
'; +echo ''; +echo ''; +echo '
Blank lines and lines starting with # are ignored.'; +echo '
'; +echo '
'; + echo '
'; -echo '

New OIDC client

'; +echo '

New OIDC client

' . gui_help_icon('oidc_client') + . 'click to open
'; echo '

Creates the variables for a client that apps use to log in through tinyauth. ' . 'Leave the secret blank to generate one.

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

New app ACL

'; +echo '

New app ACL

' . gui_help_icon('app_acl') + . 'click to open
'; echo '

Creates the access rules for an app behind tinyauth (forward auth). ' . 'Fill in what you need; empty fields are skipped.

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

Local users

'; +echo '

Local users

' . gui_help_icon('local_users') + . 'click to open
'; echo '

Tinyauth stores local users as username:hash pairs in ' . 'TINYAUTH_AUTH_USERS (comma-separated). This tool does not create passwords, because tinyauth hashes them.

'; echo '
    '; @@ -239,9 +266,10 @@ echo '
  1. Copy the printed username:hash line.
  2. '; echo '
  3. Add it to TINYAUTH_AUTH_USERS in the config above. For several users, separate entries with commas. ' . 'To add a user, append to the existing value. Do not replace it.
  4. '; echo '
  5. Save, then restart tinyauth. Hashes are masked in this tool like other secrets.
  6. '; -echo '
'; +echo ''; -echo '

Key reference (' . count(known_keys()) . ' keys)

'; +echo '

Key reference (' . count(known_keys()) . ' keys)

' + . gui_help_icon('key_reference') . 'click to open
'; echo ''; echo '
'; foreach (known_keys() as $entry) { diff --git a/src/help.php b/src/help.php new file mode 100644 index 0000000..00bf0e8 --- /dev/null +++ b/src/help.php @@ -0,0 +1,272 @@ + match any client or app name. + */ +function key_help(): array +{ + return [ + ['key' => 'TINYAUTH_APPURL', + 'layman' => 'The web address people type to reach tinyauth.', + 'expert' => 'Public base URL of this tinyauth instance. Tinyauth builds its OIDC issuer and cookie domain from it. Must be a full https URL on a real domain, not an IP or localhost.', + 'example' => 'https://auth.example.com'], + ['key' => 'TINYAUTH_SERVER_ADDRESS', + 'layman' => 'Which network address tinyauth listens on. Use 127.0.0.1 if only a local reverse proxy should reach it.', + 'expert' => 'Bind address for the HTTP server. Default 0.0.0.0 listens on all interfaces. 127.0.0.1 restricts it to loopback, which is typical behind nginx.', + 'example' => '127.0.0.1'], + ['key' => 'TINYAUTH_SERVER_PORT', + 'layman' => 'The port tinyauth listens on. nginx forwards to this port.', + 'expert' => 'TCP port for the HTTP server. Default 3000. Must match the proxy_pass or upstream in nginx.', + 'example' => '3008'], + ['key' => 'TINYAUTH_AUTH_SECURECOOKIE', + 'layman' => 'Only send login cookies over https. Turn on when the site uses https.', + 'expert' => 'Sets the Secure flag on session cookies. Default false. Use true whenever APPURL is https; false is only for plain-http testing.', + 'example' => 'true'], + ['key' => 'TINYAUTH_AUTH_SUBDOMAINSENABLED', + 'layman' => 'Share the login between your subdomains, so logging in once works on wiki.example.com and auth.example.com.', + 'expert' => 'Scopes session cookies to the parent domain. Default true. Set false if every protected app shares one exact hostname.', + 'example' => 'true'], + ['key' => 'TINYAUTH_ANALYTICS_ENABLED', + 'layman' => 'Whether tinyauth periodically reports its version. Turn off if you want no phoning home.', + 'expert' => 'Enables periodic version information collection. Default true. Set false to disable all outbound analytics.', + 'example' => 'false'], + ['key' => 'TINYAUTH_AUTH_TRUSTEDPROXIES', + 'layman' => 'The servers allowed to tell tinyauth the real visitor IP, such as nginx.', + 'expert' => 'Comma-separated IPs or CIDR ranges of reverse proxies trusted to set X-Forwarded-For and related headers. Requests from other sources have those headers ignored.', + 'example' => '127.0.0.1,192.168.1.0/24'], + ['key' => 'TINYAUTH_AUTH_USERS', + 'layman' => 'The local username and password accounts tinyauth knows about.', + 'expert' => 'Comma-separated username:bcrypt_hash pairs. Create each hash with "tinyauth user create --interactive" and paste the printed pair here, then restart.', + 'example' => 'alice:$2a$10$...,bob:$2a$10$...'], + ['key' => 'TINYAUTH_AUTH_SESSIONEXPIRY', + 'layman' => 'How long a login lasts before people must log in again.', + 'expert' => 'Session lifetime in seconds. Default 86400 (one day).', + 'example' => '604800 (one week)'], + ['key' => 'TINYAUTH_AUTH_SESSIONMAXLIFETIME', + 'layman' => 'A hard cap on how long a login can last, even if the person is active.', + 'expert' => 'Maximum session lifetime in seconds, regardless of activity. Default 0, which means no cap.', + 'example' => '2592000 (30 days)'], + ['key' => 'TINYAUTH_AUTH_LOGINMAXRETRIES', + 'layman' => 'How many wrong passwords are allowed before tinyauth slows down or blocks further tries.', + 'expert' => 'Maximum failed login attempts before the login is throttled. Default 3.', + 'example' => '5'], + ['key' => 'TINYAUTH_AUTH_LOGINTIMEOUT', + 'layman' => 'How long a login attempt can sit idle before it times out.', + 'expert' => 'Login timeout in seconds, used for the throttle window and pending logins. Default 300.', + 'example' => '300'], + ['key' => 'TINYAUTH_LDAP_ADDRESS', + 'layman' => 'The address of your LDAP directory (for example Active Directory or OpenLDAP).', + 'expert' => 'LDAP server URL. Use ldaps:// for TLS, or ldap:// only with TLS upgrade. Port 636 for ldaps, 389 for plain ldap.', + 'example' => 'ldaps://ldap.example.com:636'], + ['key' => 'TINYAUTH_LDAP_BINDDN', + 'layman' => 'The account tinyauth uses to look up users in LDAP.', + 'expert' => 'Distinguished name of the read-only service account used to bind and search. Leave empty for anonymous bind if your directory allows it.', + 'example' => 'cn=tinyauth,ou=services,dc=example,dc=com'], + ['key' => 'TINYAUTH_LDAP_BINDPASSWORD', + 'layman' => 'The password for the LDAP lookup account above.', + 'expert' => 'Password for the bind DN. Store it here or point TINYAUTH_LDAP_BINDPASSWORDFILE at a file with the password. Treat as a secret.', + 'example' => '(a long random password)'], + ['key' => 'TINYAUTH_LDAP_BASEDN', + 'layman' => 'Where in the LDAP directory to look for users.', + 'expert' => 'Base DN for user searches. Usually the domain root or the users container.', + 'example' => 'ou=people,dc=example,dc=com'], + ['key' => 'TINYAUTH_LDAP_SEARCHFILTER', + 'layman' => 'How tinyauth finds a user from the username they typed. %s is replaced with that username.', + 'expert' => 'LDAP filter with %s for the username. Default (uid=%s). For Active Directory use (sAMAccountName=%s).', + 'example' => '(sAMAccountName=%s)'], + ['key' => 'TINYAUTH_LDAP_INSECURE', + 'layman' => 'Allows LDAP connections without proper encryption or certificate checks. Only for testing.', + 'expert' => 'Skips TLS certificate verification and allows unencrypted LDAP. Default false. Avoid in production.', + 'example' => 'false'], + ['key' => 'TINYAUTH_OIDC_CLIENTS__CLIENTID', + 'layman' => 'The public name an app uses to identify itself to tinyauth.', + 'expert' => 'OAuth/OIDC client_id. Must match the client_id configured in the app exactly.', + 'example' => 'tinyauth-web'], + ['key' => 'TINYAUTH_OIDC_CLIENTS__CLIENTSECRET', + 'layman' => 'A private password shared between the app and tinyauth. Keep it secret.', + 'expert' => 'Client secret used in the token exchange. Generate a long random value, for example "openssl rand -hex 32". Rotating it means updating both sides.', + 'example' => 'a 64-character random hex string'], + ['key' => 'TINYAUTH_OIDC_CLIENTS__TRUSTEDREDIRECTURIS', + 'layman' => 'The exact web addresses tinyauth may send people back to after login. Anything else is refused.', + 'expert' => 'Comma-separated list of allowed redirect_uri values. Matched exactly: scheme, host, path, and trailing slash all count.', + 'example' => 'https://app.example.com/api/auth/callback/oidc'], + ['key' => 'TINYAUTH_OIDC_CLIENTS__NAME', + 'layman' => 'The friendly name shown for this app on the login screen.', + 'expert' => 'Display name for the client ("Client name in UI" in tinyauth help). Cosmetic only.', + 'example' => 'Seerr'], + ['key' => 'TINYAUTH_OIDC_PRIVATEKEYPATH', + 'layman' => 'Where tinyauth keeps the private key it uses to sign login tokens. Back it up and keep it private.', + 'expert' => 'Path to the RSA private key used to sign ID tokens. Default ./tinyauth_oidc_key, relative to the service working directory. Replacing it invalidates existing tokens.', + 'example' => '/var/lib/tinyauth/oidc_key'], + ['key' => 'TINYAUTH_OIDC_PUBLICKEYPATH', + 'layman' => 'Where the public half of that key is kept. Apps use it to check login tokens.', + 'expert' => 'Path to the matching public key. Default ./tinyauth_oidc_key.pub. Keep it paired with the private key.', + 'example' => '/var/lib/tinyauth/oidc_key.pub'], + ['key' => 'TINYAUTH_APPS__CONFIG_DOMAIN', + 'layman' => 'The website address that tinyauth protects with a login.', + 'expert' => 'Domain matched against the forwarded Host header in forward-auth requests. Without it, tinyauth does not match this app.', + 'example' => 'wiki.example.com'], + ['key' => 'TINYAUTH_APPS__USERS_ALLOW', + 'layman' => 'Which usernames may open this app. Leave empty to allow any logged-in user.', + 'expert' => 'Comma-separated usernames allowed by the ACL. Combine with group rules as your tinyauth version defines them.', + 'example' => 'alice,bob'], + ['key' => 'TINYAUTH_APPS__LDAP_GROUPS', + 'layman' => 'LDAP groups that may open this app. Members of any listed group get in.', + 'expert' => 'Comma-separated LDAP group names required for access.', + 'example' => 'wiki-users,admins'], + ['key' => 'TINYAUTH_APPS__OAUTH_GROUPS', + 'layman' => 'OAuth or OIDC groups that may open this app.', + 'expert' => 'Comma-separated group claims from the upstream OAuth/OIDC provider that are required for access.', + 'example' => 'wiki-users'], + ['key' => 'TINYAUTH_LOG_LEVEL', + 'layman' => 'How much detail tinyauth writes to its log. Use debug only while troubleshooting.', + 'expert' => 'Log verbosity, for example debug, info (default), warn, or error.', + 'example' => 'info'], + ['key' => 'TINYAUTH_DATABASE_PATH', + 'layman' => 'The file where tinyauth keeps its data, such as sessions.', + 'expert' => 'SQLite database file path. Default ./tinyauth.db relative to the service working directory. Back it up with the rest of the state.', + 'example' => '/var/lib/tinyauth/tinyauth.db'], + ['key' => 'TINYAUTH_UI_TITLE', + 'layman' => 'The title shown on the tinyauth login page.', + 'expert' => 'Page title and header text for the tinyauth UI. Default "Tinyauth".', + 'example' => 'Clayton\'s Login'], + ]; +} + +/** Tooltip text for the tool's own forms and sections. */ +function gui_help(): array +{ + return [ + 'paste' => [ + 'layman' => 'Paste several variables at once, one per line. The page shows a review first, and nothing is written until you confirm.', + 'expert' => 'Lines are KEY=VALUE. Blank lines and lines starting with # are ignored. Any malformed or duplicated line fails the whole paste. Existing keys need a choice: keep or update.', + 'example' => 'TINYAUTH_OIDC_CLIENTS_WIKI_CLIENTID=wiki', + ], + 'duplicate' => [ + 'layman' => 'This variable already exists in your config. Keep leaves the current value; update replaces it with the pasted one.', + 'expert' => 'A duplicate is any pasted key already present in the env file. Keep is the safe default. Update rewrites only that line, and a backup is taken first.', + 'example' => 'Keep = safe default', + ], + 'oidc_client' => [ + 'layman' => 'An OIDC client is an app that uses tinyauth to log its users in, like "Sign in with tinyauth".', + 'expert' => 'Creates the CLIENTID, CLIENTSECRET, NAME, and TRUSTEDREDIRECTURIS variables for one client. You still need to put the same ID and secret in the app.', + 'example' => 'Client name SEERR, client ID seerr', + ], + 'client_name' => [ + 'layman' => 'A short name for the app, used in the variable names. Letters and digits only.', + 'expert' => 'Becomes the NAME part of TINYAUTH_OIDC_CLIENTS__*. Uppercased, letters and digits only, no underscores.', + 'example' => 'SEERR', + ], + 'client_id' => [ + 'layman' => 'The ID the app sends to tinyauth. Leave blank to use the name in lowercase.', + 'expert' => 'Written as CLIENTID. Must match the client ID configured in the app.', + 'example' => 'seerr', + ], + 'client_secret' => [ + 'layman' => 'The password the app uses with tinyauth. Leave blank and the tool generates a strong one.', + 'expert' => 'Written as CLIENTSECRET. A generated secret is shown once after saving, so copy it into the app then.', + 'example' => 'leave blank to generate', + ], + 'display_name' => [ + 'layman' => 'The name people see on the login screen for this app. Defaults to the client name.', + 'expert' => 'Written as NAME. Cosmetic only.', + 'example' => 'Seerr', + ], + 'redirect_uris' => [ + 'layman' => 'Where tinyauth may send people after they log in. Separate several with commas. The address must be exact.', + 'expert' => 'Written as TRUSTEDREDIRECTURIS. Must match the callback URL the app uses, including scheme, host, and path.', + 'example' => 'https://seerr.example.com/api/v1/auth/oidc/callback', + ], + 'app_acl' => [ + 'layman' => 'Rules for an app that sits behind nginx. nginx asks tinyauth before showing the app, and these rules decide who gets in.', + 'expert' => 'Forward-auth app ACL. Writes TINYAUTH_APPS__* keys. Needs a matching nginx auth_request or forward-auth setup.', + 'example' => 'app WIKI, domain wiki.example.com', + ], + 'app_name' => [ + 'layman' => 'A short name for the app, used in the variable names. Letters and digits only.', + 'expert' => 'Becomes the NAME part of TINYAUTH_APPS__*.', + 'example' => 'WIKI', + ], + 'domain' => [ + 'layman' => 'The address of the app that tinyauth should protect.', + 'expert' => 'Written as CONFIG_DOMAIN. The forwarded Host header must match it.', + 'example' => 'wiki.example.com', + ], + 'users_allow' => [ + 'layman' => 'Usernames allowed into this app, separated by commas.', + 'expert' => 'Written as USERS_ALLOW. Comma-separated.', + 'example' => 'alice, bob', + ], + 'ldap_groups' => [ + 'layman' => 'LDAP groups whose members may open this app.', + 'expert' => 'Written as LDAP_GROUPS. Comma-separated LDAP group names.', + 'example' => 'wiki-users', + ], + 'oauth_groups' => [ + 'layman' => 'Groups from your OIDC or OAuth provider whose members may open this app.', + 'expert' => 'Written as OAUTH_GROUPS. Comma-separated group names from the upstream provider.', + 'example' => 'wiki-users', + ], + 'local_users' => [ + 'layman' => 'Accounts that live in tinyauth itself, not in LDAP or another provider.', + 'expert' => 'Stored as username:bcrypt pairs in TINYAUTH_AUTH_USERS. This tool does not hash passwords, so tinyauth creates the hash.', + 'example' => 'alice:$2a$10$...', + ], + 'access_group' => [ + 'layman' => 'Only members of this tinyauth group can use this tool.', + 'expert' => 'Checked against the groups claim of your OIDC login on every request. Changing it means picking a group from your own groups again.', + 'example' => 'tinyauth-admins', + ], + 'backup' => [ + 'layman' => 'Before any change, the current env file is copied, so you can roll back.', + 'expert' => 'Each write copies the env file to data/backups/ with a timestamp, then writes the new file in place. Permissions are kept.', + 'example' => 'data/backups/tinyauth.env.20261201-204852', + ], + 'blank' => [ + 'layman' => 'A variable with no value. Allowed, but tinyauth may not work with it empty.', + 'expert' => 'An empty KEY= line overrides defaults with an empty string in systemd. Fill it in or delete it.', + 'example' => 'TINYAUTH_OIDC_CLIENTS_SEERR_CLIENTID=', + ], + 'key_reference' => [ + 'layman' => 'Every setting tinyauth accepts, with a plain description and default.', + 'expert' => 'Generated from "tinyauth --help" for tinyauth v5.2.0. Names with are templates: replace NAME with your client or app name.', + 'example' => 'TINYAUTH_APPS__CONFIG_DOMAIN', + ], + ]; +} + +/** Finds tooltip text for a key from the table above, or null. */ +function help_for_key(string $key): ?array +{ + static $entries = null; + if ($entries === null) { + $entries = []; + foreach (key_help() as $entry) { + $entries[] = ['regex' => template_regex($entry['key'])] + $entry; + } + } + foreach ($entries as $entry) { + if (preg_match($entry['regex'], $key)) { + return $entry; + } + } + return null; +} + +function help_icon(array $help): string +{ + return '?' + . '' + . 'Plain: ' . h($help['layman']) . '' + . 'Expert: ' . h($help['expert']) . '' + . 'Example: ' . h($help['example']) . '' + . ''; +} + +function gui_help_icon(string $id): string +{ + $all = gui_help(); + return isset($all[$id]) ? help_icon($all[$id]) : ''; +} diff --git a/src/lib.php b/src/lib.php index 989f319..b037890 100644 --- a/src/lib.php +++ b/src/lib.php @@ -7,15 +7,20 @@ const SERVICE_ACTIONS = ['status', 'start', 'stop', 'restart']; require_once __DIR__ . '/keys.php'; +/** Turns a key template such as TINYAUTH_APPS__CONFIG_DOMAIN into a regex. */ +function template_regex(string $template): string +{ + $parts = array_map(fn (string $p): string => preg_quote($p, '/'), explode('', $template)); + return '/^' . implode('[A-Z0-9]+', $parts) . '$/'; +} + function key_info(string $key): ?array { static $entries = null; if ($entries === null) { $entries = []; foreach (known_keys() as $entry) { - $parts = array_map(fn (string $p): string => preg_quote($p, '/'), explode('', $entry['key'])); - $regex = '/^' . implode('[A-Z0-9]+', $parts) . '$/'; - $entries[] = ['regex' => $regex] + $entry; + $entries[] = ['regex' => template_regex($entry['key'])] + $entry; } } foreach ($entries as $entry) { diff --git a/src/paste.php b/src/paste.php new file mode 100644 index 0000000..c663c1e --- /dev/null +++ b/src/paste.php @@ -0,0 +1,185 @@ + $raw) { + $lineNo = $index + 1; + $line = trim($raw); + if ($line === '' || str_starts_with($line, '#')) { + continue; + } + if (!preg_match('/^([^=]*)=(.*)$/', $line, $m)) { + $errors[] = "Line $lineNo: expected KEY=VALUE, got \"" . $line . '".'; + continue; + } + $key = trim($m[1]); + $value = trim($m[2]); + + if (!preg_match(KEY_PATTERN, $key)) { + $errors[] = "Line $lineNo: \"$key\" is not a valid variable name (letters, digits and underscores)."; + continue; + } + if (preg_match('/^<.*>$/', $value)) { + $errors[] = "Line $lineNo: $key still has the placeholder $value. Replace it with a real value."; + continue; + } + if (isset($lineOf[$key])) { + $errors[] = "Line $lineNo: $key is also on line {$lineOf[$key]}. Keep only one."; + continue; + } + if ($value === '') { + $warnings[] = "$key has no value."; + } + if (key_info($key) === null) { + $warnings[] = "$key is not a known tinyauth key. Check the spelling."; + } + $entries[$key] = $value; + $lineOf[$key] = $lineNo; + } + + if ($entries === [] && $errors === []) { + $errors[] = 'Nothing to add. Paste at least one KEY=VALUE line.'; + } + return ['entries' => $entries, 'errors' => $errors, 'warnings' => $warnings]; +} + +/** Splits parsed entries into new variables and duplicates of what is already in the env file. */ +function paste_plan(string $text): array +{ + $parsed = parse_env_paste($text); + $current = env_parse(current_env()); + $new = []; + $dups = []; + foreach ($parsed['entries'] as $key => $value) { + if (array_key_exists($key, $current)) { + $dups[$key] = ['current' => $current[$key], 'new' => $value]; + } else { + $new[$key] = $value; + } + } + return $parsed + ['new' => $new, 'dups' => $dups]; +} + +function display_value(string $key, string $value): string +{ + if (preg_match(SECRET_PATTERN, $key)) { + return $value === '' ? '(empty)' : '(hidden)'; + } + return $value === '' ? '(empty)' : $value; +} + +/** Runs the paste check. Stores the text for the apply step when it is valid. */ +function paste_preview(string $text, string $token): string +{ + $plan = paste_plan($text); + if ($plan['errors'] === []) { + $_SESSION['paste_pending'] = $text; + } else { + unset($_SESSION['paste_pending']); + } + return paste_modal($plan, $token); +} + +function paste_modal(array $plan, string $token): string +{ + $hasErrors = $plan['errors'] !== []; + $title = $hasErrors ? 'Paste needs fixing' : 'Review pasted variables'; + + $html = '
' + . '' + . '

' . h($title) . ' ' . gui_help_icon('paste') . '

'; + + if ($hasErrors) { + $html .= '

Nothing was changed. Fix these lines, then paste again:

    '; + foreach ($plan['errors'] as $error) { + $html .= '
  • ' . h($error) . '
  • '; + } + $html .= '
'; + $html .= '
'; + } else { + if ($plan['new'] !== []) { + $html .= '

New variables (' . count($plan['new']) . ')

KeyDescriptionDefault
'; + foreach ($plan['new'] as $key => $value) { + $html .= ''; + } + $html .= '
' . h($key) . '' . h(display_value($key, $value)) . '
'; + } + + if ($plan['dups'] !== []) { + $html .= '

Already set (' . count($plan['dups']) . ') ' . gui_help_icon('duplicate') . '

'; + $html .= '
'; + foreach ($plan['dups'] as $key => $dup) { + $name = h($key); + $html .= '' + . '' + . '' + . ''; + } + $html .= '
VariableCurrentPastedAction
' . $name . '' . h(display_value($key, $dup['current'])) . '' . h(display_value($key, $dup['new'])) . '' + . '' + . '' + . '
'; + } + + if ($plan['warnings'] !== []) { + $html .= '
    '; + foreach ($plan['warnings'] as $warning) { + $html .= '
  • ' . h($warning) . '
  • '; + } + $html .= '
'; + } + + $html .= '' + . '
' + . '
'; + } + + return $html . ''; +} + +function handle_paste_apply(): void +{ + $text = $_SESSION['paste_pending'] ?? null; + unset($_SESSION['paste_pending']); + if ($text === null) { + flash('error', 'Nothing is waiting to be added. Paste the variables again.'); + return; + } + + $plan = paste_plan($text); + if ($plan['errors'] !== []) { + flash('error', 'Not added: ' . implode(' ', $plan['errors'])); + return; + } + + $choices = (array) ($_POST['dup'] ?? []); + $changes = $plan['new']; + $updated = 0; + foreach ($plan['dups'] as $key => $dup) { + if (($choices[$key] ?? 'keep') === 'update') { + $changes[$key] = $dup['new']; + $updated++; + } + } + if ($changes === []) { + flash('info', 'No changes. Every pasted variable was kept as it was.'); + return; + } + + $backup = apply_env_changes($changes, []); + if ($backup === null) { + return; + } + $added = count($plan['new']); + flash('ok', "Added $added, updated $updated. Restart tinyauth to apply. Backup: $backup."); +}