headscale: read the invite list out of whatever envelope the companion sends

This commit is contained in:
2026-08-05 19:02:20 +00:00
parent 864140998f
commit 9dd3f76e56
+18 -1
View File
@@ -104,10 +104,27 @@ async function create(creds: HeadscaleServerCredentials, ctx: OfficerContext): P
return relay(res, (body) => ({ available: true, invite: body.invite ?? body }));
}
/**
* Pull the invite array out of whatever envelope the companion used.
*
* §4.3 specifies the fields but not the wrapper, and the create response came back flat (no `invite` key),
* so the list may equally be a bare array or sit under `invites`/`items`/`data`. Taking the first
* array-valued property is shape-agnostic without being credulous: the body has exactly one array in it.
*/
function pickInvites(body: Record<string, unknown>): unknown[] {
if (Array.isArray(body)) return body;
for (const key of ['invites', 'items', 'data', 'results']) {
const value = body[key];
if (Array.isArray(value)) return value;
}
const found = Object.values(body).find(Array.isArray);
return Array.isArray(found) ? found : [];
}
/** `GET /_officer/enroll/invites` — the admin's audit list. Never carries a token or a key. */
async function list(creds: HeadscaleServerCredentials): Promise<Response> {
const res = await callCompanion(creds, { path: INVITES_PATH });
return relay(res, (body) => ({ available: true, invites: Array.isArray(body.invites) ? body.invites : [] }));
return relay(res, (body) => ({ available: true, invites: pickInvites(body) }));
}
/** `DELETE /_officer/enroll/invites/:id` — revoke an unclaimed invite. A no-op on a claimed one. */