-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.js
160 lines (133 loc) · 4.7 KB
/
webhook.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* @fileoverview This file contains a wrapper for a TidyHQ Webhook.
* @author Sean McGinty <[email protected]>
* @version 1.0.0
* @license GPL-3.0
*/
const axios = require("axios");
const crypto = require("crypto");
/**
* @description This class is used to listen and act on Webhooks.
* @class
*/
class TidyHQWebhook {
/**
* @type {Record<string, Function>}
*/
callbacks = {};
/**
* @description Create a new instance of the TidyHQWebhook class.
* @param {string} webhookId - The ID of the webhook.
* @param {string} signingKey - The signing key for the webhook.
* @constructor
*/
constructor(webhookId, signingKey) {
this.signingKey = signingKey;
this.webhookId = webhookId;
}
/**
* @param {string} event
* @param {Function} callback
*/
registerCallback(event, callback) {
this.callbacks[event] = callback;
}
/**
* @param {string} event
* @param {object} data
*/
handleEvent(event, data) {
if (this.callbacks[event]) {
this.callbacks[event](data);
} else {
console.log("No callback registered for event: " + event);
console.log(data);
}
}
/**
* @description Verify a message from TidyHQ.
* @param {string} tidySignatureHeader - The signature header from TidyHQ.
* @param {string} body - The raw body of the message.
* @param {string} httpMethod - The HTTP method of the webhook.
* @returns {void | string} - If an error occurs, the error message is returned.
*/
verifyAndHandle(tidySignatureHeader, body, httpMethod = 'POST') {
this.verify(tidySignatureHeader, body, httpMethod).then((data) => {
this.handleEvent(data.kind, data.data);
}).catch((error) => {
return error;
});
}
/**
* @description Verify a message from TidyHQ.
* @param {string} tidySignatureHeader - The signature header from TidyHQ.
* @param {string} body - The raw body of the message.
* @param {string} httpMethod - The HTTP method of the webhook.
* @returns {Promise<Tidy_V2_WebhookMessage>} - The message from the webhook.
*/
async verify(tidySignatureHeader, body, httpMethod = 'POST') {
const signingKey = Buffer.from(this.signingKey, 'base64')
const details = this.parseHeader(tidySignatureHeader, 'v1')
const tolerance = 300
if (!details || details.timestamp === -1) {
throw new Error('Unable to extract timestamp and signatures from header')
}
if (!details.signatures.length) {
throw new Error('No signatures found with expected scheme')
}
const timestamp = details.timestamp
const signature = details.signatures[0]
const timestampedPayload = `${timestamp}.${body}`
const expectedSignature = crypto.createHmac('sha256', signingKey)
.update(timestampedPayload, 'utf8')
.digest('hex')
if (signature !== expectedSignature) {
throw new Error('Signature mismatch')
}
const timestampAge = Math.floor(Date.now() / 1000) - timestamp
if (tolerance > 0 && timestampAge > tolerance) {
throw new Error('Timestamp outside the tolerance zone')
}
const data = JSON.parse(body);
if (data.webhook_id !== this.webhookId) {
throw new Error(`There has been a webhook ID mismatch, expected ${this.webhookId} got ${data.webhook_id}`)
}
if (data.http_method !== httpMethod) {
throw new Error(`There has been a HTTP method mismatch, expected ${httpMethod} got ${data.http_method}`)
}
return data
}
/**
*
* @param {string | null | undefined} header
* @param {string} scheme
* @returns {{ timestamp: number, signatures: string[] } | null}
*/
parseHeader(header, scheme) {
if (typeof header !== 'string') {
return null
}
return header.split(',').reduce(
/**
* @param {{ timestamp: number, signatures: string[] }} accum
* @param {string} item
* @returns {{ timestamp: number, signatures: string[] }}
*/
(accum, item) => {
const kv = item.split('=')
if (kv[0] === 't') {
accum.timestamp = parseInt(kv[1], 10)
}
if (kv[0] === scheme) {
accum.signatures.push(kv[1])
}
return accum
},
{
timestamp: -1,
signatures: [],
}
)
}
}
module.exports = TidyHQWebhook;