feat: unify device auth + pairing
This commit is contained in:
98
src/gateway/server-methods/devices.ts
Normal file
98
src/gateway/server-methods/devices.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import {
|
||||
approveDevicePairing,
|
||||
listDevicePairing,
|
||||
rejectDevicePairing,
|
||||
} from "../../infra/device-pairing.js";
|
||||
import {
|
||||
ErrorCodes,
|
||||
errorShape,
|
||||
formatValidationErrors,
|
||||
validateDevicePairApproveParams,
|
||||
validateDevicePairListParams,
|
||||
validateDevicePairRejectParams,
|
||||
} from "../protocol/index.js";
|
||||
import type { GatewayRequestHandlers } from "./types.js";
|
||||
|
||||
export const deviceHandlers: GatewayRequestHandlers = {
|
||||
"device.pair.list": async ({ params, respond }) => {
|
||||
if (!validateDevicePairListParams(params)) {
|
||||
respond(
|
||||
false,
|
||||
undefined,
|
||||
errorShape(
|
||||
ErrorCodes.INVALID_REQUEST,
|
||||
`invalid device.pair.list params: ${formatValidationErrors(
|
||||
validateDevicePairListParams.errors,
|
||||
)}`,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
const list = await listDevicePairing();
|
||||
respond(true, list, undefined);
|
||||
},
|
||||
"device.pair.approve": async ({ params, respond, context }) => {
|
||||
if (!validateDevicePairApproveParams(params)) {
|
||||
respond(
|
||||
false,
|
||||
undefined,
|
||||
errorShape(
|
||||
ErrorCodes.INVALID_REQUEST,
|
||||
`invalid device.pair.approve params: ${formatValidationErrors(
|
||||
validateDevicePairApproveParams.errors,
|
||||
)}`,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
const { requestId } = params as { requestId: string };
|
||||
const approved = await approveDevicePairing(requestId);
|
||||
if (!approved) {
|
||||
respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, "unknown requestId"));
|
||||
return;
|
||||
}
|
||||
context.broadcast(
|
||||
"device.pair.resolved",
|
||||
{
|
||||
requestId,
|
||||
deviceId: approved.device.deviceId,
|
||||
decision: "approved",
|
||||
ts: Date.now(),
|
||||
},
|
||||
{ dropIfSlow: true },
|
||||
);
|
||||
respond(true, approved, undefined);
|
||||
},
|
||||
"device.pair.reject": async ({ params, respond, context }) => {
|
||||
if (!validateDevicePairRejectParams(params)) {
|
||||
respond(
|
||||
false,
|
||||
undefined,
|
||||
errorShape(
|
||||
ErrorCodes.INVALID_REQUEST,
|
||||
`invalid device.pair.reject params: ${formatValidationErrors(
|
||||
validateDevicePairRejectParams.errors,
|
||||
)}`,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
const { requestId } = params as { requestId: string };
|
||||
const rejected = await rejectDevicePairing(requestId);
|
||||
if (!rejected) {
|
||||
respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, "unknown requestId"));
|
||||
return;
|
||||
}
|
||||
context.broadcast(
|
||||
"device.pair.resolved",
|
||||
{
|
||||
requestId,
|
||||
deviceId: rejected.deviceId,
|
||||
decision: "rejected",
|
||||
ts: Date.now(),
|
||||
},
|
||||
{ dropIfSlow: true },
|
||||
);
|
||||
respond(true, rejected, undefined);
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user