From 67d4ccc04d7ce80666c5f32ecfe7799aa51351f2 Mon Sep 17 00:00:00 2001 From: Bill Cromie Date: Mon, 25 Feb 2013 18:52:36 -0500 Subject: [PATCH 1/3] got the hooks working, but insofar as testing anything other than just creating the hooks, I'll have to get to that later --- lib/omniauth.rb | 30 +++++++++++++++++++++++++++++- lib/omniauth/builder.rb | 12 ++++++++++++ lib/omniauth/strategy.rb | 5 ++++- spec/omniauth_spec.rb | 28 ++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) diff --git a/lib/omniauth.rb b/lib/omniauth.rb index c8283b6..43a8f71 100644 --- a/lib/omniauth.rb +++ b/lib/omniauth.rb @@ -33,6 +33,9 @@ module OmniAuth :camelizations => {}, :path_prefix => '/auth', :on_failure => OmniAuth::FailureEndpoint, + :on_callback_hook =>Proc.new {|p|}, + :on_options_hook=>Proc.new {|p| }, + :on_request_hook=>Proc.new {|p| }, :form_css => Form::DEFAULT_CSS, :test_mode => false, :logger => default_logger, @@ -64,6 +67,31 @@ module OmniAuth end end + def on_callback_hook(&block) + if block_given? + @on_callback_hook = block + else + @on_callback_hook + end + end + + def on_options_hook(&block) + if block_given? + @on_options_hook = block + else + @on_options_hook + end + end + + def on_request_hook(&block) + if block_given? + @on_request_hook = block + else + @on_request_hook + end + end + + def add_mock(provider, mock={}) # Stringify keys recursively one level. mock.keys.each do |key| @@ -95,7 +123,7 @@ module OmniAuth self.camelizations[name.to_s] = camelized.to_s end - attr_writer :on_failure + attr_writer :on_failure, :on_callback_hook,:on_options_hook,:on_request_hook attr_accessor :path_prefix, :allowed_request_methods, :form_css, :test_mode, :mock_auth, :full_host, :camelizations, :logger end diff --git a/lib/omniauth/builder.rb b/lib/omniauth/builder.rb index 6f077a6..3730414 100644 --- a/lib/omniauth/builder.rb +++ b/lib/omniauth/builder.rb @@ -20,6 +20,18 @@ module OmniAuth OmniAuth.config.on_failure = block end + def on_options_hook(&block) + OmniAuth.config.on_options_hook = block + end + + def on_request_hook(&block) + OmniAuth.config.on_request_hook = block + end + + def on_callback_hook(&block) + OmniAuth.config.on_callback_hook = block + end + def configure(&block) OmniAuth.configure(&block) end diff --git a/lib/omniauth/strategy.rb b/lib/omniauth/strategy.rb index c144556..11cb9af 100644 --- a/lib/omniauth/strategy.rb +++ b/lib/omniauth/strategy.rb @@ -179,6 +179,7 @@ module OmniAuth # Responds to an OPTIONS request. def options_call + OmniAuth.config.on_options_hook.call(self.env) verbs = OmniAuth.config.allowed_request_methods.map(&:to_s).map(&:upcase).join(', ') return [ 200, { 'Allow' => verbs }, [] ] end @@ -189,6 +190,8 @@ module OmniAuth log :info, "Request phase initiated." + OmniAuth.config.on_request_hook.call(self.env) + #store query params from the request url, extracted in the callback_phase session['omniauth.params'] = request.params @@ -211,7 +214,7 @@ module OmniAuth # Performs the steps necessary to run the callback phase of a strategy. def callback_call setup_phase - + OmniAuth.config.on_callback_hook.call(self.env) log :info, "Callback phase initiated." @env['omniauth.origin'] = session.delete('omniauth.origin') @env['omniauth.origin'] = nil if env['omniauth.origin'] == '' diff --git a/spec/omniauth_spec.rb b/spec/omniauth_spec.rb index cabb73b..270f3ce 100644 --- a/spec/omniauth_spec.rb +++ b/spec/omniauth_spec.rb @@ -54,6 +54,34 @@ describe OmniAuth do expect(OmniAuth.config.on_failure.call).to eq('yoyo') end + + it "is able to set hook on option_call" do + OmniAuth.configure do |config| + config.on_options_hook do + 'yoyo' + end + end + expect(OmniAuth.config.on_options_hook.call).to eq('yoyo') + end + + it "is able to set hook on request_call" do + OmniAuth.configure do |config| + config.on_request_hook do + 'heyhey' + end + end + expect(OmniAuth.config.on_request_hook.call).to eq('heyhey') + end + + it "is able to set hook on callback_call" do + OmniAuth.configure do |config| + config.on_callback_hook do + 'heyhey' + end + end + expect(OmniAuth.config.on_callback_hook.call).to eq('heyhey') + end + describe "mock auth" do before do OmniAuth.config.add_mock(:facebook, :uid => '12345',:info=>{:name=>'Joe', :email=>'joe@example.com'}) From bad6263fec6006b30842e48556578cf84fe4a87a Mon Sep 17 00:00:00 2001 From: Bill Cromie Date: Tue, 26 Feb 2013 12:58:13 -0500 Subject: [PATCH 2/3] adding the hooks to mock request call and mock callback call --- lib/omniauth/strategy.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/omniauth/strategy.rb b/lib/omniauth/strategy.rb index 11cb9af..bc5fe07 100644 --- a/lib/omniauth/strategy.rb +++ b/lib/omniauth/strategy.rb @@ -190,11 +190,11 @@ module OmniAuth log :info, "Request phase initiated." - OmniAuth.config.on_request_hook.call(self.env) - #store query params from the request url, extracted in the callback_phase session['omniauth.params'] = request.params + OmniAuth.config.on_request_hook.call(self.env) + if options.form.respond_to?(:call) log :info, "Rendering form from supplied Rack endpoint." options.form.call(env) @@ -214,11 +214,11 @@ module OmniAuth # Performs the steps necessary to run the callback phase of a strategy. def callback_call setup_phase - OmniAuth.config.on_callback_hook.call(self.env) log :info, "Callback phase initiated." @env['omniauth.origin'] = session.delete('omniauth.origin') @env['omniauth.origin'] = nil if env['omniauth.origin'] == '' @env['omniauth.params'] = session.delete('omniauth.params') || {} + OmniAuth.config.on_callback_hook.call(@env) callback_phase end @@ -265,7 +265,7 @@ module OmniAuth setup_phase session['omniauth.params'] = request.params - + OmniAuth.config.on_request_hook.call(self.env) if request.params['origin'] @env['rack.session']['omniauth.origin'] = request.params['origin'] elsif env['HTTP_REFERER'] && !env['HTTP_REFERER'].match(/#{request_path}$/) @@ -284,6 +284,7 @@ module OmniAuth @env['omniauth.params'] = session.delete('omniauth.params') || {} @env['omniauth.origin'] = session.delete('omniauth.origin') @env['omniauth.origin'] = nil if env['omniauth.origin'] == '' + OmniAuth.config.on_callback_hook.call(@env) call_app! end end From 336ea4ec7fe51f39f037801bb763ac820d46cf4c Mon Sep 17 00:00:00 2001 From: Bill Cromie Date: Fri, 1 Mar 2013 13:12:09 -0500 Subject: [PATCH 3/3] adding specs for the hooks to ensure that they are running --- spec/omniauth/strategy_spec.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/spec/omniauth/strategy_spec.rb b/spec/omniauth/strategy_spec.rb index c4e91c3..f7f2f36 100644 --- a/spec/omniauth/strategy_spec.rb +++ b/spec/omniauth/strategy_spec.rb @@ -572,6 +572,16 @@ describe OmniAuth::Strategy do expect(strategy.env['omniauth.origin']).to eq('http://example.com/origin') end + it "executes callback_hook on the callback phase" do + OmniAuth.config.mock_auth[:test] = {} + OmniAuth.config.on_callback_hook do |env| + env['foobar']='baz' + end + strategy.call(make_env('/auth/test/callback', 'rack.session' => {'omniauth.origin' => 'http://example.com/origin'})) + expect(strategy.env['foobar']).to eq('baz') + end + + it "sets omniauth.params on the request phase" do OmniAuth.config.mock_auth[:test] = {} @@ -579,6 +589,15 @@ describe OmniAuth::Strategy do expect(strategy.env['rack.session']['omniauth.params']).to eq({'foo' => 'bar'}) end + it "executes request_hook on the request phase" do + OmniAuth.config.mock_auth[:test] = {} + OmniAuth.config.on_request_hook do |env| + env['foobar']='baz' + end + strategy.call(make_env('/auth/test', 'QUERY_STRING' => 'foo=bar')) + expect(strategy.env['foobar']).to eq('baz') + end + it "turns omniauth.params into an env variable on the callback phase" do OmniAuth.config.mock_auth[:test] = {}