-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdecrypt.ts
More file actions
24 lines (22 loc) · 797 Bytes
/
decrypt.ts
File metadata and controls
24 lines (22 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// src/lib/notifications/decrypt.ts
import "server-only"
import { decrypt } from "@/lib/crypto"
import type { NotificationConfig } from "@/lib/notifications/types"
import { isNotificationConfig } from "@/lib/notifications/types"
export function decryptNotificationConfig(
target: { name: string; encryptedConfig: string },
key: Buffer
): NotificationConfig {
let parsed: unknown
try {
const json = decrypt(target.encryptedConfig, key)
parsed = JSON.parse(json)
} catch {
// never surface raw crypto or parse error details
throw new Error(`Config is missing or invalid for notification target "${target.name}"`)
}
if (!isNotificationConfig(parsed)) {
throw new Error(`Config is missing or invalid for notification target "${target.name}"`)
}
return parsed
}