PHP Parse Signed Request (facebook)


SUBMITTED BY: Guest

DATE: July 9, 2013, 12:10 p.m.

FORMAT: PHP

SIZE: 705 Bytes

HITS: 1365

  1. function parse_signed_request($signed_request, $secret) {
  2. list($encoded_sig, $payload) = explode('.', $signed_request, 2);
  3. // decode the data
  4. $sig = base64_url_decode($encoded_sig);
  5. $data = json_decode(base64_url_decode($payload), true);
  6. if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
  7. error_log('Unknown algorithm. Expected HMAC-SHA256');
  8. return null;
  9. }
  10. // Adding the verification of the signed_request below
  11. $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
  12. if ($sig !== $expected_sig) {
  13. error_log('Bad Signed JSON signature!');
  14. return null;
  15. }
  16. return $data;
  17. }I

comments powered by Disqus