fgraph 0.6.2 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History CHANGED
@@ -1,3 +1,7 @@
1
+ v0.7.0
2
+ * Set FGraph.oauth_access_token to pass empty redirect_uri param by default
3
+ * Update FGraph Rails helper to support FB OAuth 2.0
4
+
1
5
  v0.6.2
2
6
  * Change RAILS_ROOT to Rails.root to avoid deprecated message in Rails 3
3
7
 
@@ -177,7 +177,7 @@ Sample codes:
177
177
  </script>
178
178
 
179
179
  <!-- Facebook Login Button -->
180
- <fb:login-button autologoutlink="true" perms="email,publish_stream"></fb:login-button>
180
+ <fb:login-button autologoutlink="true" scope="email,publish_stream"></fb:login-button>
181
181
 
182
182
  <% if fgraph_logged_in? %>
183
183
  <br>Hello <%= fgraph_user['name'] %>,
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
3
  :build:
4
- :minor: 6
5
- :patch: 2
4
+ :minor: 7
5
+ :patch: 0
@@ -191,7 +191,7 @@ module FGraph
191
191
  # # redirect_uri=http://www.example.com/oauth_redirect&
192
192
  # # code=...
193
193
  # FGraph.oauth_access_token('[client id]', '[client secret]',
194
- # :redirect_uri => ''http://www.example.com/oauth_redirect',
194
+ # :redirect_uri => 'http://www.example.com/oauth_redirect',
195
195
  # :code => '[authorization code]')
196
196
  #
197
197
  # Application access token requires <tt>:type => 'client_cred'</td> option. Used to access application
@@ -206,7 +206,8 @@ module FGraph
206
206
  def oauth_access_token(client_id, client_secret, options={})
207
207
  url = self.format_url('/oauth/access_token', {
208
208
  :client_id => client_id,
209
- :client_secret => client_secret
209
+ :client_secret => client_secret,
210
+ :redirect_uri => ''
210
211
  }.merge(options || {}))
211
212
 
212
213
  response = self.perform_get(url)
@@ -353,7 +354,7 @@ module FGraph
353
354
  options = stringified_options
354
355
 
355
356
  options.each do |option|
356
- next if option[1].blank?
357
+ next unless option[0]
357
358
  url << "&" if option_count > 0
358
359
  url << "#{option[0]}=#{CGI.escape(option[1].to_s)}"
359
360
  option_count += 1
@@ -1,88 +1,98 @@
1
1
  module FGraph
2
2
  module Rails
3
- module FGraphHelper
4
-
5
- # Access FGraph.config initialized with values set in <tt>[RAILS_ROOT]/config/fgraph.yml</tt>.
6
- def fgraph_config
7
- FGraph.config || {}
8
- end
3
+ module FGraphHelper
4
+
5
+ # Access FGraph.config initialized with values set in <tt>[RAILS_ROOT]/config/fgraph.yml</tt>.
6
+ def fgraph_config
7
+ FGraph.config || {}
8
+ end
9
+
10
+ # Return Facebook session, default to retrieve session from cookies.
11
+ def fgraph_session(app_id = fgraph_config['app_id'],
12
+ app_secret = fgraph_config['app_secret'])
13
+
14
+ return @fgraph_session if @fgraph_session
15
+ @fgraph_session = fgraph_session_cookies(app_id, app_secret)
16
+ end
17
+
18
+ # Return Facebook session cookies.
19
+ def fgraph_session_cookies(app_id = fgraph_config['app_id'],
20
+ app_secret = fgraph_config['app_secret'])
9
21
 
10
- # Return Facebook session, default to retrieve session from cookies.
11
- def fgraph_session(app_id = fgraph_config['app_id'],
12
- app_secret = fgraph_config['app_secret'])
13
-
14
- return @fgraph_session if @fgraph_session
15
- @fgraph_session = fgraph_session_cookies(app_id, app_secret)
16
- end
17
-
18
- # Return Facebook session cookies.
19
- def fgraph_session_cookies(app_id = fgraph_config['app_id'],
20
- app_secret = fgraph_config['app_secret'])
21
-
22
- return @fgraph_session_cookies if @fgraph_session_cookies
23
- return if @fgraph_session_cookies == false
24
-
25
- # retrieve session from cookies
26
- fbs_cookies = request.cookies["fbs_#{app_id}"]
27
- if app_id.blank? or app_secret.blank? or fbs_cookies.blank?
28
- return @fgraph_session_cookies = false
29
- end
22
+ return @fgraph_session_cookies if @fgraph_session_cookies
23
+ return if @fgraph_session_cookies == false
30
24
 
31
- # Parse facebook cookies
32
- fbs_cookies = CGI.parse(fbs_cookies.gsub(/(^\"|\"$)/, ''))
33
- session_cookies = {}
34
- fbs_cookies.each do |key, value|
35
- session_cookies[key] = value[0]
36
- end
37
-
38
- # Validate session cookies
39
- cookie_message = ''
40
- session_cookies_list = session_cookies.sort
41
- session_cookies_list.each do |cookie|
42
- cookie_message += "#{cookie[0]}=#{cookie[1]}" if cookie[0] != 'sig'
43
- end
25
+ fbsr_cookie = request.cookies["fbsr_#{app_id}"]
26
+ if app_id.blank? or app_secret.blank? or fbsr_cookie.blank?
27
+ return @fgraph_session_cookies = false
28
+ end
44
29
 
45
- # Message digest does not match
46
- if Digest::MD5.hexdigest(cookie_message + app_secret) != session_cookies['sig']
47
- @fgraph_session_cookies = false
48
- end
30
+ # Get authorization code and access token
31
+ signed_request = fgraph_parse_signed_request(fbsr_cookie, app_secret)
32
+ resp = FGraph.oauth_access_token(app_id, app_secret, :code => signed_request['code'])
33
+
34
+ @fgraph_session_cookies = {
35
+ 'access_token' => resp['access_token']
36
+ }
37
+ end
38
+
39
+ def fgraph_base64_url_decode(str)
40
+ str += '=' * (4 - str.length.modulo(4))
41
+ Base64.decode64(str.tr('-_', '+/'))
42
+ end
43
+
44
+ # Parses a signed request string provided by Facebook to canvas apps or in a secure cookie.
45
+ #
46
+ # @param Input the signed request from Facebook
47
+ # @raise RuntimeError if the signature is incomplete, invalid, or using an unsupported algorithm
48
+ # @return A hash of the validated request information
49
+ def fgraph_parse_signed_request(input, app_secret)
50
+ encoded_sig, encoded_envelope = input.split('.', 2)
51
+ raise FGraph::OAuthError, 'SignedRequest: Invalid (incomplete) signature data' unless encoded_sig && encoded_envelope
49
52
 
50
- @fgraph_session_cookies = session_cookies
51
- end
52
-
53
- def fgraph_access_token
54
- return unless fgraph_session
55
- fgraph_session['access_token']
56
- end
57
-
58
- def fgraph_logged_in?
59
- return true if fgraph_session and fgraph_access_token
60
- end
61
-
62
- # Currently logged in facebook user
63
- def fgraph_current_user
64
- return @fgraph_current_user if @fgraph_current_user
65
- @fgraph_current_user = fgraph_client.me
66
- end
67
-
68
- # Alias for fgraph_current_user
69
- def fgraph_user
70
- fgraph_current_user
71
- end
72
-
73
- # Return FGraph::Client instance initialized with settings set in <tt>fgraph.yml</tt>.
74
- # Initialized with <tt>:access_token</tt> as well if Facebook session exists.
75
- def fgraph_client
76
- return @fgraph_client if @fgraph_client
77
-
78
- @fgraph_client = FGraph::Client.new(
79
- :client_id => fgraph_config['app_id'],
80
- :client_secret => fgraph_config['app_secret'],
81
- :access_token => fgraph_access_token
82
- )
83
- end
84
-
85
- # Return Facebook object picture url: http://graph.facebook.com/[id]/picture
53
+ signature = fgraph_base64_url_decode(encoded_sig).unpack("H*").first
54
+ envelope = ActiveSupport::JSON.decode(fgraph_base64_url_decode(encoded_envelope))
55
+ raise FGraph::OAuthError, "SignedRequest: Unsupported algorithm #{envelope['algorithm']}" if envelope['algorithm'] != 'HMAC-SHA256'
56
+
57
+ hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, app_secret, encoded_envelope)
58
+ raise FGraph::OAuthError, 'SignedRequest: Invalid signature' if (signature != hmac)
59
+
60
+ envelope
61
+ end
62
+
63
+ def fgraph_access_token
64
+ return unless fgraph_session
65
+ fgraph_session['access_token']
66
+ end
67
+
68
+ def fgraph_logged_in?
69
+ return true if fgraph_session and fgraph_access_token
70
+ end
71
+
72
+ # Currently logged in facebook user
73
+ def fgraph_current_user
74
+ return @fgraph_current_user if @fgraph_current_user
75
+ @fgraph_current_user = fgraph_client.me
76
+ end
77
+
78
+ # Alias for fgraph_current_user
79
+ def fgraph_user
80
+ fgraph_current_user
81
+ end
82
+
83
+ # Return FGraph::Client instance initialized with settings set in <tt>fgraph.yml</tt>.
84
+ # Initialized with <tt>:access_token</tt> as well if Facebook session exists.
85
+ def fgraph_client
86
+ return @fgraph_client if @fgraph_client
87
+
88
+ @fgraph_client = FGraph::Client.new(
89
+ :client_id => fgraph_config['app_id'],
90
+ :client_secret => fgraph_config['app_secret'],
91
+ :access_token => fgraph_access_token
92
+ )
93
+ end
94
+
95
+ # Return Facebook object picture url: http://graph.facebook.com/[id]/picture
86
96
  #
87
97
  # ==== Type Options
88
98
  # * <tt>square</tt> - 50x50 (default)
@@ -40,12 +40,13 @@ module FGraph
40
40
  window.afterFbAsyncInit();
41
41
  }
42
42
  };
43
- (function() {
44
- var e = document.createElement('script'); e.async = true;
45
- e.src = document.location.protocol +
46
- '//connect.facebook.net/en_US/all.js';
47
- document.getElementById('fb-root').appendChild(e);
48
- }());
43
+
44
+ (function(d) {
45
+ var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
46
+ js = d.createElement('script'); js.id = id; js.async = true;
47
+ js.src = "//connect.facebook.net/en_US/all.js";
48
+ d.getElementsByTagName('head')[0].appendChild(js);
49
+ }(document));
49
50
  </script>
50
51
  }
51
52
  else
@@ -229,7 +229,7 @@ class FGraphTest < Test::Unit::TestCase
229
229
 
230
230
  should "return URL without empty options" do
231
231
  formatted_url = FGraph.format_url('/test', {:username => 'john', :age => nil})
232
- assert_equal "https://graph.facebook.com/test?username=john", formatted_url
232
+ assert_equal "https://graph.facebook.com/test?username=john&age=", formatted_url
233
233
  end
234
234
  end
235
235
 
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 6
9
- - 2
10
- version: 0.6.2
8
+ - 7
9
+ - 0
10
+ version: 0.7.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Herryanto Siatono
@@ -15,7 +15,8 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-19 00:00:00 Z
18
+ date: 2011-12-27 00:00:00 +08:00
19
+ default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: httparty
@@ -110,6 +111,7 @@ files:
110
111
  - test/fixtures/access_token.txt
111
112
  - test/fixtures/object_cocacola.json
112
113
  - test/test_helper.rb
114
+ has_rdoc: true
113
115
  homepage: http://github.com/jugend/fgraph
114
116
  licenses: []
115
117
 
@@ -139,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
141
  requirements: []
140
142
 
141
143
  rubyforge_project:
142
- rubygems_version: 1.8.11
144
+ rubygems_version: 1.4.2
143
145
  signing_key:
144
146
  specification_version: 3
145
147
  summary: Ruby Facebook Graph API