Module: Facebooker2::Rails::Controller
- Defined in:
- lib/facebooker2/rails/controller.rb
Class Method Summary collapse
Instance Method Summary collapse
- #current_facebook_client ⇒ Object
- #current_facebook_user ⇒ Object
- #facebook_params ⇒ Object
- #fb_cookie_for_app_id(app_id) ⇒ Object
- #fb_cookie_for_app_id?(app_id) ⇒ Boolean
- #fb_cookie_hash_for_app_id(app_id) ⇒ Object
- #fb_cookie_signature_correct?(hash, secret) ⇒ Boolean
- #fb_create_user_and_client(token, expires, userid) ⇒ Object
- #fb_load_facebook_params ⇒ Object
- #fb_sign_in_user_and_client(user, client) ⇒ Object
- #fb_signed_request_json(encoded) ⇒ Object
- #fb_signed_request_sig_valid?(sig, encoded) ⇒ Boolean
- #fetch_client_and_user ⇒ Object
- #fetch_client_and_user_from_cookie ⇒ Object
- #fetch_client_and_user_from_signed_request ⇒ Object
Class Method Details
.included(controller) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/facebooker2/rails/controller.rb', line 8 def self.included(controller) controller.helper Facebooker2::Rails::Helpers controller.helper_method :current_facebook_user controller.helper_method :current_facebook_client controller.helper_method :facebook_params end |
Instance Method Details
#current_facebook_client ⇒ Object
20 21 22 23 |
# File 'lib/facebooker2/rails/controller.rb', line 20 def current_facebook_client fetch_client_and_user @_current_facebook_client end |
#current_facebook_user ⇒ Object
15 16 17 18 |
# File 'lib/facebooker2/rails/controller.rb', line 15 def current_facebook_user fetch_client_and_user @_current_facebook_user end |
#facebook_params ⇒ Object
88 89 90 |
# File 'lib/facebooker2/rails/controller.rb', line 88 def facebook_params @facebook_param ||= fb_load_facebook_params end |
#fb_cookie_for_app_id(app_id) ⇒ Object
68 69 70 |
# File 'lib/facebooker2/rails/controller.rb', line 68 def (app_id) ["fbs_#{app_id}"] end |
#fb_cookie_for_app_id?(app_id) ⇒ Boolean
64 65 66 |
# File 'lib/facebooker2/rails/controller.rb', line 64 def (app_id) !(app_id).nil? end |
#fb_cookie_hash_for_app_id(app_id) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/facebooker2/rails/controller.rb', line 53 def (app_id) return nil unless (app_id) hash={} data = (app_id).gsub(/"/,"") data.split("&").each do |str| parts = str.split("=") hash[parts.first] = parts.last end hash end |
#fb_cookie_signature_correct?(hash, secret) ⇒ Boolean
72 73 74 75 76 77 78 79 80 |
# File 'lib/facebooker2/rails/controller.rb', line 72 def (hash,secret) sorted_keys = hash.keys.reject {|k| k=="sig"}.sort test_string = "" sorted_keys.each do |key| test_string += "#{key}=#{hash[key]}" end test_string += secret Digest::MD5.hexdigest(test_string) == hash["sig"] end |
#fb_create_user_and_client(token, expires, userid) ⇒ Object
40 41 42 43 44 |
# File 'lib/facebooker2/rails/controller.rb', line 40 def fb_create_user_and_client(token,expires,userid) client = Mogli::Client.new(token,expires.to_i) user = Mogli::User.new(:id=>userid) fb_sign_in_user_and_client(user,client) end |
#fb_load_facebook_params ⇒ Object
92 93 94 95 96 97 |
# File 'lib/facebooker2/rails/controller.rb', line 92 def fb_load_facebook_params return {} if params[:signed_request].blank? sig,encoded_json = params[:signed_request].split(".") return {} unless fb_signed_request_sig_valid?(sig,encoded_json) ActiveSupport::JSON.decode(fb_signed_request_json(encoded_json)).with_indifferent_access end |
#fb_sign_in_user_and_client(user, client) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/facebooker2/rails/controller.rb', line 46 def fb_sign_in_user_and_client(user,client) user.client = client @_current_facebook_user = user @_current_facebook_client = client @_fb_user_fetched = true end |
#fb_signed_request_json(encoded) ⇒ Object
82 83 84 85 86 |
# File 'lib/facebooker2/rails/controller.rb', line 82 def fb_signed_request_json(encoded) chars_to_add = 4-(encoded.size % 4) encoded += ("=" * chars_to_add) Base64.decode64(encoded) end |
#fb_signed_request_sig_valid?(sig, encoded) ⇒ Boolean
99 100 101 102 103 104 |
# File 'lib/facebooker2/rails/controller.rb', line 99 def fb_signed_request_sig_valid?(sig,encoded) base64 = Base64.encode64(HMAC::SHA256.digest(Facebooker2.secret,encoded)) #now make the url changes that facebook makes url_escaped_base64 = base64.gsub(/=*\n?$/,"").tr("+/","-_") sig == url_escaped_base64 end |
#fetch_client_and_user ⇒ Object
25 26 27 28 29 30 |
# File 'lib/facebooker2/rails/controller.rb', line 25 def fetch_client_and_user return if @_fb_user_fetched fetch_client_and_user_from_signed_request unless @_current_facebook_client @_fb_user_fetched = true end |
#fetch_client_and_user_from_cookie ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/facebooker2/rails/controller.rb', line 32 def app_id = Facebooker2.app_id if (hash_data = (app_id)) and ((app_id),Facebooker2.secret) fb_create_user_and_client(hash_data["access_token"],hash_data["expires"],hash_data["uid"]) end end |
#fetch_client_and_user_from_signed_request ⇒ Object
106 107 108 109 110 |
# File 'lib/facebooker2/rails/controller.rb', line 106 def fetch_client_and_user_from_signed_request if facebook_params[:oauth_token] fb_create_user_and_client(facebook_params[:oauth_token],facebook_params[:expires],facebook_params[:user_id]) end end |