Sha256: c31799953b319aa92af8711e55fe0539e4a04cca00105805a473e01f3cecc739

Contents?: true

Size: 1.35 KB

Versions: 16

Compression:

Stored size: 1.35 KB

Contents

module FbGraph
  # = Parse & verify facebook auth cookie
  # 
  # Used with Facebook JavaScript SDK
  # 
  #   app = FbGraph::Auth.new(APP_ID, APP_SECRET)
  #   app.from_cookie(cookie_hash)
  #   auth.access_token
  #   # => OAuth2::AccessToken (not String!)
  #   auth.user # only initialized
  #   auth.user.fetch # fetch whole profile
  # 
  # This method is called automatically if cookie is given when initializing
  # 
  #   auth = FbGraph::Auth.new(APP_ID, APP_SECRET, :cookie => {..})
  #   auth.access_token # already parsed
  class Auth
    class VerificationFailed < Exception; end

    attr_accessor :client, :access_token, :user

    def initialize(client_id, client_secret, options = {})
      @client = OAuth2::Client.new(client_id, client_secret, options.merge(
        :site => ROOT_URL
      ))
      if options[:cookie].present?
        from_cookie(options[:cookie])
      end
    end

    def from_cookie(cookie)
      cookie = Auth::Cookie.parse(self.client, cookie)
      expires_in = unless cookie[:expires].zero?
        cookie[:expires] - Time.now.to_i
      end
      self.access_token = OAuth2::AccessToken.new(
        self.client,
        cookie[:access_token],
        cookie[:refresh_token],
        expires_in
      )
      self.user = User.new(cookie[:uid], :access_token => self.access_token)
      self
    end
  end
end

require 'fb_graph/auth/cookie'

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
fb_graph-1.4.1 lib/fb_graph/auth.rb
fb_graph-1.4.0 lib/fb_graph/auth.rb
fb_graph-1.3.9 lib/fb_graph/auth.rb
fb_graph-1.3.8 lib/fb_graph/auth.rb
fb_graph-1.3.7 lib/fb_graph/auth.rb
fb_graph-1.3.6 lib/fb_graph/auth.rb
fb_graph-1.3.5 lib/fb_graph/auth.rb
fb_graph-1.3.4 lib/fb_graph/auth.rb
fb_graph-1.3.3 lib/fb_graph/auth.rb
fb_graph-1.3.2 lib/fb_graph/auth.rb
fb_graph-1.3.1 lib/fb_graph/auth.rb
fb_graph-1.3.0 lib/fb_graph/auth.rb
fb_graph-1.2.5 lib/fb_graph/auth.rb
fb_graph-1.2.4 lib/fb_graph/auth.rb
fb_graph-1.2.3 lib/fb_graph/auth.rb
fb_graph-1.2.2 lib/fb_graph/auth.rb