Created
May 18, 2018 04:52
-
-
Save jkras/c4a718d81f1165771f5c5b3a9d317fc9 to your computer and use it in GitHub Desktop.
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
exports = function(payload) { | |
var queryArg = payload.query.arg || ''; | |
var body = {}; | |
if (payload.body) { | |
body = JSON.parse(payload.body.text()); | |
} | |
// Get x-signature header and create digest for comparison | |
const signature = payload.headers['X-Signature']; | |
const digest = utils.crypto.hmac(payload.body.text(), | |
context.values.get("segment_shared_secret"), "sha1", "hex"); | |
//Only write the data if the digest matches Segment's x-signature! | |
if (digest == signature) { | |
let mongodb = context.services.get("mongodb-atlas"); | |
// Set the collection up to write data | |
let events = mongodb.db("segment").collection("events"); | |
// Write the data | |
events.insertOne(body); | |
} else { | |
console.log("Digest didn't match"); | |
} | |
return queryArg + ' ' + body.msg; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment