Files
lyuksovannyy 78fce8e1ce dsh-remote 0.4.0: edge auth – slaves log in to an auth proxy in front of the master
- lib/edge-auth.mjs: basic / bearer / oauth2 client_credentials (Authentik M2M) /
  custom headers (Cloudflare Access), proxy session cookie jar, retry with fresh
  token, clear edge-refusal vs DSH-refusal errors
- link + enroll go through the edge; master.edgeAuth stored at pair time,
  secrets as DSH credential refs only; config edgeAuth override
- /dsh-remote/edge-auth: show/change/probe; Settings UI section + pair form
- pair.mjs / install-mac.sh: --edge … flags, secrets prompted via /dev/tty
- tests: fake Authentik-like proxy for all modes + WebSocket through the edge
2026-10-11 12:19:36 +00:00

30 lines
1.4 KiB
JavaScript

// Helpers for deploy/pair.mjs (kept separate so they are unit-testable).
/**
* Set `refs.<ref>` in the text of a DSH `.credentials.yaml`, preserving everything else
* (records, other refs, comments). Values are written as JSON strings (valid YAML scalars).
*/
export function setRefIn(text, ref, value) {
if (!/^[A-Z][A-Z0-9_]*$/.test(ref)) throw new Error(`bad ref name ${ref}`)
const q = JSON.stringify(String(value))
const lines = (text || 'version: 1\n').split('\n')
if (lines[lines.length - 1] === '') lines.pop()
let refsAt = lines.findIndex((l) => /^refs:\s*(\{\})?\s*$/.test(l))
if (refsAt === -1) { lines.push('refs:'); refsAt = lines.length - 1 } else lines[refsAt] = 'refs:'
let end = refsAt + 1
while (end < lines.length && /^\s+\S/.test(lines[end])) end++
const existing = lines.slice(refsAt + 1, end).findIndex((l) => new RegExp(`^\\s+${ref}:`).test(l))
if (existing >= 0) lines[refsAt + 1 + existing] = ` ${ref}: ${q}`
else lines.splice(end, 0, ` ${ref}: ${q}`)
return lines.join('\n') + '\n'
}
/** Read `refs.<ref>` from `.credentials.yaml` text (plain, quoted or JSON-quoted scalars). */
export function getRefIn(text, ref) {
const m = String(text || '').match(new RegExp(`^\\s+${ref}:\\s*(.+)$`, 'm'))
if (!m) return undefined
const v = m[1].trim()
if (v.startsWith('"')) { try { return JSON.parse(v) } catch { return v.slice(1, -1) } }
return v.replace(/^'|'$/g, '')
}