Skip to content

Instantly share code, notes, and snippets.

@jottenlips
Created June 1, 2023 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jottenlips/6ed1b49e534e8277f7373d53fe0b7547 to your computer and use it in GitHub Desktop.
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/
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