openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.27 kB
JavaScript
import{s as normalizeBrowserPairCode,t as BROWSER_PAIR_IDENTITY_REQUIRED_MESSAGE}from"./browser-pair-CVYTNOrU.mjs";import{r as captureOpenClawStateWorkerContext}from"./openclaw-state-worker-context-Dx8YjqO2.mjs";import{$ as validateDevicePairCodeParams}from"./src-BDZMFmKa.mjs";import{i as ErrorCodes}from"./missing-scope-error-CkgBgh6d.mjs";import{p as errorShape}from"./error-codes-CQNGhXT-.mjs";import{t as isNamedOperatorRoleEnforcementEnabled}from"./operator-role-enforcement-FsaRvImR.mjs";import{r as respondUnavailableOnThrow}from"./response-BYbuAh7a.mjs";import{n as defineValidatedGatewayHandler}from"./validation-CM41YclJ.mjs";import{a as readGatewayRequestMutationAuthority}from"./session-mutation-guards-Bqnc5JYL.mjs";import{i as inspectBrowserPairRequest,r as decideBrowserPairRequest}from"./device-pairing-browser-code-Bbr0AK6W.mjs";import{t as broadcastRejectedDevicePairing}from"./pairing-events-BXsG23Nv.mjs";import{o as resolveDeviceSessionAuthz}from"./device-management-authz-CCyFphnk.mjs";
//!src/gateway/server-methods/device-pair-code.ts
const REFUSALS={unknown:`No browser is waiting for this code. Check the code shown in the browser.`,expired:`This code has expired. Choose Pair this browser again to get a new one.`,decided:`This code was already answered. Choose Pair this browser again to get a new one.`};function formatReducedRefusal(e){return`This browser is already paired with revoked or narrower access, and a code does not lift that limit. To restore administrator access, run \`openclaw dashboard\` on the Gateway host and open its link in that browser, or run \`openclaw devices remove ${e}\` and pair again.`}const devicePairCodeHandlers={"device.pair.code":defineValidatedGatewayHandler(`device.pair.code`,validateDevicePairCodeParams,async e=>{const{params:t,respond:n,context:r,client:i}=e;await respondUnavailableOnThrow(n,async()=>{const{requestId:a,decision:o}=t;const s=normalizeBrowserPairCode(t.code);if(!s||Boolean(a)!==Boolean(o)){n(false,void 0,errorShape(ErrorCodes.INVALID_REQUEST,REFUSALS.unknown));return}const c={context:captureOpenClawStateWorkerContext(),assertCurrent:readGatewayRequestMutationAuthority(e).assertCurrent};const refuse=e=>n(false,void 0,errorShape(ErrorCodes.INVALID_REQUEST,e));if(!a||!o){const e=await inspectBrowserPairRequest({code:s,...c});if(!(`requestId`in e)){refuse(REFUSALS[e.status]);return}n(true,e,void 0);return}const assertPairingSupported=()=>{c.assertCurrent();if(o===`approve`&&isNamedOperatorRoleEnforcementEnabled(r.getRuntimeConfig()))throw new Error(BROWSER_PAIR_IDENTITY_REQUIRED_MESSAGE)};assertPairingSupported();const l=await decideBrowserPairRequest({code:s,requestId:a,decision:o,callerScopes:resolveDeviceSessionAuthz(i).callerScopes,readSharedGatewaySessionGeneration:()=>r.sharedGatewaySessionGenerationState?.requiredGeneration,...c,assertCurrent:assertPairingSupported});if(l.status===`reduced`){refuse(formatReducedRefusal(l.deviceId));return}if(!(`requestId`in l)){refuse(REFUSALS[l.status]);return}const{superseded:u,...d}=l;if(d.status===`approved`){const e=Date.now();for(const t of u)broadcastRejectedDevicePairing(r.broadcast,t,e);r.logGateway.info(`browser pairing approved device=${d.deviceId}`)}n(true,d,void 0)})})};export{devicePairCodeHandlers};