-
-
Save stewones/4885b2e5ed8cdcbf4c35 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
//npm install b64url | |
//A signed_request for testing: | |
//WGvK-mUKB_Utg0l8gSPvf6smzacp46977pTtcRx0puE.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImV4cGlyZXMiOjEyOTI4MjEyMDAsImlzc3VlZF9hdCI6MTI5MjgxNDgyMCwib2F1dGhfdG9rZW4iOiIxNTI1NDk2ODQ3NzczMDJ8Mi5ZV2NxV2k2T0k0U0h4Y2JwTWJRaDdBX18uMzYwMC4xMjkyODIxMjAwLTcyMTU5OTQ3NnxQaDRmb2t6S1IyamozQWlxVldqNXp2cTBmeFEiLCJ1c2VyIjp7ImxvY2FsZSI6ImVuX0dCIiwiY291bnRyeSI6ImF1In0sInVzZXJfaWQiOiI3MjE1OTk0NzYifQ | |
function parse_signed_request(signed_request, secret) { | |
encoded_data = signed_request.split('.',2); | |
// decode the data | |
sig = encoded_data[0]; | |
json = base64url.decode(encoded_data[1]); | |
data = JSON.parse(json); // ERROR Occurs Here! | |
// check algorithm - not relevant to error | |
if (!data.algorithm || data.algorithm.toUpperCase() != 'HMAC-SHA256') { | |
console.error('Unknown algorithm. Expected HMAC-SHA256'); | |
return null; | |
} | |
// check sig - not relevant to error | |
expected_sig = crypto.createHmac('sha256',secret).update(encoded_data[1]).digest('base64').replace(/\+/g,'-').replace(/\//g,'_').replace('=',''); | |
if (sig !== expected_sig) { | |
console.error('Bad signed JSON Signature!'); | |
return null; | |
} | |
return data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment