Last active
June 2, 2021 04:10
-
-
Save wictorwilen/8ac6f9c167d70e4774e20f3c39a47a55 to your computer and use it in GitHub Desktop.
Microsoft Teams Outgoing Webhook Proxy for Microsoft Flow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const crypto = require('crypto'); | |
const request = require('request'); | |
const bufSecret = Buffer('OUTGOING-WEBHOOK-SECRET', "base64"); | |
const flowWebhook = 'MICROSOFT-FLOW-URL'; | |
module.exports = function (context, req) { | |
var auth = req.headers['authorization']; | |
// HMAC security validation | |
var msgBuf = Buffer.from(req.rawBody, 'utf8'); | |
var msgHash = "HMAC " + crypto.createHmac('sha256', bufSecret).update(msgBuf).digest("base64"); | |
if (msgHash == auth) { | |
request.post(flowWebhook, { | |
body: req.rawBody, | |
headers: { 'content-type': 'application/json' }, | |
}, function (error, response, body) { | |
context.res = { | |
headers: { 'content-type': 'application/json' }, | |
body: JSON.stringify({ | |
"type": "message", | |
"text": "Thank you!" | |
}) | |
}; | |
context.done(); | |
}); | |
} else { | |
context.res = { | |
status: 200, | |
headers: { 'content-type': 'application/json' }, | |
body: JSON.stringify({ | |
"type": "message", | |
"text": "Error: message sender cannot be authenticated." | |
}) | |
} | |
context.done(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment