Skip to content

Instantly share code, notes, and snippets.

@herval
Created May 2, 2011 01:03
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save herval/951054 to your computer and use it in GitHub Desktop.
Save herval/951054 to your computer and use it in GitHub Desktop.
Parsing Facebook's signed_request (in ruby)
def parse_data
# This is a typical set of parameters passed by Facebook
# Parameters: {"signed_request"=>"vsSe9NNeyqom0hAtGyb2L9scc3-aNbY5Xb25EW55LpE.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImV4cGlyZXMiOjEzMDA3NzAwMDAsImlzc3VlZF9hdCI6MTMwMDc2NDg2Niwib2F1dGhfdG9rZW4iOiIxNzE2MDQwOTI4NjgwNTd8Mi4xQnBWNm5mU2VXRm5RT0lOdzltNWFRX18uMzYwMC4xMzAwNzcwMDAwLTE1MjAwMzkxfEFpNXctc2t4WlJyVUd1ZzZvOU95aDZBQmdSZyIsInVzZXIiOnsiY291bnRyeSI6InVzIiwibG9jYWxlIjoiZW5fVVMiLCJhZ2UiOnsibWluIjoyMX19LCJ1c2VyX2lkIjoiMTUyMDAzOTEifQ"}
# If we have the signed_request parameters, stash them away
session[:signed_request] = params[:signed_request] if params[:signed_request]
encoded_user_data = session[:signed_request]
return if encoded_user_data.blank?
# We only care about the data after the '.'
payload = encoded_user_data.split(".")[1]
# Facebook gives us a base64URL encoded string. Ruby only supports base64 out of the box, so we have to add padding to make it work
payload += '=' * (4 - payload.length.modulo(4))
decoded_json = Base64.decode64(payload)
@signed_data = JSON.parse(decoded_json)
# This is what your parsed JSON should look like
# @signed_data => {"expires"=>1300770000, "algorithm"=>"HMAC-SHA256", "user_id"=>"15200391", "oauth_token"=>"171604092868057|2.1BpV6nfSeWFnQOINw9m5aQ__.3600.1300770000-15200391|Ai5w-skxZRrUGug6o9Oyh6ABgRg", "user"=>{"country"=>"us", "locale"=>"en_US", "age"=>{"min"=>21}}, "issued_at"=>1300764866}
#The existance of an oauth token means the user has given permission to the app.
@oauth_token = @signed_data["oauth_token"]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment