Class: FbGraph::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/fb_graph/auth.rb,
lib/fb_graph/auth/cookie.rb,
lib/fb_graph/auth/signed_request.rb

Overview

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

Defined Under Namespace

Classes: Cookie, SignedRequest, VerificationFailed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, options = {}) ⇒ Auth

Returns a new instance of Auth.



22
23
24
25
26
27
28
29
30
31
# File 'lib/fb_graph/auth.rb', line 22

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

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



20
21
22
# File 'lib/fb_graph/auth.rb', line 20

def access_token
  @access_token
end

#clientObject

Returns the value of attribute client.



20
21
22
# File 'lib/fb_graph/auth.rb', line 20

def client
  @client
end

#dataObject

Returns the value of attribute data.



20
21
22
# File 'lib/fb_graph/auth.rb', line 20

def data
  @data
end

#userObject

Returns the value of attribute user.



20
21
22
# File 'lib/fb_graph/auth.rb', line 20

def user
  @user
end

Instance Method Details

#authorized?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/fb_graph/auth.rb', line 33

def authorized?
  self.access_token.present?
end


37
38
39
40
41
42
43
# File 'lib/fb_graph/auth.rb', line 37

def from_cookie(cookie)
  data = Cookie.parse(self.client, cookie)
  self.access_token = build_access_token(data)
  self.user = User.new(data[:uid], :access_token => self.access_token)
  self.data = data
  self
end

#from_signed_request(signed_request) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/fb_graph/auth.rb', line 45

def from_signed_request(signed_request)
  data = SignedRequest.verify(self.client, signed_request)
  if data[:oauth_token]
    self.access_token = build_access_token(data)
    self.user = User.new(data[:user_id], :access_token => self.access_token)
  end
  self.data = data
  self
end