|
| 1 | +import type { GatewayConnectAuthSelection } from "@openclaw/gateway-client/browser"; |
| 2 | +import { loadDeviceAuthToken, peekStoredDeviceIdentityId } from "../lib/nodes/index.ts"; |
| 3 | + |
| 4 | +const CONTROL_UI_OPERATOR_ROLE = "operator"; |
| 5 | + |
| 6 | +export function storedDeviceTokenScopesAllowRead(role: string, scopes: readonly string[]): boolean { |
| 7 | + return ( |
| 8 | + role !== CONTROL_UI_OPERATOR_ROLE || |
| 9 | + scopes.includes("operator.read") || |
| 10 | + scopes.includes("operator.write") || |
| 11 | + scopes.includes("operator.admin") |
| 12 | + ); |
| 13 | +} |
| 14 | + |
| 15 | +/** True when the next browser connect would present a usable stored credential. */ |
| 16 | +export function hasStoredGatewayAuth(params: { |
| 17 | + gatewayUrl: string; |
| 18 | + token?: string; |
| 19 | + password?: string; |
| 20 | +}): boolean { |
| 21 | + if (params.token?.trim() || params.password?.trim()) { |
| 22 | + return true; |
| 23 | + } |
| 24 | + // Insecure contexts skip device identity, so their stored token is unusable. |
| 25 | + if (typeof crypto === "undefined" || !crypto.subtle) { |
| 26 | + return false; |
| 27 | + } |
| 28 | + const deviceId = peekStoredDeviceIdentityId(); |
| 29 | + if (!deviceId) { |
| 30 | + return false; |
| 31 | + } |
| 32 | + const storedEntry = loadDeviceAuthToken({ |
| 33 | + deviceId, |
| 34 | + gatewayUrl: params.gatewayUrl, |
| 35 | + role: CONTROL_UI_OPERATOR_ROLE, |
| 36 | + }); |
| 37 | + return Boolean( |
| 38 | + storedEntry && storedDeviceTokenScopesAllowRead(CONTROL_UI_OPERATOR_ROLE, storedEntry.scopes), |
| 39 | + ); |
| 40 | +} |
| 41 | + |
| 42 | +export function gatewayRecoveryScopeMaterial( |
| 43 | + selected: GatewayConnectAuthSelection, |
| 44 | +): string | undefined { |
| 45 | + return selected.authDeviceToken ?? selected.resolvedDeviceToken ?? selected.authToken; |
| 46 | +} |
| 47 | + |
| 48 | +async function deriveRecoveryScope(scopeMaterial: string | undefined): Promise<string> { |
| 49 | + if (!scopeMaterial || typeof crypto === "undefined" || !crypto.subtle) { |
| 50 | + return ""; |
| 51 | + } |
| 52 | + try { |
| 53 | + // Recovery records contain the unsent task. Bind them to the exact |
| 54 | + // credential without persisting that credential in browser storage. |
| 55 | + const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(scopeMaterial)); |
| 56 | + return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join( |
| 57 | + "", |
| 58 | + ); |
| 59 | + } catch { |
| 60 | + return ""; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +export class GatewayRecoveryScopeTracker { |
| 65 | + scope = ""; |
| 66 | + ready = false; |
| 67 | + private generation = 0; |
| 68 | + |
| 69 | + begin(generation: number) { |
| 70 | + this.generation = generation; |
| 71 | + this.ready = false; |
| 72 | + } |
| 73 | + |
| 74 | + async resolve(params: { |
| 75 | + generation: number; |
| 76 | + scopeMaterial?: string; |
| 77 | + isConnected: () => boolean; |
| 78 | + }): Promise<boolean> { |
| 79 | + const scope = await deriveRecoveryScope(params.scopeMaterial); |
| 80 | + if (params.generation !== this.generation || !params.isConnected()) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + this.scope = scope; |
| 84 | + this.ready = true; |
| 85 | + return true; |
| 86 | + } |
| 87 | +} |
0 commit comments