-
Notifications
You must be signed in to change notification settings - Fork 10
/
integrationConfigProvider.ts
40 lines (33 loc) · 1.21 KB
/
integrationConfigProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import * as crypto from 'js-sha256';
import {hex2bin} from "./queueitHelpers";
const __IntegrationConfixFieldName = "info";
export async function tryStoreIntegrationConfig(request: any, integrationConfigKV: any, secretKey: string) {
const bodyJSON = await request.clone().json();
const hash = bodyJSON.hash;
const configInHex = bodyJSON.integrationInfo;
if (console && console.warn) {
if (!hash) {
console.warn("Hash is missing");
}
if (!configInHex) {
console.warn("Integration configuration is missing");
}
if (integrationConfigKV == null) {
console.warn("IntegrationConfigKV is not available");
}
}
if (!(hash && configInHex)) {
return false;
}
if (crypto.sha256.hmac(secretKey, configInHex) !== hash) {
if (console && console.warn) {
console.warn("Hash didn't match the expected value");
}
return false;
}
await integrationConfigKV.put(__IntegrationConfixFieldName, hex2bin(configInHex));
return true;
}
export async function getIntegrationConfig(integrationConfigKV: any) {
return await integrationConfigKV.get(__IntegrationConfixFieldName, "text");
}