Class: Koala::Facebook::OAuth
- Inherits:
-
Object
- Object
- Koala::Facebook::OAuth
- Defined in:
- lib/koala.rb
Instance Attribute Summary collapse
-
#app_id ⇒ Object
Returns the value of attribute app_id.
-
#app_secret ⇒ Object
Returns the value of attribute app_secret.
-
#oauth_callback_url ⇒ Object
Returns the value of attribute oauth_callback_url.
Instance Method Summary collapse
- #fetch_token_string(code) ⇒ Object
- #get_access_token(code) ⇒ Object
- #get_user_from_cookie(cookie_hash) ⇒ Object (also: #get_user_from_cookies)
-
#initialize(app_id, app_secret, oauth_callback_url = nil) ⇒ OAuth
constructor
A new instance of OAuth.
- #parse_access_token(response_text) ⇒ Object
- #url_for_access_token(code, options = {}) ⇒ Object
- #url_for_oauth_code(options = {}) ⇒ Object
Constructor Details
#initialize(app_id, app_secret, oauth_callback_url = nil) ⇒ OAuth
Returns a new instance of OAuth.
216 217 218 219 220 |
# File 'lib/koala.rb', line 216 def initialize(app_id, app_secret, oauth_callback_url = nil) @app_id = app_id @app_secret = app_secret @oauth_callback_url = oauth_callback_url end |
Instance Attribute Details
#app_id ⇒ Object
Returns the value of attribute app_id.
215 216 217 |
# File 'lib/koala.rb', line 215 def app_id @app_id end |
#app_secret ⇒ Object
Returns the value of attribute app_secret.
215 216 217 |
# File 'lib/koala.rb', line 215 def app_secret @app_secret end |
#oauth_callback_url ⇒ Object
Returns the value of attribute oauth_callback_url.
215 216 217 |
# File 'lib/koala.rb', line 215 def oauth_callback_url @oauth_callback_url end |
Instance Method Details
#fetch_token_string(code) ⇒ Object
284 285 286 287 288 289 290 291 |
# File 'lib/koala.rb', line 284 def fetch_token_string(code) Koala.make_request("oauth/access_token", { :client_id => @app_id, :redirect_uri => @oauth_callback_url, :client_secret => @app_secret, :code => code }, "get") end |
#get_access_token(code) ⇒ Object
293 294 295 296 297 298 299 300 |
# File 'lib/koala.rb', line 293 def get_access_token(code) result = fetch_token_string(code) # if we have an error, parse the error JSON and raise an error raise GraphAPIError.new((JSON.parse(result)["error"] rescue nil) || {}) if result =~ /error/ # otherwise, parse the access token parse_access_token(result) end |
#get_user_from_cookie(cookie_hash) ⇒ Object Also known as:
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/koala.rb', line 222 def () # Parses the cookie set by the official Facebook JavaScript SDK. # # cookies should be a dictionary-like object mapping cookie names to # cookie values. # # If the user is logged in via Facebook, we return a dictionary with the # keys "uid" and "access_token". The former is the user's Facebook ID, # and the latter can be used to make authenticated requests to the Graph API. # If the user is not logged in, we return None. # # Download the official Facebook JavaScript SDK at # http://github.com/facebook/connect-js/. Read more about Facebook # authentication at http://developers.facebook.com/docs/authentication/. if = ["fbs_" + @app_id.to_s] # remove the opening/closing quote = .gsub(/\"/, "") # since we no longer get individual cookies, we have to separate out the components ourselves components = {} .split("&").map {|param| param = param.split("="); components[param[0]] = param[1]} # generate the signature and make sure it matches what we expect auth_string = components.keys.sort.collect {|a| a == "sig" ? nil : "#{a}=#{components[a]}"}.reject {|a| a.nil?}.join("") sig = Digest::MD5.hexdigest(auth_string + @app_secret) sig == components["sig"] && (components["expires"] == "0" || Time.now.to_i < components["expires"].to_i) ? components : nil end end |
#parse_access_token(response_text) ⇒ Object
276 277 278 279 280 281 282 |
# File 'lib/koala.rb', line 276 def parse_access_token(response_text) components = response_text.split("&").inject({}) do |hash, bit| key, value = bit.split("=") hash.merge!(key => value) end components end |
#url_for_access_token(code, options = {}) ⇒ Object
265 266 267 268 269 270 271 272 273 274 |
# File 'lib/koala.rb', line 265 def url_for_access_token(code, = {}) # Creates the URL for the token corresponding to a given code generated by Facebook if .is_a?(String) # changing the arguments puts "Deprecation warning: url_for_access_token now takes an options hash as the second argument; pass the callback as :callback." = {:callback => } end callback = [:callback] || @oauth_callback_url raise ArgumentError, "url_for_access_token must get a callback either from the OAuth object or in the parameters!" unless callback "https://#{GRAPH_SERVER}/oauth/access_token?client_id=#{@app_id}&redirect_uri=#{callback}&client_secret=#{@app_secret}&code=#{code}" end |
#url_for_oauth_code(options = {}) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/koala.rb', line 253 def url_for_oauth_code( = {}) # for permissions, see http://developers.facebook.com/docs/authentication/permissions = [:permissions] scope = ? "&scope=#{.is_a?(Array) ? .join(",") : }" : "" callback = [:callback] || @oauth_callback_url raise ArgumentError, "url_for_oauth_code must get a callback either from the OAuth object or in the options!" unless callback # Creates the URL for oauth authorization for a given callback and optional set of permissions "https://#{GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{callback}#{scope}" end |