Class: OmniAuth::Strategies::Facebook

Inherits:
OAuth2
  • Object
show all
Defined in:
lib/omniauth/strategies/oauth2/facebook.rb

Overview

Authenticate to Facebook utilizing OAuth 2.0 and retrieve basic user information.

Examples:

Basic Usage

use OmniAuth::Strategies::Facebook, 'client_id', 'client_secret'

Instance Attribute Summary

Attributes inherited from OAuth2

#client_id, #client_options, #client_secret, #options

Instance Method Summary collapse

Methods inherited from OAuth2

#callback_url, #client

Constructor Details

#initialize(app, client_id = nil, client_secret = nil, options = {}, &block) ⇒ Facebook

Returns a new instance of Facebook.

Parameters:

  • app (Rack Application)

    standard middleware application parameter

  • client_id (String) (defaults to: nil)

    the application id as registered on Facebook

  • client_secret (String) (defaults to: nil)

    the application secret as registered on Facebook

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :scope (String) — default: 'email,offline_access'

    comma-separated extended permissions such as email and manage_pages



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/omniauth/strategies/oauth2/facebook.rb', line 16

def initialize(app, client_id=nil, client_secret=nil, options = {}, &block)
  client_options = {
    :site => 'https://graph.facebook.com/',
    :token_url => '/oauth/access_token'
  }

  options = {
    :parse => :query
  }.merge(options)

  super(app, :facebook, client_id, client_secret, client_options, options, &block)
end

Instance Method Details

#auth_hashObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/omniauth/strategies/oauth2/facebook.rb', line 29

def auth_hash
  OmniAuth::Utils.deep_merge(
    super, {
      'uid' => user_data['id'],
      'user_info' => ,
      'extra' => {
        'user_hash' => user_data,
      },
    }
  )
end

#build_access_tokenObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/omniauth/strategies/oauth2/facebook.rb', line 52

def build_access_token
  if !signed_request.nil? && !signed_request.empty?
    verifier = signed_request['code']
    client.auth_code.get_token(verifier, {:redirect_uri => ''}.merge(options))
  elsif !facebook_session.nil? && !facebook_session.empty?
    @access_token = ::OAuth2::AccessToken.new(client, facebook_session['access_token'], {:mode => :query, :param_name => 'access_token'})
  else
    super
  end
end

#facebook_sessionObject



63
64
65
66
67
68
69
70
# File 'lib/omniauth/strategies/oauth2/facebook.rb', line 63

def facebook_session
  session_cookie = request.cookies["fbs_#{client.id}"]
  if session_cookie
    @facebook_session ||= Rack::Utils.parse_query(request.cookies["fbs_#{client.id}"].gsub('"', ''))
  else
    nil
  end
end

#request_phaseObject



47
48
49
50
# File 'lib/omniauth/strategies/oauth2/facebook.rb', line 47

def request_phase
  options[:scope] ||= 'email,offline_access'
  super
end

#signed_requestObject



72
73
74
75
76
77
78
79
# File 'lib/omniauth/strategies/oauth2/facebook.rb', line 72

def signed_request
  signed_request_cookie = request.cookies["fbsr_#{client.id}"]
  if signed_request_cookie
    signed_request = parse_signed_request(signed_request_cookie)
  else
    nil
  end
end

#user_dataObject



41
42
43
44
45
# File 'lib/omniauth/strategies/oauth2/facebook.rb', line 41

def user_data
  @access_token.options[:mode] = :query
  @access_token.options[:param_name] = 'access_token'
  @data ||= @access_token.get('/me').parsed
end

#user_infoObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/omniauth/strategies/oauth2/facebook.rb', line 81

def 
  {
    'nickname' => user_data['username'],
    'email' => (user_data['email'] if user_data['email']),
    'first_name' => user_data['first_name'],
    'last_name' => user_data['last_name'],
    'name' => "#{user_data['first_name']} #{user_data['last_name']}",
    'image' => "http://graph.facebook.com/#{user_data['id']}/picture?type=square",
    'urls' => {
      'Facebook' => user_data['link'],
      'Website' => user_data['website'],
    },
  }
end