UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 2.85 kB
import{l as normalizeOptionalString,o as normalizeLowercaseStringOrEmpty}from"./string-coerce-CpTFcSwY.mjs";import{h as readRuntimeConfig}from"./runtime-snapshot-Bg5K4JNu.mjs";import{r as resolveGatewayAuth}from"./auth-resolve-UwT9_UjV.mjs";import{t as ensureGatewayStartupAuth}from"./startup-auth-_Tz39OJi.mjs";import"./string-coerce-runtime-BYDH9Quw.mjs";import"./runtime-config-snapshot-N0DER8v5.mjs";import"./gateway-runtime-CCd3DVdi.mjs";import{r as persistBrowserControlCredential}from"./config-mutations-DyTCiDMe.mjs";import crypto from"node:crypto"; //!extensions/browser/src/browser/control-auth.ts function resolveBrowserControlAuth(e,t=process.env){const n=resolveGatewayAuth({authConfig:e?.gateway?.auth,env:t,tailscaleMode:e?.gateway?.tailscale?.mode});const r=normalizeOptionalString(n.token);const i=normalizeOptionalString(n.password);switch(n.mode){case`password`:case`trusted-proxy`:return{password:i};case`token`:case`none`:return{token:r};default:return{}}}function shouldAutoGenerateBrowserAuth(e){if(normalizeLowercaseStringOrEmpty(e.NODE_ENV)===`test`)return false;const t=normalizeLowercaseStringOrEmpty(e.VITEST);return!t||[`0`,`false`,`off`].includes(t)}let trustedProxyBrowserPassword;function resolveTrustedProxyBrowserPassword(){if(trustedProxyBrowserPassword)return{auth:{password:trustedProxyBrowserPassword}};trustedProxyBrowserPassword=crypto.randomBytes(24).toString(`hex`);return{auth:{password:trustedProxyBrowserPassword},generatedToken:trustedProxyBrowserPassword}}async function generateAndPersistBrowserControlCredential(e){const t=crypto.randomBytes(24).toString(`hex`);await persistBrowserControlCredential({kind:e.kind,value:t});const n=resolveBrowserControlAuth(readRuntimeConfig(),e.env);if(n.token||n.password)return{auth:n,generatedToken:n[e.kind]===t?t:void 0};return{auth:{[e.kind]:t},generatedToken:t}}async function ensureBrowserControlAuth(e){const t=e.env??process.env;const n=resolveBrowserControlAuth(e.cfg,t);if(n.token||n.password)return{auth:n};if(!shouldAutoGenerateBrowserAuth(t))return{auth:n};if(e.cfg.gateway?.auth?.mode===`password`)return{auth:n};const r=readRuntimeConfig();const i=resolveBrowserControlAuth(r,t);if(i.token||i.password)return{auth:i};if(r.gateway?.auth?.mode===`password`)return{auth:i};const a=r.gateway?.auth?.mode;if(a===`none`||a===`trusted-proxy`){const e=a===`trusted-proxy`?`password`:`token`;const n=r.gateway?.auth?.[e];if(n!=null&&typeof n!==`string`)return{auth:i};if(e===`password`)return resolveTrustedProxyBrowserPassword();return await generateAndPersistBrowserControlCredential({kind:e,env:t})}const o=await ensureGatewayStartupAuth({cfg:r,env:t,persist:true});return{auth:{token:o.auth.token,password:o.auth.password},generatedToken:o.generatedToken}}export{resolveBrowserControlAuth as n,shouldAutoGenerateBrowserAuth as r,ensureBrowserControlAuth as t};