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

#auth_hash, descendants, #has_callback?, name

Constructor Details

#initializeFacebook

Returns a new instance of Facebook.



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

def initialize
  super

  @site           = 'https://graph.facebook.com'
  @auth_site      = 'https://www.facebook.com'
  @user_info_path = 'me'
  @scope          = 'email'
  @display        = 'page'
  @token_url      = 'oauth/access_token'
  @auth_path      = 'dialog/oauth'
  @mode           = :query
  @parse          = :json
  @param_name     = 'access_token'
end

Instance Attribute Details

#access_permissionsObject

Returns the value of attribute access_permissions.



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

def access_permissions
  @access_permissions
end

#api_versionObject

Returns the value of attribute api_version.



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

def api_version
  @api_version
end

#auth_pathObject

Returns the value of attribute auth_path.



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

def auth_path
  @auth_path
end

#displayObject

Returns the value of attribute display.



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

def display
  @display
end

#modeObject (readonly)

Returns the value of attribute mode.



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

def mode
  @mode
end

#param_nameObject (readonly)

Returns the value of attribute param_name.



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

def param_name
  @param_name
end

#parseObject

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.



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

def scope
  @scope
end

#token_urlObject

Returns the value of attribute token_url.



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

def token_url
  @token_url
end

#user_info_pathObject

Returns the value of attribute user_info_path.



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

def 
  @user_info_path
end

Instance Method Details

#authorize_urlObject

overrides oauth2#authorize_url to allow customized scope.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sorcery/providers/facebook.rb', line 47

def authorize_url
  # Fix: replace default oauth2 options, specially to prevent the Faraday gem which
  # concatenates with "/", removing the Facebook api version
  options = {
    site:          File.join(@site, api_version.to_s),
    authorize_url: File.join(@auth_site, api_version.to_s, auth_path),
    token_url:     token_url
  }

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

#get_user_hash(access_token) ⇒ Object



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

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

  auth_hash(access_token).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.



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

def (_params, _session)
  authorize_url
end

#process_callback(params, _session) ⇒ Object

tries to login the user from access token



61
62
63
64
65
66
67
68
# File 'lib/sorcery/providers/facebook.rb', line 61

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