Skip to content

Instantly share code, notes, and snippets.

@wictorwilen
Last active June 2, 2021 04:10
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save wictorwilen/8ac6f9c167d70e4774e20f3c39a47a55 to your computer and use it in GitHub Desktop.
Save wictorwilen/8ac6f9c167d70e4774e20f3c39a47a55 to your computer and use it in GitHub Desktop.
Microsoft Teams Outgoing Webhook Proxy for Microsoft Flow
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