Created
June 1, 2023 19:29
-
-
Save jottenlips/6ed1b49e534e8277f7373d53fe0b7547 to your computer and use it in GitHub Desktop.
JS version of the parse signed request from Facebooks docs on data deletion https://developers.facebook.com/docs/development/create-an-app/app-dashboard/data-deletion-callback/
This file contains hidden or 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
export const parseSignedFacebookRequest = (signed_request) => { | |
const [encoded_sig, payload] = signed_request.split("."); | |
const secret = 'appsecret'; // Use your app secret here | |
// decode the data | |
const sig = base64Decode(encoded_sig); | |
const data = JSON.parse(base64Decode(payload)); | |
// confirm the signature | |
const expected_sig = crypto | |
.createHmac("sha256", secret) | |
.update(payload) | |
.digest("binary"); | |
if (sig !== expected_sig) { | |
throw Error("Bad Signed JSON signature!"); | |
} | |
return data; | |
}; | |
const base64Decode = (input: string) => { | |
return Buffer.from( | |
input.replace(/-/g, "+").replace(/_/g, "/"), | |
"base64" | |
).toString("binary"); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment