Ensure hook names are descriptive enough.

before_options_phase
before_request_phase
before_callback_phase
This commit is contained in:
Tom Milewski 2013-09-02 21:05:18 -04:00
parent a239ba2980
commit 74bcbd7122
4 changed files with 38 additions and 39 deletions

View File

@ -34,9 +34,9 @@ module OmniAuth
:path_prefix => '/auth',
:on_failure => OmniAuth::FailureEndpoint,
:failure_raise_out_environments => ['development'],
:on_callback_hook => nil,
:on_options_hook => nil,
:on_request_hook => nil,
:before_request_phase => nil,
:before_callback_phase => nil,
:before_options_phase => nil,
:form_css => Form::DEFAULT_CSS,
:test_mode => false,
:logger => default_logger,
@ -68,31 +68,30 @@ module OmniAuth
end
end
def on_callback_hook(&block)
def before_callback_phase(&block)
if block_given?
@on_callback_hook = block
@before_callback_phase = block
else
@on_callback_hook
@before_callback_phase
end
end
def on_options_hook(&block)
def before_options_phase(&block)
if block_given?
@on_options_hook = block
@before_options_phase = block
else
@on_options_hook
@before_options_phase
end
end
def on_request_hook(&block)
def before_request_phase(&block)
if block_given?
@on_request_hook = block
@before_request_phase = block
else
@on_request_hook
@before_request_phase
end
end
def add_mock(provider, mock={})
# Stringify keys recursively one level.
mock.keys.each do |key|
@ -124,7 +123,7 @@ module OmniAuth
self.camelizations[name.to_s] = camelized.to_s
end
attr_writer :on_failure, :on_callback_hook,:on_options_hook,:on_request_hook
attr_writer :on_failure, :before_callback_phase, :before_options_phase, :before_request_phase
attr_accessor :failure_raise_out_environments, :path_prefix, :allowed_request_methods, :form_css, :test_mode, :mock_auth, :full_host, :camelizations, :logger
end

View File

@ -186,7 +186,7 @@ module OmniAuth
# Responds to an OPTIONS request.
def options_call
OmniAuth.config.on_options_hook.call(self.env) if OmniAuth.config.on_options_hook
OmniAuth.config.before_options_phase.call(self.env) if OmniAuth.config.before_options_phase
verbs = OmniAuth.config.allowed_request_methods.map(&:to_s).map(&:upcase).join(', ')
return [ 200, { 'Allow' => verbs }, [] ]
end
@ -200,7 +200,7 @@ module OmniAuth
#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 OmniAuth.config.on_request_hook
OmniAuth.config.before_request_phase.call(self.env) if OmniAuth.config.before_request_phase
if options.form.respond_to?(:call)
log :info, "Rendering form from supplied Rack endpoint."
@ -225,7 +225,7 @@ module OmniAuth
@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) if OmniAuth.config.on_callback_hook
OmniAuth.config.before_callback_phase.call(@env) if OmniAuth.config.before_callback_phase
callback_phase
end
@ -268,7 +268,7 @@ module OmniAuth
setup_phase
session['omniauth.params'] = request.params
OmniAuth.config.on_request_hook.call(self.env) if OmniAuth.config.on_request_hook
OmniAuth.config.before_request_phase.call(self.env) if OmniAuth.config.before_request_phase
if request.params['origin']
@env['rack.session']['omniauth.origin'] = request.params['origin']
elsif env['HTTP_REFERER'] && !env['HTTP_REFERER'].match(/#{request_path}$/)
@ -287,7 +287,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) if OmniAuth.config.on_callback_hook
OmniAuth.config.before_callback_phase.call(@env) if OmniAuth.config.before_callback_phase
call_app!
end
end

View File

@ -596,9 +596,9 @@ 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
it "executes callback hook on the callback phase" do
OmniAuth.config.mock_auth[:test] = {}
OmniAuth.config.on_callback_hook do |env|
OmniAuth.config.before_callback_phase do |env|
env['foobar']='baz'
end
strategy.call(make_env('/auth/test/callback', 'rack.session' => {'omniauth.origin' => 'http://example.com/origin'}))
@ -612,9 +612,9 @@ 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
it "executes request hook on the request phase" do
OmniAuth.config.mock_auth[:test] = {}
OmniAuth.config.on_request_hook do |env|
OmniAuth.config.before_request_phase do |env|
env['foobar']='baz'
end
strategy.call(make_env('/auth/test', 'QUERY_STRING' => 'foo=bar'))

View File

@ -26,20 +26,20 @@ describe OmniAuth do
end
before do
@old_path_prefix = OmniAuth.config.path_prefix
@old_on_failure = OmniAuth.config.on_failure
@old_on_callback_hook = OmniAuth.config.on_callback_hook
@old_on_options_hook = OmniAuth.config.on_options_hook
@old_on_request_hook = OmniAuth.config.on_request_hook
@old_path_prefix = OmniAuth.config.path_prefix
@old_on_failure = OmniAuth.config.on_failure
@old_before_callback_phase = OmniAuth.config.before_callback_phase
@old_before_options_phase = OmniAuth.config.before_options_phase
@old_before_request_phase = OmniAuth.config.before_request_phase
end
after do
OmniAuth.configure do |config|
config.path_prefix = @old_path_prefix
config.on_failure = @old_on_failure
config.on_callback_hook = @old_on_callback_hook
config.on_options_hook = @old_on_options_hook
config.on_request_hook = @old_on_request_hook
config.path_prefix = @old_path_prefix
config.on_failure = @old_on_failure
config.before_callback_phase = @old_before_callback_phase
config.before_options_phase = @old_before_options_phase
config.before_request_phase = @old_before_request_phase
end
end
@ -63,29 +63,29 @@ describe OmniAuth do
it "is able to set hook on option_call" do
OmniAuth.configure do |config|
config.on_options_hook do
config.before_options_phase do
'yoyo'
end
end
expect(OmniAuth.config.on_options_hook.call).to eq('yoyo')
expect(OmniAuth.config.before_options_phase.call).to eq('yoyo')
end
it "is able to set hook on request_call" do
OmniAuth.configure do |config|
config.on_request_hook do
config.before_request_phase do
'heyhey'
end
end
expect(OmniAuth.config.on_request_hook.call).to eq('heyhey')
expect(OmniAuth.config.before_request_phase.call).to eq('heyhey')
end
it "is able to set hook on callback_call" do
OmniAuth.configure do |config|
config.on_callback_hook do
config.before_callback_phase do
'heyhey'
end
end
expect(OmniAuth.config.on_callback_hook.call).to eq('heyhey')
expect(OmniAuth.config.before_callback_phase.call).to eq('heyhey')
end
describe "mock auth" do