rest-core 0.4.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +2 -7
- data/CHANGES.md +13 -2
- data/Gemfile +0 -3
- data/README.md +30 -34
- data/Rakefile +5 -46
- data/lib/rest-core.rb +0 -6
- data/lib/rest-core/version.rb +1 -1
- data/rest-core.gemspec +5 -83
- metadata +11 -87
- data/example/rails2/Gemfile +0 -21
- data/example/rails2/README +0 -4
- data/example/rails2/Rakefile +0 -11
- data/example/rails2/app/controllers/application_controller.rb +0 -128
- data/example/rails2/app/views/application/helper.html.erb +0 -2
- data/example/rails2/config/boot.rb +0 -130
- data/example/rails2/config/environment.rb +0 -15
- data/example/rails2/config/environments/development.rb +0 -17
- data/example/rails2/config/environments/production.rb +0 -28
- data/example/rails2/config/environments/test.rb +0 -30
- data/example/rails2/config/initializers/cookie_verification_secret.rb +0 -7
- data/example/rails2/config/initializers/new_rails_defaults.rb +0 -21
- data/example/rails2/config/initializers/session_store.rb +0 -15
- data/example/rails2/config/preinitializer.rb +0 -23
- data/example/rails2/config/rest-core.yaml +0 -16
- data/example/rails2/config/routes.rb +0 -43
- data/example/rails2/log +0 -0
- data/example/rails2/test/functional/application_controller_test.rb +0 -197
- data/example/rails2/test/test_helper.rb +0 -18
- data/example/rails2/test/unit/rails_util_test.rb +0 -44
- data/example/rails3/Gemfile +0 -20
- data/example/rails3/README +0 -4
- data/example/rails3/Rakefile +0 -7
- data/example/rails3/app/controllers/application_controller.rb +0 -128
- data/example/rails3/app/views/application/helper.html.erb +0 -2
- data/example/rails3/config.ru +0 -4
- data/example/rails3/config/application.rb +0 -23
- data/example/rails3/config/environment.rb +0 -5
- data/example/rails3/config/environments/development.rb +0 -26
- data/example/rails3/config/environments/production.rb +0 -49
- data/example/rails3/config/environments/test.rb +0 -30
- data/example/rails3/config/initializers/secret_token.rb +0 -7
- data/example/rails3/config/initializers/session_store.rb +0 -8
- data/example/rails3/config/rest-core.yaml +0 -16
- data/example/rails3/config/routes.rb +0 -5
- data/example/rails3/test/functional/application_controller_test.rb +0 -197
- data/example/rails3/test/test_helper.rb +0 -18
- data/example/rails3/test/unit/rails_util_test.rb +0 -44
- data/example/sinatra/config.ru +0 -16
- data/lib/rest-core/client/facebook.rb +0 -251
- data/lib/rest-core/client/facebook/rails_util.rb +0 -333
- data/lib/rest-core/client/flurry.rb +0 -96
- data/lib/rest-core/client/flurry/rails_util.rb +0 -74
- data/lib/rest-core/client/github.rb +0 -18
- data/lib/rest-core/client/linkedin.rb +0 -59
- data/lib/rest-core/client/mixi.rb +0 -47
- data/lib/rest-core/client/twitter.rb +0 -101
- data/test/client/facebook/config/rest-core.yaml +0 -8
- data/test/client/facebook/test_api.rb +0 -97
- data/test/client/facebook/test_cache.rb +0 -58
- data/test/client/facebook/test_default.rb +0 -23
- data/test/client/facebook/test_error.rb +0 -65
- data/test/client/facebook/test_handler.rb +0 -84
- data/test/client/facebook/test_load_config.rb +0 -39
- data/test/client/facebook/test_misc.rb +0 -72
- data/test/client/facebook/test_oauth.rb +0 -38
- data/test/client/facebook/test_old.rb +0 -114
- data/test/client/facebook/test_page.rb +0 -106
- data/test/client/facebook/test_parse.rb +0 -128
- data/test/client/facebook/test_serialize.rb +0 -43
- data/test/client/facebook/test_timeout.rb +0 -22
- data/test/client/flurry/test_metrics.rb +0 -83
- data/test/client/twitter/test_api.rb +0 -37
data/example/sinatra/config.ru
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
|
2
|
-
require 'sinatra'
|
3
|
-
require 'rest-core'
|
4
|
-
|
5
|
-
app_id = '123'
|
6
|
-
secret = 'abc'
|
7
|
-
config = {:app_id => app_id,
|
8
|
-
:secret => secret}
|
9
|
-
|
10
|
-
post '/' do
|
11
|
-
fb = RestCore::Facebook.new(config)
|
12
|
-
fb.parse_signed_request!(params['signed_request'])
|
13
|
-
"#{fb.get('me').inspect.gsub('<', '<')}\n"
|
14
|
-
end
|
15
|
-
|
16
|
-
run Sinatra::Application
|
@@ -1,251 +0,0 @@
|
|
1
|
-
|
2
|
-
require 'rest-core'
|
3
|
-
require 'rest-core/util/hmac'
|
4
|
-
|
5
|
-
RestCore::Facebook = RestCore::Builder.client(
|
6
|
-
:data, :app_id, :secret, :old_site) do
|
7
|
-
|
8
|
-
s = self.class # this is only for ruby 1.8!
|
9
|
-
use s::Timeout , 10
|
10
|
-
|
11
|
-
use s::DefaultSite , 'https://graph.facebook.com/'
|
12
|
-
use s::DefaultHeaders, {'Accept' => 'application/json',
|
13
|
-
'Accept-Language' => 'en-us'}
|
14
|
-
use s::Oauth2Query , nil
|
15
|
-
|
16
|
-
use s::CommonLogger , nil
|
17
|
-
use s::Cache , nil, 600 do
|
18
|
-
use s::ErrorHandler, lambda{ |env|
|
19
|
-
raise ::RestCore::Facebook::Error.call(env) }
|
20
|
-
use s::ErrorDetector, lambda{ |env|
|
21
|
-
if env[s::RESPONSE_BODY].kind_of?(Hash)
|
22
|
-
env[s::RESPONSE_BODY]['error'] ||
|
23
|
-
env[s::RESPONSE_BODY]['error_code']
|
24
|
-
end}
|
25
|
-
|
26
|
-
use s::JsonDecode , true
|
27
|
-
end
|
28
|
-
|
29
|
-
use s::Defaults , :data => lambda{{}},
|
30
|
-
:old_site => 'https://api.facebook.com/'
|
31
|
-
end
|
32
|
-
|
33
|
-
class RestCore::Facebook::Error < RestCore::Error
|
34
|
-
include RestCore
|
35
|
-
class AccessToken < Facebook::Error; end
|
36
|
-
class InvalidAccessToken < AccessToken ; end
|
37
|
-
class MissingAccessToken < AccessToken ; end
|
38
|
-
|
39
|
-
attr_reader :error, :url
|
40
|
-
def initialize error, url=''
|
41
|
-
@error, @url = error, url
|
42
|
-
super("#{error.inspect} from #{url}")
|
43
|
-
end
|
44
|
-
|
45
|
-
def self.call env
|
46
|
-
error, url = env[RESPONSE_BODY], Middleware.request_uri(env)
|
47
|
-
return new(env[FAIL], url) unless error.kind_of?(Hash)
|
48
|
-
if invalid_token?(error)
|
49
|
-
InvalidAccessToken.new(error, url)
|
50
|
-
elsif missing_token?(error)
|
51
|
-
MissingAccessToken.new(error, url)
|
52
|
-
else
|
53
|
-
new(error, url)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
def self.invalid_token? error
|
58
|
-
(%w[OAuthInvalidTokenException
|
59
|
-
OAuthException].include?((error['error'] || {})['type'])) ||
|
60
|
-
(error['error_code'] == 190) # Invalid OAuth 2.0 Access Token
|
61
|
-
end
|
62
|
-
|
63
|
-
def self.missing_token? error
|
64
|
-
(error['error'] || {})['message'] =~ /^An active access token/ ||
|
65
|
-
(error['error_code'] == 104) # Requires valid signature
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
module RestCore::Facebook::Client
|
70
|
-
include RestCore
|
71
|
-
|
72
|
-
def access_token
|
73
|
-
data['access_token'] || data['oauth_token'] if data.kind_of?(Hash)
|
74
|
-
end
|
75
|
-
|
76
|
-
def access_token= token
|
77
|
-
data['access_token'] = token if data.kind_of?(Hash)
|
78
|
-
end
|
79
|
-
|
80
|
-
def secret_access_token; "#{app_id}|#{secret}" ; end
|
81
|
-
def accept ; headers['Accept'] ; end
|
82
|
-
def accept= val; headers['Accept'] = val; end
|
83
|
-
def lang ; headers['Accept-Language'] ; end
|
84
|
-
def lang= val; headers['Accept-Language'] = val; end
|
85
|
-
|
86
|
-
def authorized? ; !!access_token ; end
|
87
|
-
|
88
|
-
def next_page hash, opts={}, &cb
|
89
|
-
if hash['paging'].kind_of?(Hash) && hash['paging']['next']
|
90
|
-
get(hash['paging']['next'], {}, opts, &cb)
|
91
|
-
else
|
92
|
-
yield(nil) if block_given?
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
def prev_page hash, opts={}, &cb
|
97
|
-
if hash['paging'].kind_of?(Hash) && hash['paging']['previous']
|
98
|
-
get(hash['paging']['previous'], {}, opts, &cb)
|
99
|
-
else
|
100
|
-
yield(nil) if block_given?
|
101
|
-
end
|
102
|
-
end
|
103
|
-
alias_method :previous_page, :prev_page
|
104
|
-
|
105
|
-
def for_pages hash, pages=1, opts={}, kind=:next_page, &cb
|
106
|
-
if pages > 1
|
107
|
-
merge_data(send(kind, hash, opts){ |result|
|
108
|
-
yield(result.freeze) if block_given?
|
109
|
-
for_pages(result, pages - 1, opts, kind, &cb) if result
|
110
|
-
}, hash)
|
111
|
-
else
|
112
|
-
yield(nil) if block_given?
|
113
|
-
hash
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
# cookies, app_id, secrect related below
|
118
|
-
|
119
|
-
def parse_rack_env! env
|
120
|
-
env['HTTP_COOKIE'].to_s =~ /fbs_#{app_id}=([^\;]+)/
|
121
|
-
self.data = parse_fbs!($1)
|
122
|
-
end
|
123
|
-
|
124
|
-
def parse_cookies! cookies
|
125
|
-
self.data = parse_fbs!(cookies["fbs_#{app_id}"])
|
126
|
-
end
|
127
|
-
|
128
|
-
def parse_fbs! fbs
|
129
|
-
self.data = check_sig_and_return_data(
|
130
|
-
# take out facebook sometimes there but sometimes not quotes in cookies
|
131
|
-
Vendor.parse_query(fbs.to_s.sub(/^"/, '').sub(/"$/, '')))
|
132
|
-
end
|
133
|
-
|
134
|
-
def parse_json! json
|
135
|
-
self.data = json &&
|
136
|
-
check_sig_and_return_data(JsonDecode.json_decode(json))
|
137
|
-
rescue JsonDecode::ParseError
|
138
|
-
self.data = nil
|
139
|
-
end
|
140
|
-
|
141
|
-
def fbs
|
142
|
-
"#{fbs_without_sig(data).join('&')}&sig=#{calculate_sig(data)}"
|
143
|
-
end
|
144
|
-
|
145
|
-
# facebook's new signed_request...
|
146
|
-
|
147
|
-
def parse_signed_request! request
|
148
|
-
sig_encoded, json_encoded = request.split('.')
|
149
|
-
sig, json = [sig_encoded, json_encoded].map{ |str|
|
150
|
-
"#{str.tr('-_', '+/')}==".unpack('m').first
|
151
|
-
}
|
152
|
-
self.data = check_sig_and_return_data(
|
153
|
-
JsonDecode.json_decode(json).merge('sig' => sig)){
|
154
|
-
Hmac.sha256(secret, json_encoded)
|
155
|
-
}
|
156
|
-
rescue JsonDecode::ParseError
|
157
|
-
self.data = nil
|
158
|
-
end
|
159
|
-
|
160
|
-
# oauth related
|
161
|
-
|
162
|
-
def authorize_url opts={}
|
163
|
-
url('oauth/authorize',
|
164
|
-
{:client_id => app_id, :access_token => nil}.merge(opts))
|
165
|
-
end
|
166
|
-
|
167
|
-
def authorize! opts={}
|
168
|
-
query = {:client_id => app_id, :client_secret => secret}.merge(opts)
|
169
|
-
self.data = Vendor.parse_query(
|
170
|
-
get(url('oauth/access_token'), query,
|
171
|
-
{:json_decode => false}.merge(opts)))
|
172
|
-
end
|
173
|
-
|
174
|
-
# old rest facebook api, i will definitely love to remove them someday
|
175
|
-
|
176
|
-
def old_rest path, query={}, opts={}, &cb
|
177
|
-
uri = url("method/#{path}", {:format => 'json'}.merge(query),
|
178
|
-
{:site => old_site}.merge(opts))
|
179
|
-
if opts[:post]
|
180
|
-
post(url("method/#{path}", {:format => 'json'},
|
181
|
-
{:site => old_site}.merge(opts)),
|
182
|
-
query,
|
183
|
-
{} ,
|
184
|
-
opts.merge('cache.key' => uri, 'cache.post' => true),
|
185
|
-
&cb)
|
186
|
-
else
|
187
|
-
get(uri, {}, opts, &cb)
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
191
|
-
def secret_old_rest path, query={}, opts={}, &cb
|
192
|
-
old_rest(path, query, {:secret => true}.merge(opts), &cb)
|
193
|
-
end
|
194
|
-
|
195
|
-
def fql code, query={}, opts={}, &cb
|
196
|
-
old_rest('fql.query', {:query => code}.merge(query), opts, &cb)
|
197
|
-
end
|
198
|
-
|
199
|
-
def fql_multi codes, query={}, opts={}, &cb
|
200
|
-
old_rest('fql.multiquery',
|
201
|
-
{:queries => JsonDecode.json_encode(codes)}.merge(query), opts, &cb)
|
202
|
-
end
|
203
|
-
|
204
|
-
def exchange_sessions query={}, opts={}, &cb
|
205
|
-
q = {:client_id => app_id, :client_secret => secret,
|
206
|
-
:type => 'client_cred'}.merge(query)
|
207
|
-
post(url('oauth/exchange_sessions', q),
|
208
|
-
{}, {}, opts, &cb)
|
209
|
-
end
|
210
|
-
|
211
|
-
protected
|
212
|
-
def build_env env={}
|
213
|
-
super(env.inject({}){ |r, (k, v)|
|
214
|
-
case k.to_s
|
215
|
-
when 'secret' ; r['access_token'] = secret_access_token
|
216
|
-
when 'cache' ; r['cache.update'] = !!!v
|
217
|
-
else ; r[k.to_s] = v
|
218
|
-
end
|
219
|
-
r
|
220
|
-
})
|
221
|
-
end
|
222
|
-
|
223
|
-
def check_sig_and_return_data cookies
|
224
|
-
cookies if secret && if block_given?
|
225
|
-
yield
|
226
|
-
else
|
227
|
-
calculate_sig(cookies)
|
228
|
-
end == cookies['sig']
|
229
|
-
end
|
230
|
-
|
231
|
-
def calculate_sig cookies
|
232
|
-
Digest::MD5.hexdigest(fbs_without_sig(cookies).join + secret)
|
233
|
-
end
|
234
|
-
|
235
|
-
def fbs_without_sig cookies
|
236
|
-
cookies.reject{ |(k, v)| k == 'sig' }.sort.map{ |a| a.join('=') }
|
237
|
-
end
|
238
|
-
|
239
|
-
def merge_data lhs, rhs
|
240
|
-
[lhs, rhs].each{ |hash|
|
241
|
-
return rhs.reject{ |k, v| k == 'paging' } if
|
242
|
-
!hash.kind_of?(Hash) || !hash['data'].kind_of?(Array)
|
243
|
-
}
|
244
|
-
lhs['data'].unshift(*rhs['data'])
|
245
|
-
lhs
|
246
|
-
end
|
247
|
-
end
|
248
|
-
|
249
|
-
RestCore::Facebook.send(:include, RestCore::Facebook::Client)
|
250
|
-
require 'rest-core/client/facebook/rails_util' if
|
251
|
-
Object.const_defined?(:Rails)
|
@@ -1,333 +0,0 @@
|
|
1
|
-
|
2
|
-
require 'rest-core/util/rails_util_util'
|
3
|
-
|
4
|
-
require 'cgi'
|
5
|
-
require 'uri'
|
6
|
-
|
7
|
-
module RestCore::Facebook::DefaultAttributes
|
8
|
-
def default_log_method ; Rails.logger.method(:debug); end
|
9
|
-
def default_cache ; Rails.cache ; end
|
10
|
-
def default_canvas ; '' ; end
|
11
|
-
def default_iframe ; false ; end
|
12
|
-
def default_auto_authorize ; false ; end
|
13
|
-
def default_auto_authorize_options; {} ; end
|
14
|
-
def default_auto_authorize_scope ; '' ; end
|
15
|
-
def default_ensure_authorized ; false ; end
|
16
|
-
def default_write_session ; false ; end
|
17
|
-
def default_write_cookies ; false ; end
|
18
|
-
def default_write_handler ; nil ; end
|
19
|
-
def default_check_handler ; nil ; end
|
20
|
-
end
|
21
|
-
|
22
|
-
module RestCore::Facebook::RailsUtil
|
23
|
-
def self.init app=Rails
|
24
|
-
RestCore::Config.load_for_rails(RestCore::Facebook, 'facebook', app)
|
25
|
-
end
|
26
|
-
|
27
|
-
module Helper
|
28
|
-
def rc_facebook
|
29
|
-
controller.send(:rc_facebook)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def self.included controller
|
34
|
-
# skip if included already, any better way to detect this?
|
35
|
-
return if controller.respond_to?(:rc_facebook, true)
|
36
|
-
|
37
|
-
controller.rescue_from(RestCore::Facebook::Error::AccessToken,
|
38
|
-
:with => :rc_facebook_on_access_token_error)
|
39
|
-
controller.helper(RestCore::Facebook::RailsUtil::Helper)
|
40
|
-
controller.instance_methods.select{ |method|
|
41
|
-
method.to_s =~ /^rc_facebook/
|
42
|
-
}.each{ |method| controller.send(:protected, method) }
|
43
|
-
end
|
44
|
-
|
45
|
-
def rc_facebook_setup options={}
|
46
|
-
rc_facebook_options_ctl.merge!(
|
47
|
-
RestCore::RailsUtilUtil.extract_options(
|
48
|
-
RestCore::Facebook.members, options, :reject))
|
49
|
-
rc_facebook_options_new.merge!(
|
50
|
-
RestCore::RailsUtilUtil.extract_options(
|
51
|
-
RestCore::Facebook.members, options, :select))
|
52
|
-
|
53
|
-
# we'll need to reinitialize rc_facebook with the new options,
|
54
|
-
# otherwise if you're calling rc_facebook before rc_facebook_setup,
|
55
|
-
# you'll end up with default options without the ones you've passed
|
56
|
-
# into rc_facebook_setup.
|
57
|
-
rc_facebook.send(:initialize, rc_facebook_options_new)
|
58
|
-
|
59
|
-
rc_facebook_check_params_signed_request # canvas
|
60
|
-
rc_facebook_check_params_session # i think it would be deprecated
|
61
|
-
rc_facebook_check_cookie # for js sdk (canvas or not)
|
62
|
-
rc_facebook_check_code # oauth api
|
63
|
-
|
64
|
-
# there are above 4 ways to check the user identity!
|
65
|
-
# if nor of them passed, then we can suppose the user
|
66
|
-
# didn't authorize for us, but we can check if user has authorized
|
67
|
-
# before, in that case, the fbs would be inside session,
|
68
|
-
# as we just saved it there
|
69
|
-
|
70
|
-
rc_facebook_check_rg_fbs # check rc_facebook storage
|
71
|
-
|
72
|
-
if rc_facebook_oget(:ensure_authorized) && !rc_facebook.authorized?
|
73
|
-
rc_facebook_authorize('ensure authorized')
|
74
|
-
false # action halt, redirect to do authorize,
|
75
|
-
# eagerly, as opposed to auto_authorize
|
76
|
-
else
|
77
|
-
true # keep going
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
# override this if you need different app_id and secret
|
82
|
-
def rc_facebook
|
83
|
-
@rc_facebook ||= RestCore::Facebook.new(rc_facebook_options_new)
|
84
|
-
end
|
85
|
-
|
86
|
-
def rc_facebook_on_access_token_error error=nil
|
87
|
-
rc_facebook_authorize(error, false)
|
88
|
-
end
|
89
|
-
|
90
|
-
def rc_facebook_authorize error=nil, force_redirect=true
|
91
|
-
logger.warn("WARN: Facebook: #{error.inspect}")
|
92
|
-
|
93
|
-
if force_redirect || rc_facebook_auto_authorize?
|
94
|
-
@rc_facebook_authorize_url = rc_facebook.authorize_url(
|
95
|
-
{:redirect_uri => rc_facebook_normalized_request_uri,
|
96
|
-
:scope => rc_facebook_oget(:auto_authorize_scope)}.
|
97
|
-
merge(rc_facebook_oget(:auto_authorize_options)))
|
98
|
-
|
99
|
-
logger.debug(
|
100
|
-
"DEBUG: Facebook: redirect to #{@rc_facebook_authorize_url}")
|
101
|
-
|
102
|
-
cookies.delete("fbs_#{rc_facebook.app_id}")
|
103
|
-
rc_facebook_authorize_redirect
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
# override this if you want the simple redirect_to
|
108
|
-
def rc_facebook_authorize_redirect
|
109
|
-
unless rc_facebook_in_canvas?
|
110
|
-
redirect_to @rc_facebook_authorize_url
|
111
|
-
else
|
112
|
-
rc_facebook_js_redirect(@rc_facebook_authorize_url,
|
113
|
-
rc_facebook_authorize_body)
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
def rc_facebook_js_redirect redirect_url, body=''
|
118
|
-
render :inline => <<-HTML
|
119
|
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
120
|
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
121
|
-
<html>
|
122
|
-
<head>
|
123
|
-
<script type="text/javascript">
|
124
|
-
window.top.location.href = '#{redirect_url}'
|
125
|
-
</script>
|
126
|
-
<noscript>
|
127
|
-
<meta http-equiv="refresh" content="0;url=#{
|
128
|
-
CGI.escapeHTML(redirect_url)}"/>
|
129
|
-
<meta http-equiv="window-target" content="_top"/>
|
130
|
-
</noscript>
|
131
|
-
</head>
|
132
|
-
<body>
|
133
|
-
#{body}
|
134
|
-
</bodt>
|
135
|
-
</html>
|
136
|
-
HTML
|
137
|
-
end
|
138
|
-
|
139
|
-
def rc_facebook_authorize_body redirect_url=@rc_facebook_authorize_url
|
140
|
-
<<-HTML
|
141
|
-
<div>
|
142
|
-
Please
|
143
|
-
<a href="#{CGI.escapeHTML(redirect_url)}" target="_top">authorize</a>
|
144
|
-
if this page is not automatically redirected.
|
145
|
-
</div>
|
146
|
-
HTML
|
147
|
-
end
|
148
|
-
|
149
|
-
module_function
|
150
|
-
|
151
|
-
# ==================== begin options utility =======================
|
152
|
-
def rc_facebook_oget key
|
153
|
-
if rc_facebook_options_ctl.has_key?(key)
|
154
|
-
rc_facebook_options_ctl[key]
|
155
|
-
else
|
156
|
-
RestCore::Facebook.send("default_#{key}")
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
def rc_facebook_options_ctl
|
161
|
-
@rc_facebook_options_ctl ||= {}
|
162
|
-
end
|
163
|
-
|
164
|
-
def rc_facebook_options_new
|
165
|
-
@rc_facebook_options_new ||= {}
|
166
|
-
end
|
167
|
-
# ==================== end options utility =======================
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
# ==================== begin facebook check ======================
|
172
|
-
def rc_facebook_check_params_signed_request
|
173
|
-
return if rc_facebook.authorized? || !params[:signed_request]
|
174
|
-
|
175
|
-
rc_facebook.parse_signed_request!(params[:signed_request])
|
176
|
-
logger.debug("DEBUG: Facebook: detected signed_request," \
|
177
|
-
" parsed: #{rc_facebook.data.inspect}")
|
178
|
-
|
179
|
-
if rc_facebook.authorized?
|
180
|
-
rc_facebook_write_rg_fbs
|
181
|
-
else
|
182
|
-
logger.warn(
|
183
|
-
"WARN: Facebook: bad signed_request: #{params[:signed_request]}")
|
184
|
-
end
|
185
|
-
end
|
186
|
-
|
187
|
-
# if the code is bad or not existed,
|
188
|
-
# check if there's one in session,
|
189
|
-
# meanwhile, there the sig and access_token is correct,
|
190
|
-
# that means we're in the context of canvas
|
191
|
-
def rc_facebook_check_params_session
|
192
|
-
return if rc_facebook.authorized? || !params[:session]
|
193
|
-
|
194
|
-
rc_facebook.parse_json!(params[:session])
|
195
|
-
logger.debug("DEBUG: Facebook: detected session, parsed:" \
|
196
|
-
" #{rc_facebook.data.inspect}")
|
197
|
-
|
198
|
-
if rc_facebook.authorized?
|
199
|
-
rc_facebook_write_rg_fbs
|
200
|
-
else
|
201
|
-
logger.warn("WARN: Facebook: bad session: #{params[:session]}")
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
|
-
# if we're not in canvas nor code passed,
|
206
|
-
# we could check out cookies as well.
|
207
|
-
def rc_facebook_check_cookie
|
208
|
-
return if rc_facebook.authorized? ||
|
209
|
-
!cookies["fbs_#{rc_facebook.app_id}"]
|
210
|
-
|
211
|
-
rc_facebook.parse_cookies!(cookies)
|
212
|
-
logger.debug("DEBUG: Facebook: detected cookies, parsed:" \
|
213
|
-
" #{rc_facebook.data.inspect}")
|
214
|
-
end
|
215
|
-
|
216
|
-
# exchange the code with access_token
|
217
|
-
def rc_facebook_check_code
|
218
|
-
return if rc_facebook.authorized? || !params[:code]
|
219
|
-
|
220
|
-
rc_facebook.authorize!(
|
221
|
-
:code => params[:code],
|
222
|
-
:redirect_uri => rc_facebook_normalized_request_uri)
|
223
|
-
|
224
|
-
logger.debug(
|
225
|
-
"DEBUG: Facebook: detected code with " \
|
226
|
-
"#{rc_facebook_normalized_request_uri}," \
|
227
|
-
" parsed: #{rc_facebook.data.inspect}")
|
228
|
-
|
229
|
-
rc_facebook_write_rg_fbs if rc_facebook.authorized?
|
230
|
-
end
|
231
|
-
# ==================== end facebook check ======================
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
# ==================== begin check ================================
|
236
|
-
def rc_facebook_storage_key
|
237
|
-
"rc_facebook_fbs_#{rc_facebook_oget(:app_id)}"
|
238
|
-
end
|
239
|
-
|
240
|
-
def rc_facebook_check_rg_fbs
|
241
|
-
rc_facebook_check_rg_handler # custom method to store fbs
|
242
|
-
rc_facebook_check_rg_session # prefered way to store fbs
|
243
|
-
rc_facebook_check_rg_cookies # in canvas, session might not work..
|
244
|
-
end
|
245
|
-
|
246
|
-
def rc_facebook_check_rg_handler handler=rc_facebook_oget(:check_handler)
|
247
|
-
return if rc_facebook.authorized? || !handler
|
248
|
-
rc_facebook.parse_fbs!(handler.call)
|
249
|
-
logger.debug("DEBUG: Facebook: called check_handler, parsed:" \
|
250
|
-
" #{rc_facebook.data.inspect}")
|
251
|
-
end
|
252
|
-
|
253
|
-
def rc_facebook_check_rg_session
|
254
|
-
return if rc_facebook.authorized? || !rc_facebook_oget(:write_session) ||
|
255
|
-
!(fbs = session[rc_facebook_storage_key])
|
256
|
-
rc_facebook.parse_fbs!(fbs)
|
257
|
-
logger.debug("DEBUG: Facebook: detected rc_facebook session, parsed:" \
|
258
|
-
" #{rc_facebook.data.inspect}")
|
259
|
-
end
|
260
|
-
|
261
|
-
def rc_facebook_check_rg_cookies
|
262
|
-
return if rc_facebook.authorized? || !rc_facebook_oget(:write_cookies) ||
|
263
|
-
!(fbs = cookies[rc_facebook_storage_key])
|
264
|
-
rc_facebook.parse_fbs!(fbs)
|
265
|
-
logger.debug("DEBUG: Facebook: detected rc_facebook cookies, parsed:" \
|
266
|
-
" #{rc_facebook.data.inspect}")
|
267
|
-
end
|
268
|
-
# ==================== end check ================================
|
269
|
-
# ==================== begin write ================================
|
270
|
-
def rc_facebook_write_rg_fbs
|
271
|
-
cookies.delete("fbs_#{rc_facebook.app_id}")
|
272
|
-
rc_facebook_write_rg_handler
|
273
|
-
rc_facebook_write_rg_session
|
274
|
-
rc_facebook_write_rg_cookies
|
275
|
-
end
|
276
|
-
|
277
|
-
def rc_facebook_write_rg_handler handler=rc_facebook_oget(:write_handler)
|
278
|
-
return if !handler
|
279
|
-
handler.call(fbs = rc_facebook.fbs)
|
280
|
-
logger.debug("DEBUG: Facebook: called write_handler: fbs => #{fbs}")
|
281
|
-
end
|
282
|
-
|
283
|
-
def rc_facebook_write_rg_session
|
284
|
-
return if !rc_facebook_oget(:write_session)
|
285
|
-
session[rc_facebook_storage_key] = fbs = rc_facebook.fbs
|
286
|
-
logger.debug("DEBUG: Facebook: wrote session: fbs => #{fbs}")
|
287
|
-
end
|
288
|
-
|
289
|
-
def rc_facebook_write_rg_cookies
|
290
|
-
return if !rc_facebook_oget(:write_cookies)
|
291
|
-
cookies[rc_facebook_storage_key] = fbs = rc_facebook.fbs
|
292
|
-
logger.debug("DEBUG: Facebook: wrote cookies: fbs => #{fbs}")
|
293
|
-
end
|
294
|
-
# ==================== end write ================================
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
# ==================== begin misc ================================
|
299
|
-
def rc_facebook_normalized_request_uri
|
300
|
-
uri = if rc_facebook_in_canvas?
|
301
|
-
# rails 3 uses newer rack which has fullpath
|
302
|
-
"http://apps.facebook.com/#{rc_facebook_oget(:canvas)}" +
|
303
|
-
(request.respond_to?(:fullpath) ?
|
304
|
-
request.fullpath : request.request_uri)
|
305
|
-
else
|
306
|
-
request.url
|
307
|
-
end
|
308
|
-
|
309
|
-
rc_facebook_filter_uri(uri)
|
310
|
-
end
|
311
|
-
|
312
|
-
def rc_facebook_filter_uri uri
|
313
|
-
URI.parse(URI.encode(uri)).tap{ |uri|
|
314
|
-
uri.query = uri.query.split('&').reject{ |q|
|
315
|
-
q =~ /^(code|session|signed_request)\=/
|
316
|
-
}.join('&') if uri.query
|
317
|
-
uri.query = nil if uri.query.blank?
|
318
|
-
}.to_s
|
319
|
-
end
|
320
|
-
|
321
|
-
def rc_facebook_in_canvas?
|
322
|
-
!rc_facebook_oget(:canvas).blank?
|
323
|
-
end
|
324
|
-
|
325
|
-
def rc_facebook_auto_authorize?
|
326
|
-
!rc_facebook_oget(:auto_authorize_scope) .blank? ||
|
327
|
-
!rc_facebook_oget(:auto_authorize_options).blank? ||
|
328
|
-
rc_facebook_oget(:auto_authorize)
|
329
|
-
end
|
330
|
-
# ==================== end misc ================================
|
331
|
-
end
|
332
|
-
|
333
|
-
RestCore::Facebook::RailsUtil.init(Rails)
|