webRemoteControlRelayAuthStorageProvider.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import type { ICredentialService } from "@zcode/services";
  2. import type { AppSettings } from "@zcode/shared";
  3. export const WEB_REMOTE_CONTROL_EXTERNAL_RELAY_PASS_HASH_CREDENTIAL_KEY =
  4. "web-remote-control:external-relay:pass_hash";
  5. export interface WebRemoteControlRelayStoredAuth {
  6. deviceSid: string;
  7. passHash: string;
  8. }
  9. export interface WebRemoteControlRelayAuthStorageProvider {
  10. load(): Promise<WebRemoteControlRelayStoredAuth | undefined>;
  11. save(auth: WebRemoteControlRelayStoredAuth): Promise<void>;
  12. clear(): Promise<void>;
  13. rotate(auth: WebRemoteControlRelayStoredAuth): Promise<void>;
  14. }
  15. interface WebRemoteControlRelayAuthStorageDependencies {
  16. credentialService: Pick<ICredentialService, "load" | "save" | "delete">;
  17. loadSettings(): Promise<AppSettings>;
  18. patchSettings(patch: Partial<AppSettings>): Promise<void>;
  19. logger?: {
  20. info: (...args: unknown[]) => void;
  21. warn: (...args: unknown[]) => void;
  22. };
  23. }
  24. function safeAuthLogFields(auth: Partial<WebRemoteControlRelayStoredAuth>) {
  25. const deviceSid = auth.deviceSid?.trim();
  26. return {
  27. hasDeviceSid: Boolean(deviceSid),
  28. deviceSidSuffix: deviceSid ? deviceSid.slice(-6) : undefined,
  29. hasPassHash: Boolean(auth.passHash?.trim()),
  30. };
  31. }
  32. export function createWebRemoteControlRelayAuthStorageProvider({
  33. credentialService,
  34. loadSettings,
  35. patchSettings,
  36. logger,
  37. }: WebRemoteControlRelayAuthStorageDependencies): WebRemoteControlRelayAuthStorageProvider {
  38. const clear = async () => {
  39. await patchSettings({ webRemoteControlExternalRelayDevice: undefined });
  40. await credentialService.delete(WEB_REMOTE_CONTROL_EXTERNAL_RELAY_PASS_HASH_CREDENTIAL_KEY);
  41. logger?.info("[web-remote-control] external relay auth cleared", {
  42. hasDeviceSid: false,
  43. hasPassHash: false,
  44. });
  45. };
  46. return {
  47. async load(): Promise<WebRemoteControlRelayStoredAuth | undefined> {
  48. const settings = await loadSettings();
  49. const deviceSid = settings.webRemoteControlExternalRelayDevice?.deviceSid.trim();
  50. const passHash = (
  51. await credentialService.load(WEB_REMOTE_CONTROL_EXTERNAL_RELAY_PASS_HASH_CREDENTIAL_KEY)
  52. )?.trim();
  53. if (!deviceSid && !passHash) {
  54. return undefined;
  55. }
  56. if (!deviceSid || !passHash) {
  57. logger?.warn("[web-remote-control] external relay auth partial state cleared", {
  58. hasDeviceSid: Boolean(deviceSid),
  59. hasPassHash: Boolean(passHash),
  60. });
  61. await clear();
  62. return undefined;
  63. }
  64. logger?.info("[web-remote-control] external relay auth loaded", {
  65. ...safeAuthLogFields({ deviceSid, passHash }),
  66. });
  67. return { deviceSid, passHash };
  68. },
  69. async save(auth: WebRemoteControlRelayStoredAuth): Promise<void> {
  70. await patchSettings({
  71. webRemoteControlExternalRelayDevice: { deviceSid: auth.deviceSid },
  72. });
  73. await credentialService.save(
  74. WEB_REMOTE_CONTROL_EXTERNAL_RELAY_PASS_HASH_CREDENTIAL_KEY,
  75. auth.passHash,
  76. );
  77. logger?.info("[web-remote-control] external relay auth saved", {
  78. ...safeAuthLogFields(auth),
  79. });
  80. },
  81. clear,
  82. async rotate(auth: WebRemoteControlRelayStoredAuth): Promise<void> {
  83. await clear();
  84. await this.save(auth);
  85. logger?.info("[web-remote-control] external relay auth rotated", {
  86. ...safeAuthLogFields(auth),
  87. });
  88. },
  89. };
  90. }