UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines • 3.46 kB
import{n as runClientCredentialOperationAsync}from"./client-credential-async-Cjr3lnxO.mjs";import{n as normalizeDeviceAuthScopes,t as normalizeDeviceAuthRole}from"./device-auth-BxeuYK8r.mjs"; //!src/infra/device-auth-store.kernel.ts function deviceAuthEntryFromRow(e){try{const t=JSON.parse(e.scopes_json);if(!Array.isArray(t))return null;return{token:e.token,role:e.role,scopes:normalizeDeviceAuthScopes(t),updatedAtMs:e.updated_at_ms}}catch{return null}}function createDeviceAuthEntry(e){return{token:e.token,role:normalizeDeviceAuthRole(e.role),scopes:normalizeDeviceAuthScopes(e.scopes),updatedAtMs:e.updatedAtMs??Date.now()}} //!src/infra/device-auth-store.ts function captureDeviceAuthOperation(e){const t={...e.env??process.env};const{signal:n,assertCurrent:r}=e;const assertActive=()=>{n?.throwIfAborted();r?.()};assertActive();return{env:t,signal:n,assertCurrent:assertActive}}function tokenKey(e,t){return{device_id:e.deviceId,role:normalizeDeviceAuthRole(e.role),...t===void 0?{}:{gateway_scope:t}}}async function readDeviceAuth(e,t){const n=captureDeviceAuthOperation(e);const r=await runClientCredentialOperationAsync(n.env,{kind:`token.read`,key:tokenKey(e,t)},n);if(r.kind!==`token`)throw new Error(`Client credential owner returned an unexpected token result.`);n.assertCurrent();const i={entry:r.token?deviceAuthEntryFromRow(r.token):null,expectedToken:r.token?.token??null};e.onSnapshot?.(i);return i.entry}function loadDeviceAuthToken(e){return readDeviceAuth(e)}const loadDeviceAuthTokenReadOnly=loadDeviceAuthToken;async function loadDeviceAuthTokens(e){const t=captureDeviceAuthOperation(e);const n=[];let r;do{const i=await runClientCredentialOperationAsync(t.env,{kind:`token.list`,device_id:e.deviceId,afterRole:r},t);if(i.kind!==`tokens`)throw new Error(`Client credential owner returned an unexpected token list.`);for(const e of i.tokens){const t=deviceAuthEntryFromRow(e);if(t)n.push(t)}r=i.nextRole??void 0}while(r!==void 0);t.assertCurrent();return n}async function storeToken(e,t){const n=captureDeviceAuthOperation(e);const r=createDeviceAuthEntry(e);const i=await runClientCredentialOperationAsync(n.env,{kind:`token.replace`,key:tokenKey(e,t),expectedToken:e.expectedToken,token:{device_id:e.deviceId,role:r.role,token:r.token,scopes_json:JSON.stringify(r.scopes),updated_at_ms:r.updatedAtMs}},n);if(i.kind!==`changed`)throw new Error(`Client credential owner returned an unexpected token write result.`);return i.changed?r:null}async function clearToken(e,t){const n=captureDeviceAuthOperation(e);const r=await runClientCredentialOperationAsync(n.env,{kind:`token.remove`,key:tokenKey(e,t),expectedToken:e.expectedToken,observedToken:e.observedToken},n);if(r.kind!==`changed`)throw new Error(`Client credential owner returned an unexpected token removal result.`);return r.changed}function storeDeviceAuthToken(e){return storeToken(e)}function clearDeviceAuthToken(e){return clearToken(e)}function loadOriginDeviceToken(e){return readDeviceAuth(e,e.gatewayScope)}const loadOriginDeviceTokenReadOnly=loadOriginDeviceToken;function storeOriginDeviceToken(e){return storeToken(e,e.gatewayScope)}function clearOriginDeviceToken(e){return clearToken(e,e.gatewayScope)}export{loadDeviceAuthTokens as a,storeDeviceAuthToken as c,loadDeviceAuthTokenReadOnly as i,storeOriginDeviceToken as l,clearOriginDeviceToken as n,loadOriginDeviceToken as o,loadDeviceAuthToken as r,loadOriginDeviceTokenReadOnly as s,clearDeviceAuthToken as t};