Files
openclaw/src/gateway/device-auth.ts
2026-02-26 14:11:04 +01:00

59 lines
1.3 KiB
TypeScript

export type DeviceAuthPayloadParams = {
deviceId: string;
clientId: string;
clientMode: string;
role: string;
scopes: string[];
signedAtMs: number;
token?: string | null;
nonce: string;
};
export type DeviceAuthPayloadV3Params = DeviceAuthPayloadParams & {
platform?: string | null;
deviceFamily?: string | null;
};
function normalizeMetadataField(value?: string | null): string {
if (typeof value !== "string") {
return "";
}
return value.trim().toLowerCase();
}
export function buildDeviceAuthPayload(params: DeviceAuthPayloadParams): string {
const scopes = params.scopes.join(",");
const token = params.token ?? "";
return [
"v2",
params.deviceId,
params.clientId,
params.clientMode,
params.role,
scopes,
String(params.signedAtMs),
token,
params.nonce,
].join("|");
}
export function buildDeviceAuthPayloadV3(params: DeviceAuthPayloadV3Params): string {
const scopes = params.scopes.join(",");
const token = params.token ?? "";
const platform = normalizeMetadataField(params.platform);
const deviceFamily = normalizeMetadataField(params.deviceFamily);
return [
"v3",
params.deviceId,
params.clientId,
params.clientMode,
params.role,
scopes,
String(params.signedAtMs),
token,
params.nonce,
platform,
deviceFamily,
].join("|");
}