openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 2.57 kB
JavaScript
import{hash,randomBytes}from"node:crypto";
//!src/agents/auth-profiles/oauth-refresh-marker.ts
const OAUTH_REFRESH_FENCE_PREFIX=`openclaw-oauth-refresh-fence:v1:`;const OAUTH_REFRESH_ACCESS_PATTERN=/^openclaw-oauth-refresh-fence:v1:([a-f0-9]{32}):(failed:)?access:([a-f0-9]{64})$/;const OAUTH_REFRESH_TOKEN_PATTERN=/^openclaw-oauth-refresh-fence:v1:([a-f0-9]{32}):(failed:)?refresh:([a-f0-9]{64})$/;function buildOAuthRefreshSecretDigest(e){const t=JSON.stringify([`openclaw.oauth-refresh-generation`,1,e.profileId,e.provider,e.kind,e.secret]);return hash(`sha256`,t,`hex`)}function parseOAuthRefreshFence(e){if(!e||e.type!==`oauth`||e.expires!==1)return;const t=OAUTH_REFRESH_ACCESS_PATTERN.exec(e.access);const n=OAUTH_REFRESH_TOKEN_PATTERN.exec(e.refresh);if(!t||!n||t[1]!==n[1]||t[2]!==n[2])return;return{accessDigest:t[3],claimId:t[1],refreshDigest:n[3],state:t[2]?`failed`:`pending`}}function createOAuthRefreshFence(e){const{access:t,refresh:n,expires:r,idToken:i,oauthRef:a,copyToAgents:o,...s}=e.credential;const c=randomBytes(16).toString(`hex`);const marker=(t,n)=>`${OAUTH_REFRESH_FENCE_PREFIX}${c}:${t}:${buildOAuthRefreshSecretDigest({profileId:e.profileId,provider:e.credential.provider,kind:t,secret:n})}`;return{...s,type:`oauth`,provider:e.credential.provider,access:marker(`access`,t),refresh:marker(`refresh`,n),expires:1}}function isOAuthRefreshFence(e){return parseOAuthRefreshFence(e)!==void 0}function isFailedOAuthRefreshFence(e){return parseOAuthRefreshFence(e)?.state===`failed`}function isPendingOAuthRefreshFence(e){return parseOAuthRefreshFence(e)?.state===`pending`}function createFailedOAuthRefreshFence(e){const t=parseOAuthRefreshFence(e);if(!t||t.state===`failed`)return e;return{...e,access:`${OAUTH_REFRESH_FENCE_PREFIX}${t.claimId}:failed:access:${t.accessDigest}`,refresh:`${OAUTH_REFRESH_FENCE_PREFIX}${t.claimId}:failed:refresh:${t.refreshDigest}`}}function isSameOAuthRefreshGeneration(e){if(e.left.provider!==e.right.provider)return false;return readOAuthRefreshGenerationDigest({profileId:e.profileId,credential:e.left})===readOAuthRefreshGenerationDigest({profileId:e.profileId,credential:e.right})}function readOAuthRefreshGenerationDigest(e){return parseOAuthRefreshFence(e.credential)?.refreshDigest??buildOAuthRefreshSecretDigest({profileId:e.profileId,provider:e.credential.provider,kind:`refresh`,secret:e.credential.refresh})}export{isPendingOAuthRefreshFence as a,isOAuthRefreshFence as i,createOAuthRefreshFence as n,isSameOAuthRefreshGeneration as o,isFailedOAuthRefreshFence as r,createFailedOAuthRefreshFence as t};