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

Adds logging. Closes #583

This commit is contained in:
Michael Bleigh 2012-04-12 15:50:13 -05:00
parent d0d751a41b
commit 1a16151ab6
5 changed files with 55 additions and 1 deletions

View file

@ -120,6 +120,16 @@ environment information on the callback request. It is entirely up to
you how you want to implement the particulars of your application's
authentication flow.
## Logging
OmniAuth supports a configurable logger. By default, OmniAuth will log
to `STDOUT` but you can configure this using `OmniAuth.config.logger`:
```ruby
# Rails application example
OmniAuth.config.logger = Rails.logger
```
## <a name="resources"></a>Resources
The [OmniAuth Wiki](https://github.com/intridea/omniauth/wiki) has

View file

@ -1,5 +1,6 @@
require 'rack'
require 'singleton'
require 'logger'
module OmniAuth
class Error < StandardError; end
@ -22,12 +23,19 @@ module OmniAuth
class Configuration
include Singleton
def self.default_logger
logger = Logger.new(STDOUT)
logger.progname = "omniauth"
logger
end
@@defaults = {
:camelizations => {},
:path_prefix => '/auth',
:on_failure => OmniAuth::FailureEndpoint,
:form_css => Form::DEFAULT_CSS,
:test_mode => false,
:logger => default_logger,
:allowed_request_methods => [:get, :post],
:mock_auth => {
:default => AuthHash.new(
@ -88,7 +96,7 @@ module OmniAuth
end
attr_writer :on_failure
attr_accessor :path_prefix, :allowed_request_methods, :form_css, :test_mode, :mock_auth, :full_host, :camelizations
attr_accessor :path_prefix, :allowed_request_methods, :form_css, :test_mode, :mock_auth, :full_host, :camelizations, :logger
end
def self.config
@ -99,6 +107,10 @@ module OmniAuth
yield config
end
def self.logger
config.logger
end
def self.mock_auth_for(provider)
config.mock_auth[provider.to_sym] || config.mock_auth[:default]
end

View file

@ -142,6 +142,15 @@ module OmniAuth
"#<#{self.class.to_s}>"
end
# Direct access to the OmniAuth logger, automatically prefixed
# with this strategy's name.
#
# @example
# log :warn, "This is a warning."
def log(level, message)
OmniAuth.logger.send(level, "(#{name}) #{message}")
end
# Duplicates this instance and runs #call! on it.
# @param [Hash] The Rack environment.
def call(env)
@ -178,12 +187,16 @@ module OmniAuth
def request_call
setup_phase
log :info, "Request phase initiated."
#store query params from the request url, extracted in the callback_phase
session['omniauth.params'] = request.params
if options.form.respond_to?(:call)
log :info, "Rendering form from supplied Rack endpoint."
options.form.call(env)
elsif options.form
log :info, "Rendering form from underlying application."
call_app!
else
if request.params['origin']
@ -198,6 +211,8 @@ module OmniAuth
# Performs the steps necessary to run the callback phase of a strategy.
def callback_call
setup_phase
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') || {}
@ -266,8 +281,10 @@ module OmniAuth
# underlying application. This will default to `/auth/:provider/setup`.
def setup_phase
if options[:setup].respond_to?(:call)
log :info, "Setup endpoint detected, running now."
options[:setup].call(env)
elsif options.setup?
log :info, "Calling through to underlying application for setup."
setup_env = env.merge('PATH_INFO' => setup_path, 'REQUEST_METHOD' => 'GET')
call_app!(setup_env)
end
@ -413,6 +430,12 @@ module OmniAuth
self.env['omniauth.error.type'] = message_key.to_sym
self.env['omniauth.error.strategy'] = self
if exception
log :error, "Authentication failure! #{message_key}: #{exception.class.to_s}, #{exception.message}"
else
log :error, "Authentication failure! #{message_key} encountered."
end
OmniAuth.config.on_failure.call(self.env)
end

View file

@ -76,6 +76,13 @@ describe OmniAuth do
end
end
describe '.logger' do
it 'should call through to the configured logger' do
OmniAuth.stub(:config => mock(:logger => "foo"))
OmniAuth.logger.should == "foo"
end
end
describe '::Utils' do
describe '.deep_merge' do
it 'should combine hashes' do

View file

@ -8,6 +8,8 @@ require 'rack/test'
require 'omniauth'
require 'omniauth/test'
OmniAuth.config.logger = Logger.new("/dev/null")
RSpec.configure do |config|
config.include Rack::Test::Methods
config.extend OmniAuth::Test::StrategyMacros, :type => :strategy