facebooker2 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
data/lib/facebooker2.rb CHANGED
@@ -1,9 +1,13 @@
1
1
  # Facebooker2
2
2
  require "mogli"
3
3
  module Facebooker2
4
+
5
+ @oauth2 = false
6
+ @cookie_prefix = 'fbs_'
7
+
4
8
  class NotConfigured < Exception; end
5
9
  class << self
6
- attr_accessor :api_key, :secret, :app_id
10
+ attr_accessor :api_key, :secret, :app_id, :cookie_prefix, :oauth2
7
11
  end
8
12
 
9
13
  def self.secret
@@ -13,7 +17,7 @@ module Facebooker2
13
17
  def self.app_id
14
18
  @app_id || raise_unconfigured_exception
15
19
  end
16
-
20
+
17
21
  def self.raise_unconfigured_exception
18
22
  raise NotConfigured.new("No configuration provided for Facebooker2. Either set the app_id and secret or call Facebooker2.load_facebooker_yaml in an initializer")
19
23
  end
@@ -22,10 +26,12 @@ module Facebooker2
22
26
  self.api_key = hash[:api_key]
23
27
  self.secret = hash[:secret]
24
28
  self.app_id = hash[:app_id]
29
+ self.cookie_prefix = 'fbsr_' unless hash[:oauth2].blank?
30
+ self.oauth2 = hash[:oauth2].blank? ? false : true
25
31
  end
26
32
 
27
33
  def self.load_facebooker_yaml
28
- config = YAML.load(ERB.new(File.read(File.join(::Rails.root,"config","facebooker.yml"))).result)[::Rails.env]
34
+ config = (YAML.load(ERB.new(File.read(File.join(::Rails.root,"config","facebooker.yml"))).result)[::Rails.env])
29
35
  raise NotConfigured.new("Unable to load configuration for #{::Rails.env} from facebooker.yml. Is it set up?") if config.nil?
30
36
  self.configuration = config.with_indifferent_access
31
37
  end
@@ -12,12 +12,20 @@ module Facebooker2
12
12
  end
13
13
 
14
14
  def current_facebook_user
15
- fetch_client_and_user
15
+ if (Facebooker2.oauth2)
16
+ oauth2_fetch_client_and_user
17
+ else
18
+ fetch_client_and_user
19
+ end
16
20
  @_current_facebook_user
17
21
  end
18
22
 
19
23
  def current_facebook_client
20
- fetch_client_and_user
24
+ if (Facebooker2.oauth2)
25
+ oauth2_fetch_client_and_user
26
+ else
27
+ fetch_client_and_user
28
+ end
21
29
  @_current_facebook_client
22
30
  end
23
31
 
@@ -84,7 +92,7 @@ module Facebooker2
84
92
  end
85
93
 
86
94
  def fb_cookie_name
87
- return "fbs_#{Facebooker2.app_id}"
95
+ return "#{Facebooker2.cookie_prefix + Facebooker2.app_id.to_s}"
88
96
  end
89
97
 
90
98
  # check if the expected signature matches the one from facebook
@@ -201,7 +209,37 @@ module Facebooker2
201
209
  def set_p3p_header_for_third_party_cookies
202
210
  response.headers['P3P'] = 'CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"'
203
211
  end
204
-
212
+
213
+ ### Oauth2
214
+ def oauth2_current_facebook_user
215
+ oauth2_fetch_client_and_user
216
+ @_current_facebook_user
217
+ end
218
+
219
+ def oauth2_fetch_client_and_user
220
+ return if @_fb_user_fetched
221
+ sig = oauth2_fetch_client_and_user_from_cookie if @_current_facebook_client.nil?
222
+ @_fb_user_fetched = true
223
+ end
224
+
225
+ def oauth2_fetch_client_and_user_from_cookie
226
+ return unless fb_cookie?
227
+ sig,payload = fb_cookie.split('.')
228
+ return unless fb_signed_request_sig_valid?(sig, payload)
229
+ data = JSON.parse(base64_url_decode(payload))
230
+ authenticator = Mogli::Authenticator.new(Facebooker2.app_id, Facebooker2.secret, nil)
231
+ client = Mogli::Client.create_from_code_and_authenticator(data["code"], authenticator)
232
+ user = Mogli::User.new(:id=>data["user_id"])
233
+ fb_sign_in_user_and_client(user, client)
234
+ end
235
+
236
+
237
+ def base64_url_decode(encoded)
238
+ chars_to_add = 4-(encoded.size % 4)
239
+ encoded += ("=" * chars_to_add)
240
+ Base64.decode64(encoded.tr("-_", "+/"))
241
+ end
242
+
205
243
  end
206
244
  end
207
245
  end
@@ -12,13 +12,16 @@ module Facebooker2
12
12
  end
13
13
 
14
14
  def fb_connect_async_js(app_id=Facebooker2.app_id,options={},&proc)
15
- opts = Hash.new(true).merge!(options)
16
- cookie = opts[:cookie]
17
- status = opts[:status]
18
- xfbml = opts[:xfbml]
15
+ opts = Hash.new.merge!(options)
16
+ cookie = opts[:cookie].nil? ? true : opts[:cookie]
17
+ status = opts[:status].nil? ? true : opts[:status]
18
+ xfbml = opts[:xfbml].nil? ? true : opts[:xfbml]
19
+ oauth2 = Facebooker2.oauth2 ? true : false
19
20
  channel_url = opts[:channel_url]
21
+ lang = opts[:locale] || 'en_US'
20
22
  extra_js = capture(&proc) if block_given?
21
23
  js = <<-JAVASCRIPT
24
+ <div id="fb-root"></div>
22
25
  <script>
23
26
  window.fbAsyncInit = function() {
24
27
  FB.init({
@@ -26,19 +29,16 @@ module Facebooker2
26
29
  status : #{status}, // check login status
27
30
  cookie : #{cookie}, // enable cookies to allow the server to access the session
28
31
  #{"channelUrl : '#{channel_url}', // add channelURL to avoid IE redirect problems" unless channel_url.blank?}
32
+ #{"oauth : true," if oauth2}
29
33
  xfbml : #{xfbml} // parse XFBML
30
34
  });
31
35
  #{extra_js}
32
36
  };
33
37
 
34
38
  (function() {
35
- var s = document.createElement('div');
36
- s.setAttribute('id','fb-root');
37
- document.documentElement.getElementsByTagName("body")[0].appendChild(s);
38
- var e = document.createElement('script');
39
- e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
40
- e.async = true;
41
- s.appendChild(e);
39
+ var e = document.createElement('script'); e.async = true;
40
+ e.src = document.location.protocol + '//connect.facebook.net/#{lang}/all.js';
41
+ document.getElementById('fb-root').appendChild(e);
42
42
  }());
43
43
  </script>
44
44
  JAVASCRIPT
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: facebooker2
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 11
10
- version: 0.0.11
9
+ - 12
10
+ version: 0.0.12
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mike Mangino
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-01 00:00:00 -04:00
18
+ date: 2011-08-31 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency