Created
February 27, 2011 10:23
-
-
Save shiftb/846074 to your computer and use it in GitHub Desktop.
This is a helper class that will take a signed request from Facebook's registration system and parse into a json hash.
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
registration_request = FacebookRegistration::SignedRequest.new(params[:signed_request]) | |
registration_params = registration_request.parse_request | |
pp registration_params |
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
require 'rubygems' | |
require 'openssl' | |
require 'base64' | |
module FacebookRegistration | |
class SignedRequest | |
attr_accessor :signed_request | |
def initialize(signed_request) | |
@signed_request = signed_request | |
end | |
def parse_request | |
signature, signed_params = @signed_request.split('.') | |
signed_params = JSON(base64_url_decode(signed_params)) | |
signed_params | |
end | |
private | |
def signed_request_is_valid?(secret, signature, params) | |
signature = base64_url_decode(signature) | |
expected_signature = OpenSSL::HMAC.digest('SHA256', secret, params.tr("-_", "+/")) | |
signature == expected_signature | |
end | |
def base64_url_decode(str) | |
str = str + "=" * (6 - str.size % 6) unless str.size % 6 == 0 | |
Base64.decode64(str.tr("-_", "+/")) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment