UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.01 kB
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{t as stableStringify}from"./stable-stringify-Cy0VZ0ci.mjs";import{t as sha256Base64Url}from"./crypto-digest-T8X5EZni.mjs";import{h as readRuntimeConfig}from"./runtime-snapshot-Bg5K4JNu.mjs";import{p as isOperatorScope}from"./operator-scopes-bEvB7E5X.mjs";import{o as roleScopesAllow}from"./operator-scope-compat-TmgAKmD2.mjs";import{t as resolveIdentityOperatorScopes}from"./operator-identity-scopes-DxmAiLZL.mjs";import{i as resolveGatewayAuthForConfig}from"./auth-resolve-UwT9_UjV.mjs";import{n as checkGatewayWsBrowserOrigin}from"./origin-check-CngtffS_.mjs";import{a as isGatewayDelegationCurrent}from"./trusted-proxy-delegation-Cn2wiXdt.mjs"; //!src/gateway/auth-policy.ts const policies=/* @__PURE__ */ new WeakMap;function captureGatewayAuthPolicy(e,t){let n=policies.get(e);const r=t&&n?.captured.get(t);if(r)return r;const i=normalizeOptionalString(t?.verifiedIdentity);const a=t?.role;const o=t?.authMethod;const s=t?.authModeOverride;const c=t?.browserOrigin;const l=t?.delegation;const u=c?.remoteControlUiIngress!==void 0||l!==void 0;if(!n){const t=e.gateway;const r=t?.auth?.trustedProxy;n={global:stableStringify({trustedProxies:t?.trustedProxies?.toSorted(),allowRealIpFallback:t?.allowRealIpFallback,allowTailscale:t?.auth?.allowTailscale,trustedProxy:r&&{...r,requiredHeaders:(r.requiredHeaders??[]).toSorted(),allowUsers:(r.allowUsers??[]).toSorted()}}),identities:/* @__PURE__ */ new Map,captured:/* @__PURE__ */ new WeakMap};policies.set(e,n)}const d=u?void 0:stableStringify([a,i,o,s,c]);let f=d===void 0?void 0:n.identities.get(d);if(!f){const t=a===`operator`&&i?[...new Set(l?.scopes??resolveIdentityOperatorScopes(i,e.gateway?.auth?.identityScopes))].toSorted():void 0;let r;let u;let p;if(o===`trusted-proxy`||o===`tailscale`||o===`none`||o===`password`){const t=resolveGatewayAuthForConfig({config:e,authOverride:s?{mode:s}:void 0,tailscaleMode:e.gateway?.tailscale?.mode});if(o===`trusted-proxy`){r=t.mode===`trusted-proxy`&&t.trustedProxy!==void 0;const n=t.trustedProxy?.allowUsers;u=Boolean(l?isGatewayDelegationCurrent(l,e):i&&(!n?.length||n.includes(i)))}else if(o===`tailscale`)r=t.allowTailscale&&t.mode!==`trusted-proxy`&&t.mode!==`none`;else if(o===`none`)r=t.mode===`none`;else if(t.mode===`trusted-proxy`){r=Boolean(t.password);p=t.password?sha256Base64Url(t.password):void 0}}f=Object.freeze({generation:n.global,grantGeneration:stableStringify({grant:t,methodEnabled:r,proxyUserAllowed:u,fallbackCredentialGeneration:p,browserOriginAllowed:c?checkGatewayWsBrowserOrigin(c,e).ok:void 0}),role:a,authMethod:o,authModeOverride:s,verifiedIdentity:i,delegation:l,browserOrigin:c&&Object.freeze({...c})});if(d!==void 0){n.identities.set(d,f);n.captured.set(f,f)}}return f}function areGatewayAuthGrantsEquivalent(e,t){if(e===t)return true;const n=/^(.*"grant":)(\[[^\]]*\])(,.*)$/u;const r=n.exec(e);const i=n.exec(t);if(!r||!i||r[1]!==i[1]||r[3]!==i[3])return false;try{const e=JSON.parse(r[2]);const t=JSON.parse(i[2]);if(!Array.isArray(e)||!e.every(isOperatorScope)||!Array.isArray(t)||!t.every(isOperatorScope))return false;return roleScopesAllow({role:`operator`,requestedScopes:e,allowedScopes:t})&&roleScopesAllow({role:`operator`,requestedScopes:t,allowedScopes:e})}catch{return false}}function isGatewayAuthPolicyCurrent(e,t){if(!e)return true;const n=t===void 0?readRuntimeConfig():t;if(n===null||e.delegation&&!isGatewayDelegationCurrent(e.delegation,n))return false;const r=captureGatewayAuthPolicy(n,e);return r.generation===e.generation&&areGatewayAuthGrantsEquivalent(e.grantGeneration,r.grantGeneration)}function isGatewayAuthGrantCurrent(e,t){if(!e)return true;const n=t===void 0?readRuntimeConfig():t;return n!==null&&(!e.delegation||isGatewayDelegationCurrent(e.delegation,n))&&areGatewayAuthGrantsEquivalent(e.grantGeneration,captureGatewayAuthPolicy(n,e).grantGeneration)}export{isGatewayAuthGrantCurrent as n,isGatewayAuthPolicyCurrent as r,captureGatewayAuthPolicy as t};