Class: Sorcery::Providers::Facebook

Inherits:
Base
  • Object
show all
Includes:
Sorcery::Protocols::Oauth2
Defined in:
lib/sorcery/providers/facebook.rb

Overview

This class adds support for OAuth with facebook.com.

config.facebook.key = <key>
config.facebook.secret = <secret>
...

Instance Attribute Summary collapse

Attributes inherited from Base

#access_token, #callback_url, #key, #original_callback_url, #secret, #site, #state, #user_info_mapping

Instance Method Summary collapse

Methods included from Sorcery::Protocols::Oauth2

#build_client, #get_access_token, #oauth_version

Methods inherited from Base

descendants, #has_callback?, name

Constructor Details

#initializeFacebook

Returns a new instance of Facebook.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sorcery/providers/facebook.rb', line 17

def initialize
  super

  @site           = 'https://graph.facebook.com'
  @user_info_path = '/me'
  @scope          = 'email,offline_access'
  @display        = 'page'
  @token_url      = 'oauth/access_token'
  @mode           = :query
  @parse          = :query
  @param_name     = 'access_token'
end

Instance Attribute Details

#access_permissionsObject

Returns the value of attribute access_permissions.



14
15
16
# File 'lib/sorcery/providers/facebook.rb', line 14

def access_permissions
  @access_permissions
end

#displayObject

Returns the value of attribute display.



14
15
16
# File 'lib/sorcery/providers/facebook.rb', line 14

def display
  @display
end

#modeObject (readonly)

Returns the value of attribute mode.



13
14
15
# File 'lib/sorcery/providers/facebook.rb', line 13

def mode
  @mode
end

#param_nameObject (readonly)

Returns the value of attribute param_name.



13
14
15
# File 'lib/sorcery/providers/facebook.rb', line 13

def param_name
  @param_name
end

#parseObject (readonly)

Returns the value of attribute parse.



13
14
15
# File 'lib/sorcery/providers/facebook.rb', line 13

def parse
  @parse
end

#scopeObject

Returns the value of attribute scope.



14
15
16
# File 'lib/sorcery/providers/facebook.rb', line 14

def scope
  @scope
end

#token_urlObject

Returns the value of attribute token_url.



14
15
16
# File 'lib/sorcery/providers/facebook.rb', line 14

def token_url
  @token_url
end

#user_info_pathObject

Returns the value of attribute user_info_path.



14
15
16
# File 'lib/sorcery/providers/facebook.rb', line 14

def 
  @user_info_path
end

Instance Method Details

#authorize_urlObject

overrides oauth2#authorize_url to allow customized scope.



46
47
48
49
# File 'lib/sorcery/providers/facebook.rb', line 46

def authorize_url
  @scope = access_permissions.present? ? access_permissions.join(',') : scope
  super
end

#get_user_hash(access_token) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/sorcery/providers/facebook.rb', line 30

def get_user_hash(access_token)
  response = access_token.get()

  {}.tap do |h|
    h[:user_info] = JSON.parse(response.body)
    h[:uid] = h[:user_info]['id']
  end
end

#login_url(params, session) ⇒ Object

calculates and returns the url to which the user should be redirected, to get authenticated at the external provider’s site.



41
42
43
# File 'lib/sorcery/providers/facebook.rb', line 41

def (params, session)
  authorize_url
end

#process_callback(params, session) ⇒ Object

tries to login the user from access token



52
53
54
55
56
57
58
59
# File 'lib/sorcery/providers/facebook.rb', line 52

def process_callback(params, session)
  args = {}.tap do |a|
    a[:code] = params[:code] if params[:code]
  end

  get_access_token(args, token_url: token_url, mode: mode,
    param_name: param_name, parse: parse)
end