openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
6 lines • 7.07 kB
JavaScript
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{_ as resolveSecretInputRef,o as hasConfiguredSecretInput}from"./types.secrets-UWdyCdE6.mjs";import{c as getAuthoredConfigSecretRef,d as hasUnresolvedConfigPath,l as getConfigResolutionFacts}from"./resolution-facts-DYKtoyes.mjs";import{n as containsEnvVarReference}from"./env-substitution-CbEfgBpH.mjs";
//!src/gateway/credential-planner.ts
const trimToUndefined=normalizeOptionalString;function trimCredentialToUndefined(e){const t=trimToUndefined(e);if(t&&containsEnvVarReference(t))return;return t}function createGatewayCredentialPlan(e){const t=e.env??process.env;const n=e.config.gateway;const r=n?.remote;const i=e.defaults??e.config.secrets?.defaults;const a=n?.auth?.mode;const o=trimToUndefined(t.OPENCLAW_GATEWAY_TOKEN);const s=trimToUndefined(t.OPENCLAW_GATEWAY_PASSWORD);function resolveInput(t,n){const r=getConfigResolutionFacts(e.config);if(hasUnresolvedConfigPath(e.config,t)||getAuthoredConfigSecretRef(e.config,t))return{path:t,configured:true,refPath:t,hasSecretRef:false};if(r!==null&&typeof n===`string`)return{path:t,configured:Boolean(trimToUndefined(n)),value:trimToUndefined(n),hasSecretRef:false};const a=resolveSecretInputRef({value:n,defaults:i}).ref;return{path:t,configured:hasConfiguredSecretInput(n,i),value:a?void 0:trimToUndefined(n),refPath:a?t:void 0,hasSecretRef:a!==null}}const c=resolveInput(`gateway.auth.token`,n?.auth?.token);const l=resolveInput(`gateway.auth.password`,n?.auth?.password);const u=resolveInput(`gateway.remote.token`,r?.token);const d=resolveInput(`gateway.remote.password`,r?.password);const f=a!==`password`&&a!==`none`&&a!==`trusted-proxy`;const p=a===`password`||a===`trusted-proxy`||a!==`token`&&a!==`none`&&!(o||c.configured);const m=Boolean(o||c.configured||u.configured);const h=a===`password`||a===`trusted-proxy`||a!==`token`&&a!==`none`&&!m;const g=f&&(a===`token`||a===void 0&&!(s||l.configured));const _=n?.mode===`remote`;const v=Boolean(trimToUndefined(r?.url));const y=n?.tailscale?.mode===`serve`||n?.tailscale?.mode===`funnel`;const b=_||v||y;const x=f&&!o&&!c.configured;const S=a!==`trusted-proxy`&&!s&&!l.configured&&h;return{configuredMode:n?.mode===`remote`?`remote`:`local`,authMode:a,envToken:o,envPassword:s,localToken:c,localPassword:l,remoteToken:u,remotePassword:d,localTokenCanWin:f,localPasswordCanWin:p,localTokenSurfaceActive:g,tokenCanWin:m,passwordCanWin:h,remoteMode:_,remoteUrlConfigured:v,tailscaleRemoteExposure:y,remoteConfiguredSurface:b,remoteTokenFallbackActive:x,remoteTokenActive:b||x,remotePasswordFallbackActive:S,remotePasswordActive:b||S}}
//!src/gateway/credentials.ts
function resolveExplicitGatewayAuth(e){return{token:trimToUndefined(e?.token),password:trimToUndefined(e?.password)}}const GATEWAY_SECRET_REF_UNAVAILABLE_ERROR_CODE=`GATEWAY_SECRET_REF_UNAVAILABLE`;var GatewaySecretRefUnavailableError=class extends Error{constructor(e){super([`${e} is configured as a secret reference but is unavailable in this command path.`,`Fix: set OPENCLAW_GATEWAY_TOKEN/OPENCLAW_GATEWAY_PASSWORD, pass explicit --token/--password,`,`or run a gateway command path that resolves secret references before credential selection.`].join(`
`));this.code=GATEWAY_SECRET_REF_UNAVAILABLE_ERROR_CODE;this.name=`GatewaySecretRefUnavailableError`;this.path=e}};function isGatewaySecretRefUnavailableError(e,t){return e instanceof GatewaySecretRefUnavailableError&&(!t||e.path===t)}function resolveGatewayCredentialsFromValues(e){const t=e.env??process.env;const n=trimToUndefined(t.OPENCLAW_GATEWAY_TOKEN);const r=trimToUndefined(t.OPENCLAW_GATEWAY_PASSWORD);const i=trimCredentialToUndefined(e.configToken);const a=trimCredentialToUndefined(e.configPassword);const o=e.tokenPrecedence??`env-first`;const s=e.passwordPrecedence??`env-first`;return{token:o===`config-first`?i||n:n||i,password:s===`config-first`?a||r:r||a}}function resolveLocalGatewayCredentials(e){const{plan:t,localPrecedence:n}=e;const resolveCredential=(e,t,r)=>{const i=e.configured?e.value:r;return n===`config-first`?e.value||t||(e.configured?void 0:r):t||i};const r=resolveCredential(t.localToken,t.envToken,t.remoteToken.value);const i=resolveCredential(t.localPassword,t.envPassword,t.authMode===`trusted-proxy`?void 0:t.remotePassword.value);const a={token:r,password:i};const o=e.plan.authMode===`password`||e.plan.authMode===`trusted-proxy`||e.plan.authMode!==`token`&&e.plan.authMode!==`none`&&!a.token;const s=e.plan.authMode===`token`||e.plan.authMode!==`password`&&e.plan.authMode!==`none`&&e.plan.authMode!==`trusted-proxy`&&!a.password;const c=[{credential:t.localToken,env:t.envToken,value:r,canWin:s},{credential:t.localPassword,env:t.envPassword,value:i,canWin:o}].filter(({credential:e,canWin:t})=>e.refPath&&t);const l=(n===`config-first`?c.find(({credential:e,env:t})=>!e.value&&Boolean(t)):void 0)??c.find(({value:e,env:t})=>!e&&!t);if(l?.credential.refPath)throw new GatewaySecretRefUnavailableError(l.credential.refPath);return a}function resolveGatewayCredentialsFromConfig(e){const t=e.env??process.env;const n=resolveExplicitGatewayAuth(e.explicitAuth);if(n.token||n.password)return n;if(trimToUndefined(e.urlOverride))return e.urlOverrideSource===`env`?resolveGatewayCredentialsFromValues({env:t}):{};const r=createGatewayCredentialPlan({config:e.cfg,env:t});if((e.modeOverride??r.configuredMode)===`local`)return resolveLocalGatewayCredentials({plan:r,localPrecedence:e.localPrecedence??`config-first`});const i=e.remoteTokenFallback??`remote-env-local`;const a=e.remotePasswordFallback??`remote-env-local`;const o=e.remoteTokenPrecedence??`remote-first`;const s=e.remotePasswordPrecedence??`env-first`;const resolveCredential=e=>{const t=r[`remote${e}`].value;if((e===`Token`?i:a)===`remote-only`)return t;const n=r[`env${e}`];const c=r[`local${e}`].value;return(e===`Token`?o:s)===`env-first`?n||t||c:t||n||c};const c=resolveCredential(`Token`);const l=resolveCredential(`Password`);const u=i!==`remote-only`;const d=i===`remote-only`?void 0:r.localToken.value;const f=a===`remote-only`?void 0:r.localPassword.value;if(r.remoteToken.refPath&&!c&&!r.envToken&&!d&&!l)throw new GatewaySecretRefUnavailableError(r.remoteToken.refPath);if(r.remotePassword.refPath&&!l&&!r.envPassword&&!f&&!c)throw new GatewaySecretRefUnavailableError(r.remotePassword.refPath);if(r.localToken.refPath&&u&&!c&&!l&&!r.envToken&&!r.remoteToken.value&&r.localTokenCanWin)throw new GatewaySecretRefUnavailableError(r.localToken.refPath);return{token:c,password:l}}function resolveGatewayProbeCredentialsFromConfig(e){return resolveGatewayCredentialsFromConfig({cfg:e.cfg,env:e.env,explicitAuth:e.explicitAuth,urlOverride:e.urlOverride,urlOverrideSource:e.urlOverrideSource,modeOverride:e.mode,remoteTokenFallback:`remote-only`})}export{resolveGatewayCredentialsFromValues as a,trimToUndefined as c,resolveGatewayCredentialsFromConfig as i,isGatewaySecretRefUnavailableError as n,resolveGatewayProbeCredentialsFromConfig as o,resolveExplicitGatewayAuth as r,createGatewayCredentialPlan as s,GatewaySecretRefUnavailableError as t};