@mesh-tech/mesh-cli
Version:
CLI for Mesh platform development utilities
46 lines (45 loc) • 1.38 kB
JavaScript
import { getContextConfig, getValidToken } from "../commands/login.js";
export function resolveVpnJoinBroker(context, getCfg = getContextConfig) {
const cfg = getCfg(context);
if (cfg?.vpnJoinBroker)
return cfg.vpnJoinBroker;
return deriveVpnJoinBroker(context, getCfg);
}
export function deriveVpnJoinBroker(context, getCfg = getContextConfig) {
const cfg = getCfg(context);
if (!cfg?.issuer)
return null;
try {
const u = new URL(cfg.issuer);
const parts = u.hostname.split(".");
parts[0] = "vpn-join";
return `https://${parts.join(".")}`;
}
catch {
return null;
}
}
export async function mintPreAuthKey(context, brokerUrl, deps) {
const token = await deps.getValidToken(context);
if (!token)
return null;
const doFetch = deps.fetchFn ?? fetch;
try {
const res = await doFetch(`${brokerUrl}/preauth`, {
method: "POST",
headers: { authorization: `Bearer ${token}` },
});
if (!res.ok)
return null;
const body = (await res.json());
if (!body.authKey)
return null;
return { authKey: body.authKey, loginServer: body.loginServer ?? "" };
}
catch {
return null;
}
}
export function realMintDeps() {
return { getValidToken: (c) => getValidToken(c) };
}