diff --git a/lib/omniauth/strategies/facebook.rb b/lib/omniauth/strategies/facebook.rb index f1337ab..0b9e24d 100644 --- a/lib/omniauth/strategies/facebook.rb +++ b/lib/omniauth/strategies/facebook.rb @@ -3,7 +3,29 @@ require 'omniauth/strategies/oauth2' module OmniAuth module Strategies class Facebook < OmniAuth::Strategies::OAuth2 - include OmniAuth::Strategy + option :client_options, { + :site => 'https://graph.facebook.com', + :token_url => '/oauth/access_token' + } + + option :token_params, { + :parse => :query + } + + option :access_token_options, { + :header_format => 'OAuth %s', + :param_name => 'access_token' + } + + def build_access_token + super.tap do |token| + token.options.merge!(access_token_options) + end + end + + def access_token_options + options.access_token_options.inject({}) { |h,(k,v)| h[k.to_sym] = v; h } + end end end end diff --git a/spec/omniauth/strategies/facebook_spec.rb b/spec/omniauth/strategies/facebook_spec.rb index 66277e7..5e6f1e9 100644 --- a/spec/omniauth/strategies/facebook_spec.rb +++ b/spec/omniauth/strategies/facebook_spec.rb @@ -2,4 +2,45 @@ require 'spec_helper' require 'omniauth-facebook' describe OmniAuth::Strategies::Facebook do + subject do + OmniAuth::Strategies::Facebook.new(nil, @options || {}) + end + + it_should_behave_like 'an oauth2 strategy' + + describe '#client' do + it 'has correct Facebook site' do + subject.client.site.should eq('https://graph.facebook.com') + end + + it 'has correct authorize url' do + subject.client.options[:authorize_url].should eq('/oauth/authorize') + end + + it 'has correct token url' do + subject.client.options[:token_url].should eq('/oauth/access_token') + end + end + + describe '#authorize_params' do + it 'is empty by default' do + subject.authorize_params.should be_empty + end + end + + describe '#token_params' do + it 'has correct parse strategy' do + subject.token_params[:parse].should eq(:query) + end + end + + describe '#access_token_options' do + it 'has correct param name by default' do + subject.access_token_options[:param_name].should eq('access_token') + end + + it 'has correct header format by default' do + subject.access_token_options[:header_format].should eq('OAuth %s') + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b919247..d0d2710 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,6 @@ require 'bundler/setup' require 'rspec' +Dir[File.expand_path('../support/**/*', __FILE__)].each { |f| require f } RSpec.configure do |config| end diff --git a/spec/support/shared_examples.rb b/spec/support/shared_examples.rb new file mode 100644 index 0000000..8a39760 --- /dev/null +++ b/spec/support/shared_examples.rb @@ -0,0 +1,36 @@ +shared_examples 'an oauth2 strategy' do + describe '#client' do + it 'should be initialized with symbolized client_options' do + @options = { :client_options => { 'authorize_url' => 'https://example.com' } } + subject.client.options[:authorize_url].should == 'https://example.com' + end + end + + describe '#authorize_params' do + it 'should include any authorize params passed in the :authorize_params option' do + @options = { :authorize_params => { :foo => 'bar', :baz => 'zip' } } + subject.authorize_params['foo'].should eq('bar') + subject.authorize_params['baz'].should eq('zip') + end + + it 'should include top-level options that are marked as :authorize_options' do + @options = { :authorize_options => [:scope, :foo], :scope => 'bar', :foo => 'baz' } + subject.authorize_params['scope'].should eq('bar') + subject.authorize_params['foo'].should eq('baz') + end + end + + describe '#token_params' do + it 'should include any authorize params passed in the :authorize_params option' do + @options = { :token_params => { :foo => 'bar', :baz => 'zip' } } + subject.token_params['foo'].should eq('bar') + subject.token_params['baz'].should eq('zip') + end + + it 'should include top-level options that are marked as :authorize_options' do + @options = { :token_options => [:scope, :foo], :scope => 'bar', :foo => 'baz' } + subject.token_params['scope'].should eq('bar') + subject.token_params['foo'].should eq('baz') + end + end +end