From db7f116a7a3a6718a8324e7bf5e5bcf0928c6d5a Mon Sep 17 00:00:00 2001 From: Michael Bleigh Date: Wed, 18 Mar 2009 16:28:54 -0400 Subject: [PATCH] Added support for custom callbacks via YML config. --- .gitignore | 1 + app/controllers/sessions_controller.rb | 5 ++++- app/models/twitter_auth/basic_user.rb | 2 +- lib/twitter_auth.rb | 11 ++++++++++- spec/controllers/sessions_controller_spec.rb | 8 ++++++++ spec/fixtures/config/twitter_auth.yml | 1 + 6 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1e32bed --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/spec/debug.log diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 3f63e38..991eff7 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -4,7 +4,10 @@ def new @request_token = TwitterAuth.consumer.get_request_token session[:request_token] = @request_token.token session[:request_token_secret] = @request_token.secret - redirect_to @request_token.authorize_url + + url = @request_token.authorize_url + url << "&oauth_callback=#{CGI.escape(TwitterAuth.oauth_callback)}" if TwitterAuth.oauth_callback? + redirect_to url end end diff --git a/app/models/twitter_auth/basic_user.rb b/app/models/twitter_auth/basic_user.rb index 15b29ef..c474ae5 100644 --- a/app/models/twitter_auth/basic_user.rb +++ b/app/models/twitter_auth/basic_user.rb @@ -1,6 +1,6 @@ module TwitterAuth class BasicUser < TwitterAuth::GenericUser - + attr_protected :cryped_password, :salt end end diff --git a/lib/twitter_auth.rb b/lib/twitter_auth.rb index 885364f..76655b7 100644 --- a/lib/twitter_auth.rb +++ b/lib/twitter_auth.rb @@ -3,7 +3,16 @@ module TwitterAuth self.base_url = 'https://twitter.com' def self.config(environment=RAILS_ENV) - YAML.load(File.open(RAILS_ROOT + '/config/twitter_auth.yml').read)[environment] + @config ||= {} + @config[environment] ||= YAML.load(File.open(RAILS_ROOT + '/config/twitter_auth.yml').read)[environment] + end + + def self.oauth_callback? + config.key?('oauth_callback') + end + + def self.oauth_callback + config['oauth_callback'] end # The authentication strategy employed by this diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb index 72c6026..c68223d 100644 --- a/spec/controllers/sessions_controller_spec.rb +++ b/spec/controllers/sessions_controller_spec.rb @@ -37,6 +37,14 @@ get :new response.should redirect_to('https://twitter.com/oauth/authorize?oauth_token=faketoken') end + + it 'should redirect to the oauth_callback if one is specified' do + TwitterAuth.stub!(:oauth_callback).and_return('http://localhost:3000/development') + TwitterAuth.stub!(:oauth_callback?).and_return(true) + + get :new + response.should redirect_to('https://twitter.com/oauth/authorize?oauth_token=faketoken&oauth_callback=' + CGI.escape(TwitterAuth.oauth_callback)) + end end describe '#oauth_callback' do diff --git a/spec/fixtures/config/twitter_auth.yml b/spec/fixtures/config/twitter_auth.yml index eb944b0..2001b76 100644 --- a/spec/fixtures/config/twitter_auth.yml +++ b/spec/fixtures/config/twitter_auth.yml @@ -2,6 +2,7 @@ development: strategy: oauth oauth_consumer_key: devkey oauth_consumer_secret: devsecret + oauth_callback: "http://localhost:3000" test: strategy: oauth oauth_consumer_key: testkey