Class: OmniAuth::Strategies::Facebook

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

Defined Under Namespace

Classes: NoAuthorizationCodeError

Constant Summary collapse

DEFAULT_SCOPE =
'email'

Instance Method Summary collapse

Instance Method Details

#access_token_optionsObject



110
111
112
# File 'lib/omniauth/strategies/facebook.rb', line 110

def access_token_options
  options.access_token_options.inject({}) { |h,(k,v)| h[k.to_sym] = v; h }
end

#authorize_paramsObject

You can pass display, state or scope params to the auth request, if you need to set them dynamically. You can also set these options in the OmniAuth config :authorize_params option.

/auth/facebook?display=popup&state=ABC



121
122
123
124
125
126
# File 'lib/omniauth/strategies/facebook.rb', line 121

def authorize_params
  super.tap do |params|
    %w[display state scope].each { |v| params[v.to_sym] = request.params[v] if request.params[v] }
    params[:scope] ||= DEFAULT_SCOPE
  end
end

#build_access_tokenObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/omniauth/strategies/facebook.rb', line 59

def build_access_token
  if access_token = request.params["access_token"]
    ::OAuth2::AccessToken.from_hash(
      client, 
      {"access_token" => access_token}.update(access_token_options)
    )
  elsif signed_request_contains_access_token?
    hash = signed_request.clone
    ::OAuth2::AccessToken.new(
      client,
      hash.delete('oauth_token'),
      hash.merge!(access_token_options.merge(:expires_at => hash.delete('expires')))
    )
  else
    with_authorization_code! { super }.tap do |token|
      token.options.merge!(access_token_options)
    end
  end
end

#callback_urlObject

NOTE if we’re using code from the signed request then FB sets the redirect_uri to ” during the authorize phase + it must match during the access_token phase: github.com/facebook/php-sdk/blob/master/src/base_facebook.php#L348



102
103
104
105
106
107
108
# File 'lib/omniauth/strategies/facebook.rb', line 102

def callback_url
  if @authorization_code_from_signed_request
    ''
  else
    options[:callback_url] || super
  end
end

#raw_infoObject



55
56
57
# File 'lib/omniauth/strategies/facebook.rb', line 55

def raw_info
  @raw_info ||= access_token.get('/me').parsed || {}
end

#request_phaseObject



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

def request_phase
  if signed_request_contains_access_token?
    # if we already have an access token, we can just hit the
    # callback URL directly and pass the signed request along
    params = { :signed_request => raw_signed_request }
    params[:state] = request.params['state'] if request.params['state']
    query = Rack::Utils.build_query(params)

    url = callback_url
    url << "?" unless url.match(/\?/)
    url << "&" unless url.match(/[\&\?]$/)
    url << query

    redirect url
  else
    super
  end
end

#signed_requestObject

Parse signed request in order, from:

  1. the request ‘signed_request’ param (server-side flow from canvas pages) or

  2. a cookie (client-side flow via JS SDK)



134
135
136
137
# File 'lib/omniauth/strategies/facebook.rb', line 134

def signed_request
  @signed_request ||= raw_signed_request &&
    parse_signed_request(raw_signed_request)
end