Permalink
Browse files

Merge git://github.com/mmangino/facebooker2

Conflicts:
	lib/facebooker2.rb
	lib/facebooker2/rails/controller.rb
	lib/facebooker2/rails/helpers/javascript.rb
	spec/helpers/javascript_spec.rb
	spec/rails/controller_spec.rb
  • Loading branch information...
dekart committed Sep 16, 2011
2 parents 89a41fb + 31fac9a commit 18ee331773d7bb0a9e9b098212bcc5255f2c94e8
View
@@ -1,4 +1,5 @@
*.swp
.bundle
.rvmrc
-Gemfile.lock
+Gemfile.lock
+.idea
View
@@ -4,7 +4,7 @@ Facebooker2
Provides rails helpers for interfacing with [Facebook's OpenGraph Javascript
API](http://developers.facebook.com/docs/reference/javascript/).
-Requires the mogli gem.
+Requires the mogli and ruby-hmac gems.
Example
View
@@ -2,19 +2,22 @@
require "mogli"
module Facebooker2
+
+ @oauth2 = true
+ @cookie_prefix = 'fbsr_'
+
class NotConfigured < Exception; end
class << self
- attr_accessor :api_key, :secret, :app_id, :canvas_page_name, :callback_url, :callback_domain
+ attr_accessor :api_key, :secret, :app_id, :canvas_page_name, :callback_url, :callback_domain, :cookie_prefix, :oauth2
end
def self.secret
@secret || raise_unconfigured_exception
end
-
def self.app_id
@app_id || raise_unconfigured_exception
end
@@ -27,19 +30,19 @@ def self.callback_url(protocol = nil)
@callback_domain ? "#{ protocol || 'http://' }#{@callback_domain}" : @callback_url
end
-
def self.raise_unconfigured_exception
raise NotConfigured.new("No configuration provided for Facebooker2. Either set the app_id and secret or call Facebooker2.load_facebooker_yaml in an initializer")
end
-
def self.configuration=(hash)
self.api_key = hash[:api_key]
self.secret = hash[:secret]
self.app_id = hash[:app_id]
self.canvas_page_name = hash[:canvas_page_name]
self.callback_url = hash[:callback_url]
self.callback_domain = hash[:callback_domain]
+ self.cookie_prefix = 'fbsr_' unless hash[:oauth2].blank?
+ self.oauth2 = hash[:oauth2].blank? ? false : true
end
@@ -20,14 +20,22 @@ def self.included(controller)
def current_facebook_user
- fetch_client_and_user
+ if Facebooker2.oauth2
+ oauth2_fetch_client_and_user
+ else
+ fetch_client_and_user
+ end
@_current_facebook_user
end
def current_facebook_client
- fetch_client_and_user
+ if Facebooker2.oauth2
+ oauth2_fetch_client_and_user
+ else
+ fetch_client_and_user
+ end
@_current_facebook_client
end
@@ -41,6 +49,16 @@ def fetch_client_and_user
fetch_client_and_user_from_signed_request
fetch_client_and_user_from_cookie if @_current_facebook_client.nil? and !signed_request_from_logged_out_user?
+ #write the authentication params to a new cookie
+ if !@_current_facebook_client.nil?
+ #we may have generated the signature based on the params in @facebook_params, and the expiration here is different
+
+ set_fb_cookie(@_current_facebook_client.access_token, @_current_facebook_client.expiration, @_current_facebook_user.id, sig)
+ else
+ # if we do not have a client, delete the cookie
+ set_fb_cookie(nil,nil,nil,nil)
+ end
+
@_fb_user_fetched = true
end
end
@@ -99,7 +117,7 @@ def fb_cookie
end
def fb_cookie_name
- "fbs_#{Facebooker2.app_id}"
+ Facebooker2.cookie_prefix + Facebooker2.app_id.to_s
end
# check if the expected signature matches the one from facebook
@@ -297,6 +315,38 @@ def redirect_from_iframe(url_options)
</head></html>
HTML
end
+
+
+ ### Oauth2
+ def oauth2_current_facebook_user
+ oauth2_fetch_client_and_user
+ @_current_facebook_user
+ end
+
+ def oauth2_fetch_client_and_user
+ return if @_fb_user_fetched
+ sig = oauth2_fetch_client_and_user_from_cookie if @_current_facebook_client.nil?
+ @_fb_user_fetched = true
+ end
+
+ def oauth2_fetch_client_and_user_from_cookie
+ return unless fb_cookie?
+ sig,payload = fb_cookie.split('.')
+ return unless fb_signed_request_sig_valid?(sig, payload)
+ data = JSON.parse(base64_url_decode(payload))
+ authenticator = Mogli::Authenticator.new(Facebooker2.app_id, Facebooker2.secret, nil)
+ client = Mogli::Client.create_from_code_and_authenticator(data["code"], authenticator)
+ user = Mogli::User.new(:id=>data["user_id"])
+ fb_sign_in_user_and_client(user, client)
+ end
+
+
+ def base64_url_decode(encoded)
+ chars_to_add = 4-(encoded.size % 4)
+ encoded += ("=" * chars_to_add)
+ Base64.decode64(encoded.tr("-_", "+/"))
+ end
+
end
end
end
@@ -17,10 +17,15 @@ module FacebookConnect
# => <fb:login-button onlogin="window.location.href = &quot;/other_page&quot;;" v="2">Login with Facebook</fb:login-button>
#
def fb_login_and_redirect(url, options = {})
- js = update_page do |page|
- page.redirect_to url
+ # Check if we got the update_page method (pre-Rails 3.1)
+ if respond_to? 'update_page'
+ js = update_page do |page|
+ page.redirect_to url
+ end
+ # Else use plain js
+ else
+ js = "window.location.href = '#{url}'"
end
-
text = options.delete(:text)
#rails 3 only escapes non-html_safe strings, so get the raw string instead of the SafeBuffer
@@ -36,6 +36,7 @@ def fb_connect_js(*args, &block)
:cookie => true,
:status => true,
:xfbml => true,
+ :oauth => Facebooker2.oauth2,
:locale => "en_US"
)
@@ -47,6 +48,7 @@ def fb_connect_js(*args, &block)
status : #{options[:status]}, // check login status
cookie : #{options[:cookie]}, // enable cookies to allow the server to access the session
xfbml : #{options[:xfbml]}, // parse XFBML
+ oauth : #{options[:oauth]}
channelUrl : '#{ options[:channel_url] || 'null' }'
});
JAVASCRIPT
View
@@ -1,5 +1,9 @@
require "spec_helper"
describe Facebooker2 do
+ after :all do
+ Facebooker2.configuration = {:app_id=>1234,:secret=>"secret", :oauth2=>false}
+ Facebooker2.cookie_prefix='fbs_'
+ end
describe "Configuration" do
it "allows setting of the api_key" do
@@ -44,6 +48,13 @@
Facebooker2.load_facebooker_yaml
end.should raise_error(Facebooker2::NotConfigured)
end
+
+ context "with oauth2" do
+ it "specifies the correct cookie prefix" do
+ Facebooker2.configuration= {:oauth2=>true}
+ Facebooker2.cookie_prefix.should == 'fbsr_'
+ end
+ end
end
describe "Casting to facebook_id" do
@@ -1,6 +1,7 @@
require "spec_helper"
describe Facebooker2::Rails::Helpers::Javascript, :type=>:helper do
include Facebooker2::Rails::Helpers
+ include Facebooker2
describe "fb_connect_async_js" do
it "loads with defaults" do
js = fb_connect_async_js '12345'
@@ -12,8 +13,8 @@
appId : '12345',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
-
xfbml : true, // parse XFBML
+ oauth : true,
channelUrl : 'null'
});
@@ -54,6 +55,12 @@
js.include?("//connect.facebook.net/fr_FR/all.js").should be_true, js
end
+ it "supports oauth" do
+ Facebooker2.oauth2=true
+ js = fb_connect_async_js '12345'
+ js.include?("oauth").should be_true, js
+ end
+
# Can't get this to work!
# it "adds extra js" do
# helper.output_buffer = ""
Oops, something went wrong.

0 comments on commit 18ee331

Please sign in to comment.