diff --git a/app.js b/app.js index 7f30a56..415114e 100644 --- a/app.js +++ b/app.js @@ -5,6 +5,13 @@ app.use(express.urlencoded({ extended: true })); const port = 3000; +const myArgs = process.argv.slice(2); + +app.all('/validate', function (req, res) { + console.log("-------------- Validate Request --------------"); + res.json( {"confirmation_code":myArgs[0]} ); +}) + app.all('/*', function (req, res) { console.log("-------------- New Request --------------"); console.log("Headers:"+ JSON.stringify(req.headers, null, 3)); diff --git a/appTwitter.js b/appTwitter.js new file mode 100644 index 0000000..4a3666e --- /dev/null +++ b/appTwitter.js @@ -0,0 +1,42 @@ +var express = require('express'); +crypto = require('crypto') + +var app = express(); +app.use(express.json()); +const port = 3000; +const consumer_secret = "Twitter API Key Secret"; + +get_challenge_response = function(crc_token, consumer_secret) { + hmac = crypto.createHmac('sha256', consumer_secret).update(crc_token).digest('base64') + return hmac +} + +app.post('/*', function (req, res) { + console.log("-------------- New Request POST --------------"); + console.log("Headers:"+ JSON.stringify(req.headers, null, 3)); + console.log("Body:"+ JSON.stringify(req.body, null, 3)); + res.json({ message: "Thank you for the message" }); +}) + +// Add support for GET requests to Twitter webhook +app.get("/*", (req, res) => { + // Parse the query param + var crc_token = req.query["crc_token"]; + console.log("-------------- New Request GET --------------"); + console.log("Headers:"+ JSON.stringify(req.headers, null, 3)); + console.log("Body:"+ JSON.stringify(req.body, null, 3)); + // Check if a token is in the query string of the request + if (crc_token) { + var hash = get_challenge_response(crc_token, consumer_secret) + console.log("crc token hash="+ hash); + res.status(200); + res.send({response_token: 'sha256=' + hash}) + } else { + res.status(400); + res.send({ message: "Error: crc_token missing from request."}) + } +}); + +app.listen(port, function () { + console.log(`Example Twitter app listening at ${port}`) +}) \ No newline at end of file diff --git a/package.json b/package.json index c2c41fc..dbc3989 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "start": "node app.js", "startSlack": "node appSlack.js", "startFacebook": "node appFB.js", - "startDropbox": "node appDropbox.js" + "startDropbox": "node appDropbox.js", + "startTwitter": "node appTwitter.js" }, "repository": { "type": "git", diff --git a/testhmac.js b/testhmac.js new file mode 100644 index 0000000..1f22be0 --- /dev/null +++ b/testhmac.js @@ -0,0 +1,16 @@ +const crypto = require('crypto'); + +const verifySignature = function(secret, payload){ + const hash = crypto + .createHmac('sha256', secret) + .update(payload) + .digest('base64'); + return `sha256=${hash}`; + } + +payload={"event_id":"01GJ0FH2Y3H1TYFF8EP9H2RHSG","event_type":"form_response","form_response":{"form_id":"uyifVtg4","token":"a5f3s9fabveqqec7o9l9sa5f3s9fitvt","landed_at":"2022-11-16T15:04:08Z","submitted_at":"2022-11-16T15:04:43Z","definition":{"id":"uyifVtg4","title":"My typeform","fields":[{"id":"HTE1PyMQrlQK","ref":"01GJ0DJYHF6NH3B6NH0YSP1DVF","type":"short_text","title":"Hello, what's your name?","properties":{}},{"id":"7EfljHzxs76Q","ref":"01GJ0DJYHSJ8HMEG2MQ1E819TN","type":"multiple_choice","title":"Nice to meet you, {{field:01GJ0DJYHF6NH3B6NH0YSP1DVF}}, how is your day going?","properties":{},"choices":[{"id":"rbTzd6XVsx8x","label":"Terrific!"},{"id":"tHnbelIABoFL","label":"Not so well..."}]}]},"answers":[{"type":"text","text":"Felippe","field":{"id":"HTE1PyMQrlQK","type":"short_text","ref":"01GJ0DJYHF6NH3B6NH0YSP1DVF"}},{"type":"choice","choice":{"label":"Terrific!"},"field":{"id":"7EfljHzxs76Q","type":"multiple_choice","ref":"01GJ0DJYHSJ8HMEG2MQ1E819TN"}}]}} + +secret = "12345"; +hmac = verifySignature(secret, JSON.stringify(payload)); +console.log(hmac); + \ No newline at end of file