Skip to content

Instantly share code, notes, and snippets.

@bgmort
Forked from cyakimov/gist:1139981
Last active June 2, 2020 03:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgmort/d2b89943768358b3da72a13a517708f1 to your computer and use it in GitHub Desktop.
Save bgmort/d2b89943768358b3da72a13a517708f1 to your computer and use it in GitHub Desktop.
Decode Facebook signed_request with NodeJS
// Fixed an error with character replacement, removed a dependency, included imports, throw errors, and fixed global variables
var crypto = require('crypto');
//remove a dependency on b64url
function atob(str) {
return new Buffer(str, 'base64').toString('binary');
}
//this is not used here, but to leave it out would be like passing the salt without the pepper.
function btoa(str) {
return new Buffer(str, 'utf8').toString('base64');
}
function parseSignedRequest(signedRequest, secret) {
var encodedData = signedRequest.split('.');
// decode the data
var sig = encodedData[0];
var json = atob(encodedData[1]);
var data = JSON.parse(json); // ERROR Occurs Here!
// check algorithm - not relevant to error
if (!data.algorithm || data.algorithm.toUpperCase() != 'HMAC-SHA256') {
throw Error('Unknown algorithm: ' + data.algorithm + '. Expected HMAC-SHA256');
}
// check sig - not relevant to error
var expectedSig = crypto.createHmac('sha256', secret).update(encodedData[1]).digest('base64').replace(/\+/g,'-').replace(/\//g,'_').replace('=','');
if (sig !== expectedSig) {
throw Error('Invalid signature: ' + sig + '. Expected ' + expectedSig);
}
return data;
}
module.exports = parseSignedRequest;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment