-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhooks-api.js
More file actions
205 lines (164 loc) · 5.77 KB
/
webhooks-api.js
File metadata and controls
205 lines (164 loc) · 5.77 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const qs = require('qs');
const crypto = require('crypto');
const exec = require('child_process').exec;
const n = require('nonce')();
const jwt = require('jsonwebtoken');
module.exports = function (app_api_key, access_key, signing_key, options) {
if (!app_api_key || !access_key || !signing_key) {
return false;
} else {
return new AuthyWebhooks(app_api_key, access_key, signing_key, options);
}
};
function AuthyWebhooks(app_api_key, access_key, signing_key, options) {
this.DEBUG = options.DEBUG || false;
this.PROD = options.PROD || true;
this.api_url = options.API_URL;
this.app_api_key = app_api_key;
this.access_key = access_key;
this.signing_key = signing_key;
this.nonce = options.PROD ? n() + "." + n() : 123;
this.computed_sig;
}
/**
* Sort by property only.
* Normal JS sort parses the entire string so a stringified array value like 'events=zzzz'
* would be moved after 'events=aaaa'.
*
* Instead we split tokenize the string around the '=' value and only sort alphabetically
* by the property.
*
* @param {string} x
* @param {string} y
* @returns {number}
*/
function sortByPropertyOnly(x, y) {
var xx = x.split("=");
var yy = y.split("=");
if (xx < yy) {
return -1;
}
if (xx > yy) {
return 1;
}
return 0;
}
AuthyWebhooks.prototype = {
/**
* Call the CURL command
* @param curl
*/
callCurl: function (curl) {
if (this.DEBUG) {
console.log("cURL call:\n", curl);
}
console.log("CURL COMMAND:");
console.log(curl);
exec(curl, (error, stdout, stderr) => {
console.log(`stdout: ${stdout}\n`);
console.log(`stderr: ${stderr}`);
if (error !== null) {
console.log(`exec error: ${error}`);
}
});
},
/**
* Compute the HMAC sig based upon the API keys, payload params, method, url and a nonce.
*
* @param {!string} url
* @param {!string} method POST || GET
* @param {Object=} params Any additional params.
*/
computeSig: function (url, method, params) {
var allParams = {
app_api_key: this.app_api_key,
access_key: this.access_key
};
// add any additional params
for (var key in params) {
allParams[key] = params[key];
}
if (this.DEBUG) console.log("Params:\n", allParams);
var sorted_params = qs.stringify(allParams, {arrayFormat: 'brackets'}).split("&").sort(sortByPropertyOnly).join("&").replace(/%20/g, '+');
if (this.DEBUG) console.log("Sorted: \n", sorted_params);
var dataToSign = this.nonce + "|" + method + "|" + url + "|" + sorted_params;
this.computed_sig = crypto.createHmac('sha256', this.signing_key).update(dataToSign).digest('base64');
if (this.DEBUG) {
console.log("Nonce:\n", this.nonce);
console.log("Signature:\n", this.computed_sig);
}
},
deleteWebhook: function (id) {
if (!id) {
console.log('webhook id required');
return false;
}
var url = this.api_url + "/dashboard/json/application/webhooks/" + id;
var method = "DELETE";
// extra params for sig computation
var params = {
};
this.computeSig(url, method, params);
var curl = 'curl -X DELETE ' + url
+ ' -d app_api_key=' + this.app_api_key
+ ' -d access_key=' + this.access_key
+ ' -H "X-Authy-Signature-Nonce:' + this.nonce + '"'
+ ' -H "X-Authy-Signature: ' + this.computed_sig + '"';
this.callCurl(curl);
},
listWebhooks: function () {
var url = this.api_url + "/dashboard/json/application/webhooks";
var method = "GET";
this.computeSig(url, method);
var curl = 'curl -X GET ' + url
+ ' -d app_api_key=' + this.app_api_key
+ ' -d access_key=' + this.access_key
+ ' -H "X-Authy-Signature-Nonce:' + this.nonce + '"'
+ ' -H "X-Authy-Signature: ' + this.computed_sig + '"';
this.callCurl(curl);
},
/**
* {Array.<strings>} events
*/
createWebhooks: function (events, callback_url, name) {
var url = this.api_url + "/dashboard/json/application/webhooks";
var method = "POST";
if (events.length < 1 || !callback_url || !name) {
console.log('no events or no callback or webhook name');
return false;
}
var webhook_events = '';
for (var i = 0; i < events.length; i++) {
webhook_events += ' -d events[]="' + events[i] + '" '
}
// extra params for sig computation
var params = {
app_api_key: this.app_api_key,
access_key: this.access_key,
url: callback_url,
events: events,
name: name
};
this.computeSig(url, method, params);
var curl = 'curl -X POST ' + url
+ ' -d url="' + callback_url + '"'
+ ' -d name="' + name + '"'
+ webhook_events
+ ' -d app_api_key="' + this.app_api_key + '"'
+ ' -d access_key="' + this.access_key + '"'
+ ' -H "X-Authy-Signature-Nonce:' + this.nonce + '"'
+ ' -H "X-Authy-Signature: ' + this.computed_sig + '"';
this.callCurl(curl);
},
verifyJWTResponse: function (message, secret) {
try {
var decoded = jwt.verify(message, secret, {algorithm: ["HS256"]});
console.log('Signature verified and decoded');
return decoded;
} catch (err) {
console.log('Invalid signature. Cannot verify JWT');
console.log(err);
return false;
}
}
};