UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.18 kB
import{l as resolveSecretSentinel,s as looksLikeSecretSentinel}from"./sentinel-BjpdrOLs.mjs";import crypto from"node:crypto"; //!src/agents/execution-auth-binding.ts const authBindingFingerprintKey=crypto.randomBytes(32);function hashAuthBinding(e){return crypto.createHmac(`sha256`,authBindingFingerprintKey).update(JSON.stringify(e)).digest(`hex`)}function normalizeIdentity(e,t=false){const n=e?.trim();return n?t?n.toLowerCase():n:void 0}function fingerprintAuthProfileOwnerShape(e){const t=e.credential;if(!t)return;switch(t.type){case`api_key`:case`token`:return hashAuthBinding([`profile-owner-v1`,e.profileId,t.type,t.provider,(t.type===`api_key`?t.keyRef:t.tokenRef)??null,normalizeIdentity(t.email,true)??null,normalizeIdentity(t.displayName)??null,...t.type===`api_key`?[t.metadata??null]:[]]);case`oauth`:{const n=decodeJwtIdentity(t.idToken);return hashAuthBinding([`profile-owner-v1`,e.profileId,t.type,t.provider,normalizeIdentity(t.accountId)??n.subject??null,normalizeIdentity(t.email,true)??n.email??null,t.clientId??null,t.enterpriseUrl??null,t.projectId??null])}}}function fingerprintOpaqueRuntimeOwner(e){const t=e.runtimeArtifactFingerprint;const n=normalizeIdentity(e.authProfileId);if(n&&!e.authProfileOwnerFingerprint)return;if(!n&&e.skipLocalCredential)return;if((e.kind===`cli-runtime`||e.kind===`plugin-harness`)&&!t)return;return hashAuthBinding([e.kind===`aws-sdk`?`opaque-runtime-owner-v1`:`opaque-runtime-owner-v2`,e.kind,e.runner,e.provider.trim(),e.backendId,e.backendConfig??null,n??null,e.authProfileOwnerFingerprint??null,e.authSource??null,e.skipLocalCredential===true,t??null])}function fingerprintAwsSdkRuntimeOwner(e){if(e.auth?.mode!==`aws-sdk`||e.auth.apiKey)return;const t=e.env??process.env;let n;if(t.AWS_BEARER_TOKEN_BEDROCK?.trim())n=[`bearer`,hashAuthBinding(t.AWS_BEARER_TOKEN_BEDROCK.trim())];else if(t.AWS_PROFILE?.trim())return;else if(t.AWS_ACCESS_KEY_ID?.trim()&&t.AWS_SECRET_ACCESS_KEY?.trim())n=[`access-key`,t.AWS_ACCESS_KEY_ID.trim(),hashAuthBinding([t.AWS_SECRET_ACCESS_KEY.trim(),t.AWS_SESSION_TOKEN?.trim()??null])];else return;return fingerprintOpaqueRuntimeOwner({kind:`aws-sdk`,runner:`embedded`,provider:e.provider,backendId:e.backendId,authSource:hashAuthBinding([e.auth.source,n])})}function decodeJwtIdentity(e){const t=e?.split(`.`)[1];if(!t)return{};try{const e=JSON.parse(Buffer.from(t,`base64url`).toString(`utf8`));return{subject:typeof e.sub===`string`?normalizeIdentity(e.sub):void 0,email:typeof e.email===`string`?normalizeIdentity(e.email,true):void 0}}catch{return{}}}function fingerprintAuthProfileCredential(e){const t=e.credential;switch(t.type){case`api_key`:case`token`:{const n=t.type===`api_key`?t.key:t.token;if(!n)return;return hashAuthBinding([t.type,e.profileId,t.provider,n,(t.type===`api_key`?t.keyRef:t.tokenRef)??null,t.email??null,t.displayName??null,...t.type===`api_key`?[t.metadata??null]:[]])}case`oauth`:{const n=decodeJwtIdentity(t.idToken);const r=normalizeIdentity(t.accountId)??n.subject;const i=normalizeIdentity(t.email,true)??n.email;const a=r??i;const o=a?null:[t.access,t.refresh,t.idToken??null];if(!a&&!t.access&&!t.refresh&&!t.idToken)return;return hashAuthBinding([`oauth`,e.profileId,t.provider,t.clientId??null,r??null,i??null,t.enterpriseUrl??null,t.projectId??null,o])}}}function fingerprintResolvedAuthProfileCredential(e){const t=e.credential;if(t.type===`oauth`)return fingerprintAuthProfileCredential({profileId:e.profileId,credential:t});if(e.resolvedAuth&&e.resolvedAuth.profileId!==e.profileId)return;const n=t.type===`api_key`?t.key:t.token;const r=e.resolvedAuth?.apiKey??n;if(!r)return;return fingerprintAuthProfileCredential({profileId:e.profileId,credential:t.type===`api_key`?{...t,key:r}:{...t,token:r}})}function fingerprintResolvedProviderAuth(e){if(!e?.apiKey)return;const t=looksLikeSecretSentinel(e.apiKey)?resolveSecretSentinel(e.apiKey):e.apiKey;if(!t)return;return hashAuthBinding([`resolved-v2`,e.mode,t])}export{fingerprintResolvedAuthProfileCredential as a,fingerprintOpaqueRuntimeOwner as i,fingerprintAuthProfileOwnerShape as n,fingerprintResolvedProviderAuth as o,fingerprintAwsSdkRuntimeOwner as r,fingerprintAuthProfileCredential as t};