UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 3.98 kB
import{o as resolveRequiredOsHomeDir}from"./home-dir-DEN5DisQ.mjs";import{r as resolveStateDir}from"./state-dir-BwIClCab.mjs";import{n as ignoreMissingPath}from"./errno-DUBt6SQQ.mjs";import"./paths-D_FOvGon.mjs";import{h as resolveDatabasePathKey}from"./sqlite-worker-identity-CikEUuqF.mjs";import{Fn as object,Jn as string,Lt as boolean,Nt as array,Pn as number,Yt as discriminatedUnion,xn as literal}from"./schemas-DuCQi-Qb.mjs";import{t as requestJsonlSocket}from"./jsonl-socket-BNEYKrDi.mjs";import path from"node:path";import crypto from"node:crypto";import fs from"node:fs/promises"; //!src/infra/client-credential-ipc.ts const identity=object({identity_key:string(),device_id:string(),public_key_pem:string(),private_key_pem:string(),created_at_ms:number(),updated_at_ms:number()});const token=object({device_id:string(),role:string(),token:string(),scopes_json:string(),updated_at_ms:number()});const socket=object({socket_key:literal(`current`),socket_path:string().nullable(),socket_token:string().nullable(),updated_at_ms:number()});const resultSchema=discriminatedUnion(`kind`,[object({kind:literal(`identity`),identity:identity.nullable()}),object({kind:literal(`identityRepair`),identity,repaired:boolean(),rotated:boolean()}),object({kind:literal(`token`),token:token.nullable()}),object({kind:literal(`tokens`),tokens:array(token),nextRole:string().nullable()}),object({kind:literal(`socket`),socket:socket.nullable()}),object({kind:literal(`changed`),changed:boolean()}),object({kind:literal(`cleared`),count:number().int().nonnegative()})]);const responseSchema=discriminatedUnion(`ok`,[object({ok:literal(true),result:resultSchema}),object({ok:literal(false),error:object({code:string(),message:string()})})]);function controlDirectory(e){const t=e.OPENCLAW_PROFILE?.trim();if(t&&t.toLowerCase()!=="default"&&(!/^[a-z0-9][a-z0-9_-]{0,63}$/.test(t)||[`gateway`,`mac`,`node`].includes(t)))throw new Error(`Client credentials require a valid macOS app profile.`);return path.join(resolveRequiredOsHomeDir(e),`.openclaw${t&&t.toLowerCase()!=="default"?`-${t}`:``}`)}async function requestClientCredentialsFromApp(e,t,n,r){if(process.platform!==`darwin`)return;const i=controlDirectory(e);const a=path.join(i,`mac-control.token`);const o=await fs.open(a,fs.constants.O_RDONLY|fs.constants.O_NOFOLLOW).catch(ignoreMissingPath);if(!o)return;let s;try{const e=await o.stat();if(!e.isFile()||e.nlink!==1||e.size<1||e.size>256||(e.mode&511)!==384||process.getuid&&e.uid!==process.getuid())throw new Error(`App control credentials are unsafe; restart the matching OpenClaw app.`);s=(await o.readFile(`utf8`)).replace(/\n+$/,``);if(!s)throw new Error(`App control credentials are empty; restart the matching OpenClaw app.`)}finally{await o.close()}n?.throwIfAborted();r?.();const c=JSON.stringify({operation:`client.credentials`,credentialRoot:resolveDatabasePathKey(resolveStateDir(e)),credentialOperation:t});const l=crypto.randomUUID();const u=Date.now();const d=JSON.stringify({id:crypto.randomUUID(),nonce:l,ts:u,requestJson:c,hmac:crypto.createHmac(`sha256`,s).update(`${l}:${u}:${c}`).digest(`hex`)});if(Buffer.byteLength(d)>=65536)throw new Error(`Client credential request exceeds the app control limit.`);const f=await requestJsonlSocket({socketPath:path.join(i,`mac-control.sock`),requestLine:d,timeoutMs:15e3,signal:n,beforeSend:()=>{n?.throwIfAborted();r?.()},maxResponseLineBytes:65535,accept:e=>({kind:`response`,response:responseSchema.parse(e)}),onResponseLost:()=>({kind:`lost`}),onTimeout:()=>({kind:`lost`})});n?.throwIfAborted();if(!f)return;if(f.kind===`lost`)throw new Error(`The client credential owner did not reply; retry through the matching OpenClaw app.`);const{response:p}=f;if(!p.ok){const{code:e,message:t}=p.error;if(e===`credential_root_unowned`||e===`invalid_request`&&t===`Unknown control operation.`)return;throw new Error(`Client credential owner refused the operation: ${t}`)}return p.result}export{requestClientCredentialsFromApp};