Skip to content

Commit

Permalink
Fix signature
Browse files Browse the repository at this point in the history
  • Loading branch information
mjh1 committed Feb 1, 2024
1 parent 4d6276d commit 9490476
Showing 1 changed file with 52 additions and 46 deletions.
98 changes: 52 additions & 46 deletions packages/api/src/webhooks/cannon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,16 +442,22 @@ export default class WebhookCannon {
errorMessage = e.message;
await this.retry(trigger, params, e);
} finally {
await storeResponse(
webhook,
event.id,
event.event,
resp,
startTime,
responseBody,
webhook.sharedSecret,
params
);
try {
await storeResponse(
webhook,
event.id,
event.event,
resp,
startTime,
responseBody,
webhook.sharedSecret,
params
);
} catch (e) {
console.log(
`Unable to store response of webhook ${webhook.id} url: ${webhook.url}`
);
}
await this.storeTriggerStatus(
trigger.webhook,
triggerTime,
Expand Down Expand Up @@ -591,41 +597,35 @@ async function storeResponse(
sharedSecret: string,
params
): Promise<WebhookResponse> {
try {
const hrDuration = process.hrtime(startTime);
const encodedResponseBody = Buffer.from(
responseBody.substring(0, 1024)
).toString("base64");

const webhookResponse = {
id: uuid(),
webhookId: webhook.id,
eventId: eventId,
event: eventName,
userId: webhook.userId,
createdAt: Date.now(),
duration: hrDuration[0] + hrDuration[1] / 1e9,
response: {
body: encodedResponseBody,
redirected: resp.redirected,
status: resp.status,
statusText: resp.statusText,
},
request: {
url: webhook.url,
body: params.body,
method: params.method,
headers: params.headers,
},
sharedSecret: sharedSecret,
};
await db.webhookResponse.create(webhookResponse);
return webhookResponse;
} catch (e) {
console.log(
`Unable to store response of webhook ${webhook.id} url: ${webhook.url}`
);
}
const hrDuration = process.hrtime(startTime);
const encodedResponseBody = Buffer.from(
responseBody.substring(0, 1024)
).toString("base64");

const webhookResponse = {
id: uuid(),
webhookId: webhook.id,
eventId: eventId,
event: eventName,
userId: webhook.userId,
createdAt: Date.now(),
duration: hrDuration[0] + hrDuration[1] / 1e9,
response: {
body: encodedResponseBody,
redirected: resp.redirected,
status: resp.status,
statusText: resp.statusText,
},
request: {
url: webhook.url,
body: params.body,
method: params.method,
headers: params.headers,
},
sharedSecret: sharedSecret,
};
await db.webhookResponse.create(webhookResponse);
return webhookResponse;
}

export async function resendWebhook(
Expand All @@ -639,10 +639,16 @@ export async function resendWebhook(
let statusCode: number;
let errorMessage: string;
try {
const timestamp = Date.now();
const requestBody = JSON.parse(webhookResponse.request.body);
webhookResponse.request.body = JSON.stringify({
...requestBody,
timestamp,
});
const sigHeaders = signatureHeaders(
webhookResponse.request.body,
webhookResponse.sharedSecret,
Date.now()
timestamp
);
webhookResponse.request.headers = {
...webhookResponse.request.headers,
Expand Down

0 comments on commit 9490476

Please sign in to comment.