forked from paperos-labs/x-hub.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
express.js
186 lines (159 loc) · 4.06 KB
/
express.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
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
"use strict";
/** @type {XHubExpressPackage} */
let XHubExpress = module.exports;
let XHub = require("../x-hub-signature.js");
/**
* @typedef XHubExpressPackage
* @prop {XHubExpressCreate} create
* @prop {String} XHubExpress._mismatchSignature
* @prop {_XHubPipe} _pipe
* @returns {XHubExpress}
*/
/**
* @typedef XHubExpress
* @prop {import('express').Handler} readPayload
* @prop {import('express').Handler} verifyPayload
* @prop {_XHubPipeVerify} _pipeVerify
*/
/**
* @typedef {XHub.XHubOptions & XHubExpressOptionsPart} XHubExpressOptions
*
* @typedef XHubExpressOptionsPart
* @prop {Boolean} [allowUnsignedGet]
* @prop {String} [xhubParam]
*/
XHubExpress._mismatchSignature =
"X-Hub-Signature-256 does not match sha256 hmac of the request body using the shared key";
XHubExpress.create = function (opts) {
let xhub = XHub.create(opts);
let xhubParam = opts.xhubParam;
if (!xhubParam) {
xhubParam = "_xhubSignaturePromise";
}
let routes = {};
// IMPORTANT! This function MUST resolve synchronously so that req
// can be read simultaneously by the body parser in the same tick
/** @type {import('express').Handler} */
routes.readPayload = function (req, res, next) {
if (req.body) {
let err = createError(
"xhub webhook middleware must be 'app.use()'d before any body parser",
);
next(err);
return;
}
let xhubSig256 = req.headers["x-hub-signature-256"];
let xhubSig1 = req.headers["x-hub-signature"];
let xhubSig = xhubSig256 || xhubSig1;
if (!xhubSig) {
//@ts-ignore
req[xhubParam] = Promise.resolve(false);
next();
return;
}
let hasBody =
req.headers["content-length"] ||
"chunked" === req.headers["transfer-encoding"];
if (!hasBody) {
//@ts-ignore
req[xhubParam] = xhub.verify(xhubSig, "");
next();
return;
}
//@ts-ignore
req[xhubParam] = routes._pipeVerify(req, xhubSig).catch(function (e) {
console.error(e);
return false;
});
next();
};
/** @type _XHubPipeVerify */
routes._pipeVerify = async function (req, xhubSig) {
let payload = await XHubExpress._pipe(req);
let equal = await xhub.verify(xhubSig, payload);
return equal;
};
/** @type {import('express').Handler} */
routes.verifyPayload = async function (req, res, next) {
//@ts-ignore
let result = await req[xhubParam];
if (true === result) {
next();
return;
}
if (opts.allowUnsignedGet) {
if ("GET" === req.method) {
let xhubSig256 = req.headers["x-hub-signature-256"];
let xhubSig1 = req.headers["x-hub-signature"];
let xhubSig = xhubSig256 || xhubSig1;
if (!xhubSig) {
next();
return;
}
}
}
let err = createError(XHubExpress._mismatchSignature);
next(err);
};
return routes;
};
/**
* Eventually WebCrypto should be streamable
* https://webcrypto-streams.proposal.wintercg.org/
*
* @param {import('express').Request} req
*/
XHubExpress._pipe = async function (req) {
/** @type {Array<Buffer>} */
let chunks = [];
req.on("readable", function () {
for (;;) {
let chunk = req.read();
if (!chunk) {
break;
}
chunks.push(chunk);
}
});
return new Promise(function (resolve, reject) {
req.on(
"error",
/** @param {Error} e */
function (e) {
reject(e);
},
);
req.on("end", function () {
let data = Buffer.concat(chunks);
let text = data.toString("utf8");
resolve(text);
});
});
};
/**
* @param {String} msg
*/
function createError(msg) {
let err = new Error(msg);
err.message = msg;
Object.assign(err, {
code: "E_XHUB_WEBHOOK",
});
return err;
}
/**
* @callback XHubExpressCreate
* @param {XHubExpressOptions} opts
* @returns {XHubExpress}
*/
/**
* @callback _XHubPipeVerify
* @param {import('express').Request} req
* @param {String} xhubSig
* @returns {Promise<Boolean>}
*/
/**
* @callback _XHubPipe
* @param {import('express').Request} req
* @returns {Promise<String>}
*/