/
callback-lambda.ts
41 lines (37 loc) · 1.15 KB
/
callback-lambda.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
41
import fetch from "node-fetch";
import { TaskResponse } from "./types";
import crypto = require("crypto");
export const handler = async (event: { detail: TaskResponse }) => {
try {
const { responseCallbackUrl, clientId, ...responsePayload } = event.detail;
const serializedPayload = JSON.stringify(responsePayload);
const response = await fetch(event.detail.responseCallbackUrl, {
method: "POST",
headers: {
"X-Hmac-SHA256": await computeHMAC(serializedPayload, clientId),
},
body: serializedPayload,
});
if (!response.ok) {
throw new Error(
`Error occured while calling back ${event.detail.responseCallbackUrl}.`
);
}
} catch (error) {
console.log(error);
return {
status: 500,
};
}
return {
status: 200,
};
};
const computeHMAC = async (data: string, clientId: string) => {
const key = await getKeyForClientId(clientId);
return crypto.createHmac("sha256", key).update(data).digest("hex");
};
const getKeyForClientId = async (clientId: string): Promise<string> => {
// TODO: get key for client from a reemote & secure repo
return Promise.resolve("42");
};