webRemoteControlRelayAuthProvider.ts 838 B

123456789101112131415161718192021222324
  1. import { createHash, createHmac, randomBytes } from "node:crypto";
  2. import type { WebRemoteControlRelayRole } from "@zcode/shared";
  3. export interface WebRemoteControlRelayAuthProvider {
  4. createPassword(): string;
  5. createPassHash(password: string): string;
  6. calculateProof(
  7. passHash: string,
  8. nonce: string,
  9. role: WebRemoteControlRelayRole,
  10. deviceSid: string,
  11. ): string;
  12. }
  13. export function createNodeWebRemoteControlRelayAuthProvider(): WebRemoteControlRelayAuthProvider {
  14. return {
  15. createPassword: () => randomBytes(24).toString("base64url"),
  16. createPassHash: (password) => createHash("sha256").update(password).digest("base64"),
  17. calculateProof: (passHash, nonce, role, deviceSid) =>
  18. createHmac("sha256", passHash)
  19. .update(`${nonce}|${role}|${deviceSid}`)
  20. .digest("base64url"),
  21. };
  22. }