openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
103 lines (102 loc) • 4.02 kB
JavaScript
import { _ as resolveSecretInputRef } from "./types.secrets-BmNLLnHp.mjs";
import { a as copyConfigResolutionFactsExcept } from "./resolution-facts-xaIKuREs.mjs";
import { a as getRuntimeConfigSnapshot } from "./runtime-snapshot-WbwbY5b7.mjs";
import { t as createGatewayCredentialPlan } from "./credential-planner-DCQbkiQO.mjs";
import { a as resolveGatewayCredentialsFromValues } from "./credentials-UDpmcQCV.mjs";
//#region src/gateway/auth-resolve.ts
/** Merge sparse runtime auth overrides without erasing configured values. */
function mergeGatewayAuthConfig(e, t) {
const n = { ...e };
if (!t) return n;
for (const e of [
"mode",
"token",
"password",
"allowTailscale",
"rateLimit",
"trustedProxy"
]) if (t[e] !== void 0) Object.assign(n, { [e]: t[e] });
return n;
}
/** Apply auth mode and Tailscale policy to credentials already selected by their owner. */
function resolveGatewayAuthFromCredentials(e) {
const { authConfig: t, authOverride: n, token: r, password: i } = e;
const a = n?.mode ?? t.mode ?? (i ? "password" : "token");
return {
mode: a,
modeSource: n?.mode !== void 0 ? "override" : t.mode ? "config" : i ? "password" : r ? "token" : "default",
token: r,
password: i,
allowTailscale: t.allowTailscale ?? (e.tailscaleMode === "serve" && a !== "password" && a !== "trusted-proxy"),
trustedProxy: t.trustedProxy
};
}
/** Resolve Gateway auth mode, credentials, trusted-proxy policy, and Tailscale allowance. */
function resolveGatewayAuth(e) {
const t = getRuntimeConfigSnapshot();
if (t && t.gateway?.auth === e.authConfig) return resolveGatewayAuthForConfig({
config: t,
authOverride: e.authOverride,
env: e.env,
tailscaleMode: e.tailscaleMode
});
const n = e.authOverride ?? void 0;
const r = mergeGatewayAuthConfig(e.authConfig, n);
const i = e.env ?? process.env;
const a = resolveSecretInputRef({ value: r.token }).ref;
const o = resolveSecretInputRef({ value: r.password }).ref;
const s = resolveGatewayCredentialsFromValues({
configToken: a ? void 0 : r.token,
configPassword: o ? void 0 : r.password,
env: i,
tokenPrecedence: "config-first",
passwordPrecedence: "config-first"
});
return resolveGatewayAuthFromCredentials({
authConfig: r,
authOverride: n,
token: s.token,
password: s.password,
tailscaleMode: e.tailscaleMode
});
}
/** Credential edits may reload only while their resolved authentication mode stays fixed. */
function canHotReloadGatewayAuthCredentials(e, t) {
if (!e || !t) return false;
const n = [e, t].map((e) => {
const t = e.gateway?.auth;
if (!t?.mode && (resolveSecretInputRef({ value: t?.token }).ref || resolveSecretInputRef({ value: t?.password }).ref)) return;
return resolveGatewayAuth({
authConfig: t,
tailscaleMode: e.gateway?.tailscale?.mode
}).mode;
});
return (n[0] === "token" || n[0] === "password") && n[0] === n[1];
}
/** Resolve auth from an env-substituted config while retaining its resolution facts. */
function resolveGatewayAuthForConfig(e) {
const t = e.authOverride ?? void 0;
const n = mergeGatewayAuthConfig(e.config.gateway?.auth, t);
const r = {
...e.config,
gateway: {
...e.config.gateway,
auth: n
}
};
const i = [...t?.token !== void 0 ? ["gateway.auth.token"] : [], ...t?.password !== void 0 ? ["gateway.auth.password"] : []];
copyConfigResolutionFactsExcept(e.config, r, i);
const a = createGatewayCredentialPlan({
config: r,
env: e.env
});
return resolveGatewayAuthFromCredentials({
authConfig: n,
authOverride: t,
token: a.localToken.hasSecretRef ? void 0 : a.localToken.value ?? a.envToken ?? a.remoteToken.value,
password: a.localPassword.hasSecretRef ? void 0 : a.localPassword.value ?? a.envPassword ?? (a.authMode === "trusted-proxy" ? void 0 : a.remotePassword.value),
tailscaleMode: e.tailscaleMode
});
}
//#endregion
export { resolveGatewayAuthFromCredentials as a, resolveGatewayAuthForConfig as i, mergeGatewayAuthConfig as n, resolveGatewayAuth as r, canHotReloadGatewayAuthCredentials as t };