Created
November 15, 2011 04:52
-
-
Save gaarf/1366188 to your computer and use it in GitHub Desktop.
Methods to deal with FB Signed Requests
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
# see https://github.com/ptarjan/base64url/blob/master/ruby.rb | |
def base64_url_decode(str) | |
str += '=' * (4 - str.length.modulo(4)) | |
Base64.decode64(str.gsub("-", "+").gsub("_", "/")) | |
end | |
# see http://developers.facebook.com/docs/authentication/signed_request/ | |
def valid_facebook_signature?(signature, encoded_data) | |
base64_url_decode(signature) == HMAC::SHA256.digest(FACEBOOK_APP_SECRET, encoded_data) | |
end | |
# see http://developers.facebook.com/docs/authentication/fb_sig/ | |
def valid_legacy_facebook_signature? | |
str = '' | |
fbsig = {} | |
params.each_pair do |key, value| | |
key = key.to_s | |
if key =~ /^fb_sig_/ | |
fbsig[key[7,key.size]] = value | |
end | |
end | |
fbsig.to_a.sort.each do |kv| | |
str << "#{kv[0]}=#{kv[1]}" | |
end | |
str << FACEBOOK_APP_SECRET | |
Digest::MD5.hexdigest(str) == params[:fb_sig] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment