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

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.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fb_graph/auth.rb', line 7

def initialize(client_id, client_secret, options = {})
  @client = Rack::OAuth2::Client.new(
    :identifier             => client_id,
    :secret                 => client_secret,
    :host                   => URI.parse(ROOT_URL).host,
    :authorization_endpoint => '/oauth/authorize',
    :token_endpoint         => '/oauth/access_token',
    :redirect_uri           => options[:redirect_uri]
  )
  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.



5
6
7
# File 'lib/fb_graph/auth.rb', line 5

def access_token
  @access_token
end

#clientObject

Returns the value of attribute client.



5
6
7
# File 'lib/fb_graph/auth.rb', line 5

def client
  @client
end

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/fb_graph/auth.rb', line 5

def data
  @data
end

#userObject

Returns the value of attribute user.



5
6
7
# File 'lib/fb_graph/auth.rb', line 5

def user
  @user
end

Instance Method Details

#authorize_uri(canvas_uri, options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/fb_graph/auth.rb', line 27

def authorize_uri(canvas_uri, options = {})
  endpoint = URI.parse SignedRequest::OAUTH_DIALOG_ENDPOINT
  params = options.merge(
    :client_id    => self.client.identifier,
    :redirect_uri => canvas_uri
  )
  params[:scope] = Array(params[:scope]).join(',') if params[:scope].present?
  endpoint.query = params.to_query
  endpoint.to_s
end

#authorized?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/fb_graph/auth.rb', line 23

def authorized?
  self.access_token.present?
end


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

def from_cookie(cookie)
  self.data = Cookie.parse(client, cookie)
  get_access_token! data[:code]
  self
end

#from_session_key(session_key) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fb_graph/auth.rb', line 53

def from_session_key(session_key)
  response = HTTPClient.new.post "#{ROOT_URL}/oauth/exchange_sessions", {:client_id => @client.identifier, :client_secret => @client.secret, :sessions => session_key}
  if response.body && self.data = JSON.parse(response.body)
    if self.data[0]
      self.access_token = build_access_token(self.data[0].with_indifferent_access)
    else
      # If the session key is unknown or there's an error, Facebook returns null
      self.access_token = nil
    end
  end
  self
end

#from_signed_request(signed_request) ⇒ Object



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

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