Module: RestCore::Facebook::RailsUtil

Includes:
RailsUtilUtil
Defined in:
lib/rest-core/client/facebook/rails_util.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RailsUtilUtil

extend_rails_util, include_rails_util, load_config, setup_helper

Class Method Details

.included(controller) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/rest-core/client/facebook/rails_util.rb', line 25

def self.included controller
  # skip if included already, any better way to detect this?
  return if controller.respond_to?(:rc_facebook, true)
  super
  controller.rescue_from(RestCore::Facebook::Error::AccessToken,
                         :with => :rc_facebook_on_access_token_error)
end

Instance Method Details

#rc_facebook_authorize(error = nil, force_redirect = true) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rest-core/client/facebook/rails_util.rb', line 57

def rc_facebook_authorize error=nil, force_redirect=true
  logger.info("INFO: Facebook: #{error.inspect}")

  if force_redirect || rc_facebook_auto_authorize?
    rc_facebook_cleanup

    @rc_facebook_authorize_url = rc_facebook.authorize_url(
      {:redirect_uri => rc_facebook_normalized_request_uri,
       :scope        =>
         rc_options_get(RestCore::Facebook, :auto_authorize_scope)}.
      merge(rc_options_get(RestCore::Facebook, :auto_authorize_options)))

    logger.debug(
      "DEBUG: Facebook: redirect to #{@rc_facebook_authorize_url}")

    rc_facebook_authorize_redirect
  end
end

#rc_facebook_authorize_body(redirect_url = @rc_facebook_authorize_url) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/rest-core/client/facebook/rails_util.rb', line 108

def rc_facebook_authorize_body redirect_url=@rc_facebook_authorize_url
  <<-HTML
  <div>
    Please
    <a href="#{CGI.escapeHTML(redirect_url)}" target="_top">authorize</a>
    if this page is not automatically redirected.
  </div>
  HTML
end

#rc_facebook_authorize_redirectObject

override this if you want the simple redirect_to



77
78
79
80
81
82
83
84
# File 'lib/rest-core/client/facebook/rails_util.rb', line 77

def rc_facebook_authorize_redirect
  unless rc_facebook_in_canvas?
    redirect_to @rc_facebook_authorize_url
  else
    rc_facebook_js_redirect(@rc_facebook_authorize_url,
                             rc_facebook_authorize_body)
  end
end

#rc_facebook_auto_authorize?Boolean

Returns:

  • (Boolean)


301
302
303
304
305
306
# File 'lib/rest-core/client/facebook/rails_util.rb', line 301

def rc_facebook_auto_authorize?
  client = RestCore::Facebook
  !rc_options_get(client, :auto_authorize_scope)  .blank? ||
  !rc_options_get(client, :auto_authorize_options).blank? ||
   rc_options_get(client, :auto_authorize)
end

#rc_facebook_check_codeObject

exchange the code with access_token



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rest-core/client/facebook/rails_util.rb', line 169

def rc_facebook_check_code
  return if rc_facebook.authorized? || !params[:code]

  rc_facebook.authorize!(
    :code         => params[:code],
    :redirect_uri => rc_facebook_normalized_request_uri)

  logger.debug(
    "DEBUG: Facebook: detected code with "   \
    "#{rc_facebook_normalized_request_uri}," \
    " parsed: #{rc_facebook.data.inspect}")

  rc_facebook_write_fbs if rc_facebook.authorized?
end

if we’re not in canvas nor code passed, we could check out cookies as well.



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rest-core/client/facebook/rails_util.rb', line 156

def rc_facebook_check_cookie
  return if rc_facebook.authorized?                 ||
            (!cookies["fbsr_#{rc_facebook.app_id}"] &&
             !cookies["fbs_#{rc_facebook.app_id}"])

  rc_facebook.parse_cookies!(cookies)
  logger.debug("DEBUG: Facebook: detected cookies, parsed:" \
               " #{rc_facebook.data.inspect}")

  rc_facebook_write_fbs if rc_facebook.authorized?
end

#rc_facebook_check_cookiesObject



217
218
219
220
221
222
223
224
225
# File 'lib/rest-core/client/facebook/rails_util.rb', line 217

def rc_facebook_check_cookies
  return if rc_facebook.authorized?                             ||
            !rc_options_get(RestCore::Facebook, :write_cookies) ||
            !(fbs = cookies[rc_facebook_storage_key])

  rc_facebook.parse_fbs!(fbs)
  logger.debug("DEBUG: Facebook: detected rc_facebook cookies, parsed:" \
               " #{rc_facebook.data.inspect}")
end

#rc_facebook_check_fbsObject



192
193
194
195
196
# File 'lib/rest-core/client/facebook/rails_util.rb', line 192

def rc_facebook_check_fbs
  rc_facebook_check_handler # custom method to store fbs
  rc_facebook_check_session # prefered way to store fbs
  rc_facebook_check_cookies # in canvas, session might not work..
end

#rc_facebook_check_handler(handler = rc_options_get(RestCore::Facebook, :check_handler)) ⇒ Object



198
199
200
201
202
203
204
205
# File 'lib/rest-core/client/facebook/rails_util.rb', line 198

def rc_facebook_check_handler handler=
    rc_options_get(RestCore::Facebook, :check_handler)

  return if rc_facebook.authorized? || !handler
  rc_facebook.parse_fbs!(handler.call)
  logger.debug("DEBUG: Facebook: called check_handler, parsed:" \
               " #{rc_facebook.data.inspect}")
end

#rc_facebook_check_params_sessionObject

if the code is bad or not existed, check if there’s one in session, meanwhile, there the sig and access_token is correct, that means we’re in the context of canvas



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rest-core/client/facebook/rails_util.rb', line 140

def rc_facebook_check_params_session
  return if rc_facebook.authorized? || !params[:session]

  rc_facebook.parse_json!(params[:session])
  logger.debug("DEBUG: Facebook: detected session, parsed:" \
               " #{rc_facebook.data.inspect}")

  if rc_facebook.authorized?
    rc_facebook_write_fbs
  else
    logger.warn("WARN: Facebook: bad session: #{params[:session]}")
  end
end

#rc_facebook_check_params_signed_requestObject

begin facebook check ======================


121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rest-core/client/facebook/rails_util.rb', line 121

def rc_facebook_check_params_signed_request
  return if rc_facebook.authorized? || !params[:signed_request]

  rc_facebook.parse_signed_request!(params[:signed_request])
  logger.debug("DEBUG: Facebook: detected signed_request,"  \
               " parsed: #{rc_facebook.data.inspect}")

  if rc_facebook.authorized?
    rc_facebook_write_fbs
  else
    logger.warn(
      "WARN: Facebook: bad signed_request: #{params[:signed_request]}")
  end
end

#rc_facebook_check_sessionObject



207
208
209
210
211
212
213
214
215
# File 'lib/rest-core/client/facebook/rails_util.rb', line 207

def rc_facebook_check_session
  return if rc_facebook.authorized?                             ||
            !rc_options_get(RestCore::Facebook, :write_session) ||
            !(fbs = session[rc_facebook_storage_key])

  rc_facebook.parse_fbs!(fbs)
  logger.debug("DEBUG: Facebook: detected rc_facebook session, parsed:" \
               " #{rc_facebook.data.inspect}")
end

#rc_facebook_cleanupObject

begin misc ================================


258
259
260
261
262
263
# File 'lib/rest-core/client/facebook/rails_util.rb', line 258

def rc_facebook_cleanup
  cookies.delete("fbs_#{rc_facebook.app_id}")
  cookies.delete("fbsr_#{rc_facebook.app_id}")
  cookies.delete(rc_facebook_storage_key)
  session.delete(rc_facebook_storage_key)
end

#rc_facebook_filter_uri(uri) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/rest-core/client/facebook/rails_util.rb', line 281

def rc_facebook_filter_uri uri
  URI.parse(uri).tap{ |uri|
    uri.query = uri.query.split('&').reject{ |q|
                  q =~ /^(code|session|signed_request)\=/
                }.join('&') if uri.query
    uri.query = nil if uri.query.blank?
  }.to_s
rescue URI::InvalidURIError => e
  if @rc_facebook_filter_uri_retry
    raise e
  else
    @rc_facebook_filter_uri_retry = uri = URI.encode(uri)
    retry
  end
end

#rc_facebook_in_canvas?Boolean

Returns:

  • (Boolean)


297
298
299
# File 'lib/rest-core/client/facebook/rails_util.rb', line 297

def rc_facebook_in_canvas?
  !rc_options_get(RestCore::Facebook, :canvas).blank?
end

#rc_facebook_js_redirect(redirect_url, body = '') ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rest-core/client/facebook/rails_util.rb', line 86

def rc_facebook_js_redirect redirect_url, body=''
  render :inline => <<-HTML
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html>
    <head>
    <script type="text/javascript">
      window.top.location.href = '#{redirect_url}'
    </script>
    <noscript>
      <meta http-equiv="refresh" content="0;url=#{
        CGI.escapeHTML(redirect_url)}"/>
      <meta http-equiv="window-target" content="_top"/>
    </noscript>
    </head>
    <body>
      #{body}
    </bodt>
  </html>
  HTML
end

#rc_facebook_normalized_request_uriObject



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/rest-core/client/facebook/rails_util.rb', line 265

def rc_facebook_normalized_request_uri
  @rc_facebook_normalized_request_uri ||= begin
    uri = if rc_facebook_in_canvas?
            # rails 3 uses newer rack which has fullpath
            "http://apps.facebook.com/#{
              rc_options_get(RestCore::Facebook, :canvas)}" +
            (request.respond_to?(:fullpath) ?
              request.fullpath : request.request_uri)
          else
            request.url
          end

    rc_facebook_filter_uri(uri)
  end
end

#rc_facebook_on_access_token_error(error = nil) ⇒ Object



53
54
55
# File 'lib/rest-core/client/facebook/rails_util.rb', line 53

def rc_facebook_on_access_token_error error=nil
  rc_facebook_authorize(error, false)
end

#rc_facebook_setup(options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rest-core/client/facebook/rails_util.rb', line 33

def rc_facebook_setup options={}
  super

  rc_facebook_check_params_signed_request # canvas
  rc_facebook_check_params_session        # i think it would be deprecated
  rc_facebook_check_code                  # oauth api
  rc_facebook_check_fbs                   # check rc_facebook storage
  rc_facebook_check_cookie                # for js sdk (canvas or not)

  if rc_options_get(RestCore::Facebook, :ensure_authorized) &&
     !rc_facebook.authorized?

    rc_facebook_authorize('ensure authorized')
    false # action halt, redirect to do authorize,
          # eagerly, as opposed to auto_authorize
  else
    true  # keep going
  end
end

#rc_facebook_storage_keyObject

begin check ================================


188
189
190
# File 'lib/rest-core/client/facebook/rails_util.rb', line 188

def rc_facebook_storage_key
  "rc_facebook_#{rc_facebook.app_id}"
end

#rc_facebook_write_cookiesObject



248
249
250
251
252
# File 'lib/rest-core/client/facebook/rails_util.rb', line 248

def rc_facebook_write_cookies
  return if !rc_options_get(RestCore::Facebook, :write_cookies)
  cookies[rc_facebook_storage_key] = fbs = rc_facebook.fbs
  logger.debug("DEBUG: Facebook: wrote cookies: fbs => #{fbs}")
end

#rc_facebook_write_fbsObject

end check ================================
begin write ================================


228
229
230
231
232
# File 'lib/rest-core/client/facebook/rails_util.rb', line 228

def rc_facebook_write_fbs
  rc_facebook_write_handler
  rc_facebook_write_session
  rc_facebook_write_cookies
end

#rc_facebook_write_handler(handler = rc_options_get(RestCore::Facebook, :write_handler)) ⇒ Object



234
235
236
237
238
239
240
# File 'lib/rest-core/client/facebook/rails_util.rb', line 234

def rc_facebook_write_handler handler=
  rc_options_get(RestCore::Facebook, :write_handler)

  return if !handler
  handler.call(fbs = rc_facebook.fbs)
  logger.debug("DEBUG: Facebook: called write_handler: fbs => #{fbs}")
end

#rc_facebook_write_sessionObject



242
243
244
245
246
# File 'lib/rest-core/client/facebook/rails_util.rb', line 242

def rc_facebook_write_session
  return if !rc_options_get(RestCore::Facebook, :write_session)
  session[rc_facebook_storage_key] = fbs = rc_facebook.fbs
  logger.debug("DEBUG: Facebook: wrote session: fbs => #{fbs}")
end