# Superpulse edge-proxy integration

`superpulse-proxy.conf` belongs in **lighttpd-proxy**, the TLS termination
service, not the lighttpd application server on port 81. The application owns
the route/credential policy; the administrator owns secrets and service wiring.
No PHP runtime, Composer installation, or additional proxy service is needed.

## Administrator integration

1. Provision `/etc/superpulse/snipeit.conf` **outside every document root**:

   ```lighttpd
   var.superpulse.snipeit-token = "REPLACE_WITH_A_READ_ONLY_SNIPE_IT_TOKEN"
   ```

   Use a least-privilege account with only the asset-read permissions required.
   Keep the file accessible only to root and, if needed, the proxy service
   account (for example, directory mode 0750 and file mode 0640 with the
   appropriate service group). Provision it through the administrator's secret
   workflow; never put the value in Git, command-line arguments, frontend build
   variables, or public configuration dumps. A missing include/variable fails
   configuration validation. An invalid token fails at Snipe-IT.

2. Inside the **existing authenticated** `logs.saikrupagas.com` edge vhost,
   after its normal proxy and authentication configuration, add:

   ```lighttpd
   include "/srv/www/logs.saikrupagas.com/deploy/superpulse-proxy.conf"
   ```

   Use the actual absolute project path if different. This is an explicit
   include, not automatic `.htaccess` discovery. Restrict the vhost to the
   intended hostname. Required modules: `mod_access`, `mod_setenv`, `mod_proxy`,
   plus the existing authentication modules. Load FastCGI authentication before
   `mod_proxy`, as in the current edge configuration.

3. Preserve the dashboard's session-based authentication/authorization on
   `/api/devices/…`. **The include is not an authentication provider.**
   `enable-authelia-authorizer.conf` currently configures forwarding safeguards;
   it is not, by itself, the FastCGI authorizer. On the current deployment,
   include `var.macrodir + "include-authelia-fcgi-authz.conf"` in the logs vhost
   to install the `/` authorizer as well as `/auth/user`. A responder mapping
   for `/auth/user` alone does not protect device requests. Missing or expired
   sessions must be denied before any request reaches Snipe-IT.

   `mod_setenv` replaces Authorization during URI processing, before FastCGI
   authorization. The authorizer must authenticate the browser session cookie,
   not treat the injected Snipe-IT token as user authentication. It is a trusted
   local component and may see that header. Browser cookies also travel to the
   trusted equipment backend; this minimal configuration is not suitable for
   an untrusted third-party upstream.

4. Confirm `127.0.0.1:81` reaches Snipe-IT when the HTTP Host is
   `equipment.saikrupagas.com`. The include changes the upstream Host and URL
   path; it does not use the equipment site's public HTTPS endpoint. If the
   edge runs elsewhere, adapt the upstream to an administrator-provided secure
   transport. Never carry the token over public plaintext HTTP. Keep port 81
   restricted to the intended proxy; enforce equivalent private-file denies
   there if anything can access it directly.

5. Validate the complete edge configuration, for example:

   ```sh
   lighttpd -tt -f /etc/lighttpd/lighttpd-proxy.conf
   ```

   Stage the include on the edge independently (at the path used by its
   `include` directive), and integrate/reload it **before deploying the new
   frontend**. Pushing to main triggers application deployment, so do not push
   this migration before the administrator has prepared the edge. Keep the
   existing `logs.php` application release until the new route is ready. After
   frontend rollout, retire the old PHP route/pool as appropriate and securely
   remove the old document-root `.env.php`. Existing CI deliberately preserves
   `.env.php` and `vendor/`; source deletion does not remove those server files.
   Do not remove a PHP pool used by other applications. Application deployments
   do not automatically reload the edge proxy: validate/reload it explicitly
   whenever this include changes. On the deployed OpenRC service,
   `rc-service lighttpd-proxy reload` only reopens log files; apply configuration
   with `rc-service lighttpd-proxy graceful` after successful validation.

## Endpoint and exposure

- Browser: `GET /api/devices/<tag>` (one tag starting with a letter, digit,
  underscore or hyphen, followed by letters, digits, underscores, dots or
  hyphens). Only GET without query parameters is permitted.
- Upstream: `GET /api/v1/hardware/bytag/<tag>` on the equipment vhost, with the
  server's bearer token replacing any browser-supplied Authorization value.
- Other application paths retain their existing routing and headers.
- Responses are `Cache-Control: no-store`; upstream Set-Cookie is suppressed.
  `/deploy/`, `/.env.php` and the retired `/logs.php` route are denied.
- The **full upstream asset JSON** is returned to authenticated callers, not
  the former PHP field allowlist. The frontend maps display name (falling back
  to model name), serial, assignee, image, category color, and notes. Display
  mapping is not data filtering: confirm the account's asset responses are
  appropriate for every authorized dashboard user. If field-level restriction
  is required, retain an adapter instead of a plain proxy.

## Verification

```sh
python3 tests/proxy-config.test.py
node tests/device-lookup.test.js
```

The proxy test runs a real local lighttpd against synthetic HTTP and FastCGI
services and a dummy token. It checks authorization-before-upstream, token
replacement, fixed host/path mapping, invalid methods/queries/tags, private
paths, no-store, cookie suppression, vhost isolation, and missing-secret failure.
It never contacts production. Set `LIGHTTPD` and optionally `LIGHTTPD_MODULES`
when the binary/modules are not on the standard path.

Before enabling production, verify a signed-in device lookup, anonymous and
expired-session denial, invalid-tag/method rejection, and unchanged event-feed
behavior. These synthetic tests do not verify the deployed Authelia policy.
