mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-25 20:05:46 -06:00
66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
// Gateway Client module implements device auth behavior.
|
|
export function normalizeDeviceMetadataForAuth(value?: string | null): string {
|
|
if (typeof value !== "string") {
|
|
return "";
|
|
}
|
|
const trimmed = value.trim();
|
|
if (!trimmed) {
|
|
return "";
|
|
}
|
|
return trimmed.replace(/[A-Z]/g, (char) => String.fromCharCode(char.charCodeAt(0) + 32));
|
|
}
|
|
|
|
type DeviceAuthPayloadParams = {
|
|
deviceId: string;
|
|
clientId: string;
|
|
clientMode: string;
|
|
role: string;
|
|
scopes: string[];
|
|
signedAtMs: number;
|
|
token?: string | null;
|
|
nonce: string;
|
|
};
|
|
|
|
type DeviceAuthPayloadV3Params = DeviceAuthPayloadParams & {
|
|
platform?: string | null;
|
|
deviceFamily?: string | null;
|
|
};
|
|
|
|
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 ?? "";
|
|
// Device signatures are byte-for-byte compared by the gateway. Normalize
|
|
// optional metadata before joining so case differences do not break auth.
|
|
const platform = normalizeDeviceMetadataForAuth(params.platform);
|
|
const deviceFamily = normalizeDeviceMetadataForAuth(params.deviceFamily);
|
|
return [
|
|
"v3",
|
|
params.deviceId,
|
|
params.clientId,
|
|
params.clientMode,
|
|
params.role,
|
|
scopes,
|
|
String(params.signedAtMs),
|
|
token,
|
|
params.nonce,
|
|
platform,
|
|
deviceFamily,
|
|
].join("|");
|
|
}
|