/**
* WireGuard invite redemption routes — DC-150.
*
* PUBLIC, unauthenticated mount (the invite token IS the proof, spec §8.5):
*
* GET /network/wg/redeem/:token → self-contained redemption page
* (browser X25519 keygen, spec §8.2)
* POST /network/wg/redeem/:token → redeem API: validates the one-time
* token, registers the browser-generated
* PUBLIC key, returns the client config
*
* Key-lifecycle invariants (NORMATIVE, spec §8.2):
* - The client keypair is generated in the invitee's BROWSER via WebCrypto
* X25519; the server receives ONLY the base64 public key.
* - The private key never transits or rests on the server: it lives in the
* page session, rendered into the config text + QR CLIENT-side.
* - This module must never reference, accept, or emit a private key.
*
* The provider contract used here (DC-149, src/network/providers/wireguard.js):
* - provider.redeemInvite(token) — single-use + 7d TTL, marks the token
* used on success, throws on unknown/used/expired tokens.
* (Note: the DC-149 provider has no separate getInvite peek; redeemInvite
* is the validation primitive. Any failure there maps to 410 Gone.)
* - provider.addPeer({ personId, deviceId, publicKey, name }) →
* { address, allowedIps, dns, hubPublicKey, hubAddress, listenPort, ... }
*
* Middleware registration (NOT done here — src/utilities/middleware.js is
* drift-test guarded): export PUBLIC_REDEEM_PATH_PREFIX and add
* { path: PUBLIC_REDEEM_PATH_PREFIX, prefix: true } → PUBLIC_ROUTES
* plus the CSRF excludedPaths entry for the POST.
* The CSRF skip is safe because the token is single-use, 7-day TTL, and the
* page is served cross-origin cold (no session cookie exists yet).
*/
'use strict';
const crypto = require('crypto');
/** Public mount prefix — also the constant middleware registration needs. */
const PUBLIC_REDEEM_PATH_PREFIX = '/network/wg/redeem/';
/** WireGuard public/private key format: 43 base64 chars + '=' pad → 32 bytes. */
const WG_KEY_RE = /^[A-Za-z0-9+/]{43}=$/;
const MAX_DEVICE_NAME = 100;
/**
* Deterministic personId for a redeemed invite. The DC-149 redeemInvite
* returns { role, email } only (no person record), so the route derives a
* stable id: invite-carried personId > invite email > hash of the token.
* Deterministic-on-token means a retried redemption (e.g. addPeer failed
* after the token burned) maps to the same person, not a duplicate.
*/
function derivePersonId(invite, token) {
if (invite && typeof invite.personId === 'string' && invite.personId) {
return invite.personId;
}
if (invite && typeof invite.email === 'string' && invite.email.includes('@')) {
return 'wge:' + invite.email;
}
return (
'wgi:' + crypto.createHash('sha256').update(String(token)).digest('hex').slice(0, 24)
);
}
function validDeviceName(s) {
return typeof s === 'string' && s.trim().length > 0 && s.trim().length <= MAX_DEVICE_NAME;
}
function validPublicKey(s) {
if (typeof s !== 'string' || !WG_KEY_RE.test(s)) return false;
try {
return Buffer.from(s, 'base64').length === 32;
} catch (_) {
return false;
}
}
/**
* The redemption page. Self-contained: no build step, no server-side
* keygen, no server-side QR dependency (CDN qrcode with a graceful
* copyable-text + download fallback when the CDN is unreachable).
*/
function renderRedemptionPage() {
return '\n'
+ '\n'
+ '
Your device keys are generated in this browser and never leave it — the server only receives your public key.
\n'
+ '
\n'
+ ' \n'
+ ' \n'
+ ' \n'
+ '
\n'
+ ' \n'
+ '
\n'
+ '
Save this config now. The private key exists only in this page session and is shown exactly once — it is never sent to the server and cannot be recovered later.
\n'
+ ' \n'
+ ' \n'
+ '
QR unavailable (offline / CDN blocked) — use the copyable config above or the download link below.