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    => 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

#exchange_token!(access_token) ⇒ Object



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

def exchange_token!(access_token)
  raise Unauthorized.new('No Access Token') unless access_token
  client.fb_exchange_token = access_token
  self.access_token = client.access_token! :client_auth_body
  self
rescue Rack::OAuth2::Client::Error => e
  Exception.handle_response e.status, e.message
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_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