openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
126 lines (125 loc) • 5.09 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { _ as resolveSecretInputRef, o as hasConfiguredSecretInput } from "./types.secrets-BmNLLnHp.mjs";
import { t as formatCliCommand } from "./command-format-CqdwUgPk.mjs";
import { v as formatUnsafeGatewayTailscaleNoAuthMessage, y as isUnsafeGatewayTailscaleNoAuth } from "./validation-core-1Fx8ByEb.mjs";
import { r as replaceConfigFile } from "./mutate-DhAOi0-2.mjs";
import { r as resolveGatewayAuth } from "./auth-resolve-C6_i-P4T.mjs";
import "./auth-dV1bLOjr.mjs";
import { i as assertGatewayServiceUpdateCurrent, o as isUpdateOwnedGatewayServiceCommand } from "./service-update-authority-wlK0pAdP.mjs";
import { n as collectDurableServiceEnvVars } from "./state-dir-dotenv-DM9BdByB.mjs";
import { n as hasAmbiguousGatewayAuthModeConfig } from "./auth-mode-policy-Dn8RauUH.mjs";
import { t as randomToken } from "./random-token-Sl4a6KEQ.mjs";
import { t as resolveGatewayAuthToken } from "./auth-token-resolution-CUaXbu3r.mjs";
//#region src/gateway/auth-install-policy.ts
/** Decide whether install should require token auth when no durable password source exists. */
function shouldRequireGatewayTokenForInstall(e, t) {
switch (e.gateway?.auth?.mode) {
case "token": return true;
case "password":
case "none":
case "trusted-proxy": return false;
case void 0:
}
if (hasConfiguredSecretInput(e.gateway?.auth?.password, e.secrets?.defaults)) return false;
const n = collectDurableServiceEnvVars({
env: t,
config: e
});
return !normalizeOptionalString(n.OPENCLAW_GATEWAY_PASSWORD);
}
//#endregion
//#region src/commands/gateway-install-token.ts
async function maybePersistAutoGeneratedGatewayInstallToken(e) {
try {
const { snapshot: t, writeOptions: n } = e.generation;
if (t.exists && !t.valid) {
e.warnings.push("Warning: config file exists but is invalid; skipping token persistence.");
return;
}
const r = t.exists ? t.sourceConfig ?? t.config : {};
const i = resolveSecretInputRef({
value: r.gateway?.auth?.token,
defaults: r.secrets?.defaults
}).ref;
const a = i || typeof r.gateway?.auth?.token !== "string" ? void 0 : normalizeOptionalString(r.gateway.auth.token);
if (!i && !a) {
await replaceConfigFile({
sourceConfig: {
...r,
gateway: {
...r.gateway,
auth: {
...r.gateway?.auth,
mode: r.gateway?.auth?.mode ?? "token",
token: e.token
}
}
},
snapshot: t,
writeOptions: {
baseSnapshot: t,
...n,
...isUpdateOwnedGatewayServiceCommand() ? { assertCurrent: () => {
n.assertCurrent?.();
assertGatewayServiceUpdateCurrent();
} } : {},
skipRuntimeSnapshotRefresh: true
},
afterWrite: { mode: "auto" }
});
return;
}
if (a) return;
e.warnings.push("Warning: gateway.auth.token is SecretRef-managed; skipping plaintext token persistence.");
} catch (t) {
e.warnings.push(`Warning: could not persist token to config: ${String(t)}`);
}
}
function formatAmbiguousGatewayAuthModeReason() {
return ["gateway.auth.token and gateway.auth.password are both configured while gateway.auth.mode is unset.", `Set ${formatCliCommand("openclaw config set gateway.auth.mode token")} or ${formatCliCommand("openclaw config set gateway.auth.mode password")}.`].join(" ");
}
/** Validate install auth, using the caller's captured write preparation for token generation. */
async function resolveGatewayInstallToken(e) {
const t = e.config;
const n = [];
const resolution = (e) => ({
unavailableReason: e,
warnings: n
});
if (hasAmbiguousGatewayAuthModeConfig(t)) return resolution(formatAmbiguousGatewayAuthModeReason());
const r = resolveGatewayAuth({
authConfig: t.gateway?.auth,
env: e.env,
tailscaleMode: t.gateway?.tailscale?.mode ?? "off"
});
const i = t.gateway?.tailscale?.mode ?? "off";
if (isUnsafeGatewayTailscaleNoAuth({
authMode: r.mode,
tailscaleMode: i
})) return resolution(formatUnsafeGatewayTailscaleNoAuthMessage(i));
if (!(shouldRequireGatewayTokenForInstall(t, e.env) && !r.allowTailscale)) return resolution();
const a = await resolveGatewayAuthToken({
cfg: t,
env: e.env,
explicitToken: e.explicitToken,
envFallback: "no-secret-ref",
unresolvedReasonStyle: "detailed"
});
const o = a.secretRefConfigured;
const s = a.source === "secretRef" ? void 0 : a.token;
let c;
if (o && a.source === "secretRef") n.push("gateway.auth.token is SecretRef-managed; install will not persist a resolved token in service environment. Check that the SecretRef is resolvable in the daemon runtime context.");
else if (o && !s) c = `gateway.auth.token SecretRef is configured but unresolved (${a.unresolvedRefReason ?? "unknown reason"}).`;
if (!s && !o && e.generateIfMissing) {
const t = randomToken();
n.push("No gateway token found. Auto-generated one and saving to config.");
await maybePersistAutoGeneratedGatewayInstallToken({
token: t,
generation: e.generateIfMissing,
warnings: n
});
}
return resolution(c);
}
//#endregion
export { resolveGatewayInstallToken as t };