Created
September 2, 2019 15:23
-
-
Save rmartone/b06d5300a1616214b9962e14c10aa07b to your computer and use it in GitHub Desktop.
Validate signature from FBInstant.player.getSignedPlayerInfoAsync()
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 createHmac = require("crypto").createHmac; | |
const APP_SECRET = "<APP_SECRET>"; | |
/** | |
* Validates the signature provided by FBInstant.player.getSignedPlayerInfoAsync() | |
* @param {string} signedPayload returned by getSignature() after | |
* @returns response payload as a JSON object; otherwise, returns undefined. | |
* @see https://developers.facebook.com/docs/games/instant-games/sdk/fbinstant6.1/#signedplayerinfo | |
*/ | |
function validateSignedPlayerInfo(signedPayload) { | |
const data = signedPayload.split("."); | |
// buffer supports base64url | |
const signature = new Buffer(data[0], "base64").toString("hex"); | |
const payload = new Buffer(data[1], "base64").toString("utf8"); | |
return createHmac("sha256", APP_SECRET) | |
.update(data[1]) | |
.digest("hex") === signature | |
? payload | |
: undefined; | |
} | |
FBInstant.player.getSignedPlayerInfoAsync().then(result => { | |
console.log(validateSignedPlayerInfo(result.getSignature())); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment