1
0
Fork 0
mirror of https://github.com/omniauth/omniauth.git synced 2022-11-09 12:31:49 -05:00

got the hooks working, but insofar as testing anything other than just creating the hooks, I'll have to get to that later

This commit is contained in:
Bill Cromie 2013-02-25 18:52:36 -05:00
parent 6ddc70b312
commit e9e8a6a0d2
4 changed files with 73 additions and 2 deletions

View file

@ -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

View file

@ -21,6 +21,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

View file

@ -186,6 +186,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
@ -196,6 +197,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
@ -218,7 +221,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'] == ''

View file

@ -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'})