Skip to content

Commit

Permalink
Add better fail handling for exceptions, add configure and on_failure…
Browse files Browse the repository at this point in the history
… methods to Builder.
  • Loading branch information
Michael Bleigh committed Oct 8, 2010
1 parent 9164a73 commit 5e8eb28
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions oa-basic/lib/omniauth/strategies/http_basic.rb
Expand Up @@ -40,8 +40,8 @@ def perform
@env['PATH_INFO'] = "#{OmniAuth.config.path_prefix}/#{name}/callback"

@app.call(@env)
rescue RestClient::Request::Unauthorized
fail!(:invalid_credentials)
rescue RestClient::Request::Unauthorized => e
fail!(:invalid_credentials, e)
end

def perform_authentication(uri, headers = request_headers)
Expand Down
8 changes: 8 additions & 0 deletions oa-core/lib/omniauth/builder.rb
Expand Up @@ -7,6 +7,14 @@ def initialize(app, &block)
super(&block)
end

def on_failure(&block)
OmniAuth.config.on_failure = block
end

def configure(&block)
OmniAuth.configure(&block)
end

def provider(klass, *args, &block)
if klass.is_a?(Class)
middleware = klass
Expand Down
3 changes: 2 additions & 1 deletion oa-core/lib/omniauth/strategy.rb
Expand Up @@ -77,7 +77,8 @@ def redirect(uri)

def user_info; {} end

def fail!(message_key)
def fail!(message_key, exception = nil)
self.env['rack.auth.error'] = exception
OmniAuth.config.on_failure.call(self.env, message_key.to_sym)
end
end
Expand Down
3 changes: 1 addition & 2 deletions oa-enterprise/lib/omniauth/strategies/ldap.rb
Expand Up @@ -50,8 +50,7 @@ def perform

@app.call(@env)
rescue Exception => e
puts e.message
fail!(:invalid_credentials)
fail!(:invalid_credentials, e)
end
end

Expand Down
4 changes: 2 additions & 2 deletions oa-oauth/lib/omniauth/strategies/oauth.rb
Expand Up @@ -24,8 +24,8 @@ def callback_phase
request_token = ::OAuth::RequestToken.new(consumer, session[:oauth][name.to_sym].delete(:request_token), session[:oauth][name.to_sym].delete(:request_secret))
@access_token = request_token.get_access_token(:oauth_verifier => request.params['oauth_verifier'])
super
rescue ::OAuth::Unauthorized
fail!(:invalid_credentials)
rescue ::OAuth::Unauthorized => e
fail!(:invalid_credentials, e)
end

def auth_hash
Expand Down
4 changes: 2 additions & 2 deletions oa-oauth/lib/omniauth/strategies/oauth2.rb
Expand Up @@ -26,8 +26,8 @@ def callback_phase
verifier = request.params['code']
@access_token = client.web_server.get_access_token(verifier, :redirect_uri => callback_url)
super
rescue ::OAuth2::HTTPError => e
fail!(:invalid_credentials)
rescue ::OAuth2::HTTPError, ::OAuth2::AccessDenied => e
fail!(:invalid_credentials, e)
end

def auth_hash
Expand Down
3 changes: 2 additions & 1 deletion oa-oauth/lib/omniauth/strategies/twitter.rb
Expand Up @@ -14,7 +14,8 @@ module Strategies
class Twitter < OmniAuth::Strategies::OAuth
def initialize(app, consumer_key, consumer_secret)
super(app, :twitter, consumer_key, consumer_secret,
:site => 'https://api.twitter.com')
:site => 'https://api.twitter.com',
:authorize_path => '/oauth/authenticate')
end

def auth_hash
Expand Down

0 comments on commit 5e8eb28

Please sign in to comment.