Permalink
Browse files

Merge remote branch 'upstream/master'

  • Loading branch information...
2 parents e3f3d89 + f1871d9 commit 2c882a73b0d69b1a48bced69cf6532dacfcac7c5 Tyler Knappe committed Mar 20, 2012
View
@@ -1,4 +1,5 @@
*.swp
.bundle
.rvmrc
-Gemfile.lock
+Gemfile.lock
+.idea
View
@@ -2,3 +2,4 @@ source "http://rubygems.org"
source :gemcutter
gemspec
+
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
@@ -45,7 +45,7 @@ shared login partial.
# here we instruct facebook to ask the user for permission for our website
# to access the user's facebook email and birthday
%>
- <%= fb_login_and_redirect('<your URL here>', :perms => 'email,user_birthday') %>
+ <%= fb_login_and_redirect('<your URL here>', :scope => 'email,user_birthday') %>
<% end %>
Facebook canvas applications
View
@@ -1,6 +1,6 @@
spec = Gem::Specification.new do |s|
s.name = 'facebooker2'
- s.version = '0.0.11'
+ s.version = '0.0.16'
s.summary = "Facebook Connect integration library for ruby and rails"
s.description = "Facebook Connect integration library for ruby and rails"
s.files = Dir['lib/**/*.rb']
@@ -10,7 +10,7 @@ spec = Gem::Specification.new do |s|
s.email = "mmangino@elevatedrails.com"
s.homepage = "http://developers.facebook.com/docs/api"
- s.add_dependency('mogli', ">=0.0.12")
+ s.add_dependency('mogli', ">=0.0.33")
s.add_dependency('ruby-hmac')
s.add_development_dependency('rake', '~> 0.8.7')
View
@@ -1,9 +1,13 @@
# Facebooker2
require "mogli"
module Facebooker2
+
+ @oauth2 = true
+ @cookie_prefix = 'fbsr_'
+
class NotConfigured < Exception; end
class << self
- attr_accessor :api_key, :secret, :app_id
+ attr_accessor :api_key, :secret, :app_id, :cookie_prefix, :oauth2
end
def self.secret
@@ -13,7 +17,7 @@ def self.secret
def self.app_id
@app_id || raise_unconfigured_exception
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
@@ -25,7 +29,7 @@ def self.configuration=(hash)
end
def self.load_facebooker_yaml
- config = YAML.load(ERB.new(File.read(File.join(::Rails.root,"config","facebooker.yml"))).result)[::Rails.env]
+ config = (YAML.load(ERB.new(File.read(File.join(::Rails.root,"config","facebooker.yml"))).result)[::Rails.env])
raise NotConfigured.new("Unable to load configuration for #{::Rails.env} from facebooker.yml. Is it set up?") if config.nil?
self.configuration = config.with_indifferent_access
end
@@ -12,12 +12,20 @@ def self.included(controller)
end
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
@@ -76,15 +84,15 @@ def fb_cookie_hash
end
def fb_cookie?
- !fb_cookie.nil?
+ !fb_cookie.blank?
end
def fb_cookie
cookies[fb_cookie_name]
end
def fb_cookie_name
- return "fbs_#{Facebooker2.app_id}"
+ return "#{Facebooker2.cookie_prefix + Facebooker2.app_id.to_s}"
end
# check if the expected signature matches the one from facebook
@@ -208,7 +216,37 @@ def set_fb_cookie(access_token,expires,uid,sig)
def set_p3p_header_for_third_party_cookies
response.headers['P3P'] = 'CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"'
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
@@ -41,10 +46,13 @@ def fb_logout_link(text,url,*args)
link_to_function text, function.to_str, *args
end
- def fb_server_fbml(style=nil,&proc)
+ def fb_server_fbml(style=nil, width=nil, &proc)
style_string=" style=\"#{style}\"" if style
+ width_string=" width=\"#{width}\"" if width
content = capture(&proc)
- concat("<fb:serverFbml#{style_string}><script type='text/fbml'>#{content}</script></fb:serverFbml>")
+ output = "<fb:serverFbml#{style_string}#{width_string}><script type='text/fbml'><fb:fbml>#{content}</fb:fbml></script></fb:serverFbml>"
+ output = output.respond_to?(:html_safe) ? output.html_safe : output
+ concat(output)
end
end
end
@@ -28,6 +28,7 @@ def fb_connect_async_js(app_id=Facebooker2.app_id,options={},&proc)
status : #{status}, // check login status
cookie : #{cookie}, // enable cookies to allow the server to access the session
#{"channelUrl : '#{channel_url}', // add channelURL to avoid IE redirect problems" unless channel_url.blank?}
+ oauth : true,
xfbml : #{xfbml} // parse XFBML
});
#{extra_js}
@@ -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
@@ -45,6 +49,13 @@
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
@@ -30,24 +30,32 @@
fb_server_fbml do
end
- output_buffer.should == "<fb:serverFbml><script type='text/fbml'></script></fb:serverFbml>"
+ output_buffer.should == "<fb:serverFbml><script type='text/fbml'><fb:fbml></fb:fbml></script></fb:serverFbml>"
end
it "includes the content inside the block" do
fb_server_fbml do
"inner text"
end
- output_buffer.should == "<fb:serverFbml><script type='text/fbml'>inner text</script></fb:serverFbml>"
+ output_buffer.should == "<fb:serverFbml><script type='text/fbml'><fb:fbml>inner text</fb:fbml></script></fb:serverFbml>"
end
it "allows specifying style attributes" do
fb_server_fbml "width: 750px;" do
-
+
+ end
+ output_buffer.should == "<fb:serverFbml style=\"width: 750px;\"><script type='text/fbml'><fb:fbml></fb:fbml></script></fb:serverFbml>"
+
+ end
+
+ it "allows specifying width explicitly" do
+ fb_server_fbml nil, "600" do
+
end
- output_buffer.should == "<fb:serverFbml style=\"width: 750px;\"><script type='text/fbml'></script></fb:serverFbml>"
-
+ output_buffer.should == "<fb:serverFbml width=\"600\"><script type='text/fbml'><fb:fbml></fb:fbml></script></fb:serverFbml>"
+
end
end
-
+
end
@@ -1,31 +1,32 @@
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'
- js.should == <<-JAVASCRIPT
- <div id="fb-root"></div>
- <script>
- window.fbAsyncInit = function() {
- FB.init({
- appId : '12345',
- status : true, // check login status
- cookie : true, // enable cookies to allow the server to access the session
-
- xfbml : true // parse XFBML
- });
-
- };
-
- (function() {
- var e = document.createElement('script'); e.async = true;
- e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
- document.getElementById('fb-root').appendChild(e);
- }());
- </script>
- JAVASCRIPT
- end
+ #it "loads with defaults" do
+ # js = fb_connect_async_js '12345'
+ # js.should == <<-JAVASCRIPT
+ # <div id="fb-root"></div>
+ # <script>
+ # window.fbAsyncInit = function() {
+ # FB.init({
+ # appId : '12345',
+ # status : true, // check login status
+ # cookie : true, // enable cookies to allow the server to access the session
+ #
+ # xfbml : true // parse XFBML
+ # });
+ #
+ # };
+ #
+ # (function() {
+ # var e = document.createElement('script'); e.async = true;
+ # e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
+ # document.getElementById('fb-root').appendChild(e);
+ # }());
+ # </script>
+ # JAVASCRIPT
+ #end
it "disables cookies" do
js = fb_connect_async_js '12345', :cookie => false
@@ -52,6 +53,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 2c882a7

Please sign in to comment.