From 21bfe3da7c348c359f9ef551aae76fea2f298f16 Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Fri, 11 Dec 2020 13:43:51 -0600 Subject: [PATCH] middleware model --- src/client/InteractionClient.js | 41 +++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/src/client/InteractionClient.js b/src/client/InteractionClient.js index ffb1faec..c87790f4 100644 --- a/src/client/InteractionClient.js +++ b/src/client/InteractionClient.js @@ -135,20 +135,37 @@ class InteractionClient extends BaseClient { } } - async handleFromHTTP(body, signature, timestamp) { - if (sodium === undefined) { - sodium = require('../util/Sodium'); - } - if (!sodium.methods.verify(Buffer.from(signature, 'hex'), Buffer.from(timestamp + body), this.publicKey)) { - return { status: 400, body: '' }; - } - const data = JSON.parse(body); + middleware() { + return async (req, res, next) => { + const timestamp = req.get('x-signature-timestamp'); + const signature = req.get('x-signature-ed25519'); - const result = await this.handle(data); + const chunks = []; + for await (const chunk of req) { + chunks.push(chunk); + } + const body = Buffer.concat(chunks); - return { - status: 200, - body: JSON.stringify(result), + if (sodium === undefined) { + sodium = require('../util/Sodium'); + } + if ( + !sodium.methods.verify( + Buffer.from(signature, 'hex'), + Buffer.concat([Buffer.from(timestamp), body]), + this.publicKey, + ) + ) { + res.status(403).end(); + return; + } + + const data = JSON.parse(body.toString()); + + const result = await this.handle(data); + res.status(200).end(JSON.stringify(result)); + + next(); }; }