omniauth--omniauth/lib/omniauth.rb

171 lines
4.3 KiB
Ruby
Raw Normal View History

2011-09-03 18:08:07 +00:00
require 'rack'
require 'singleton'
2012-04-12 20:50:13 +00:00
require 'logger'
2011-09-03 18:08:07 +00:00
module OmniAuth
class Error < StandardError; end
module Strategies
autoload :Developer, 'omniauth/strategies/developer'
end
2011-09-03 18:08:07 +00:00
autoload :Builder, 'omniauth/builder'
autoload :Strategy, 'omniauth/strategy'
autoload :Test, 'omniauth/test'
autoload :Form, 'omniauth/form'
autoload :AuthHash, 'omniauth/auth_hash'
autoload :FailureEndpoint, 'omniauth/failure_endpoint'
2011-09-03 18:08:07 +00:00
def self.strategies
2014-01-16 04:00:46 +00:00
@strategies ||= []
2011-09-03 18:08:07 +00:00
end
class Configuration
include Singleton
2012-04-12 20:50:13 +00:00
def self.default_logger
logger = Logger.new(STDOUT)
2014-01-16 04:00:46 +00:00
logger.progname = 'omniauth'
2012-04-12 20:50:13 +00:00
logger
end
2011-09-03 18:08:07 +00:00
def self.defaults
2014-01-16 04:00:46 +00:00
@defaults ||= {
:camelizations => {},
:path_prefix => '/auth',
:on_failure => OmniAuth::FailureEndpoint,
:failure_raise_out_environments => ['development'],
:before_request_phase => nil,
:before_callback_phase => nil,
:before_options_phase => nil,
:form_css => Form::DEFAULT_CSS,
:test_mode => false,
:logger => default_logger,
:allowed_request_methods => [:get, :post],
2014-08-18 11:21:39 +00:00
:mock_auth => {:default => AuthHash.new('provider' => 'default', 'uid' => '1234', 'info' => {'name' => 'Example User'})},
2014-01-16 04:00:46 +00:00
}
2011-09-03 18:08:07 +00:00
end
def initialize
2014-01-16 04:00:46 +00:00
self.class.defaults.each_pair { |k, v| send("#{k}=", v) }
2011-09-03 18:08:07 +00:00
end
def on_failure(&block)
if block_given?
@on_failure = block
else
@on_failure
end
end
def before_callback_phase(&block)
if block_given?
@before_callback_phase = block
else
@before_callback_phase
end
end
def before_options_phase(&block)
if block_given?
@before_options_phase = block
else
@before_options_phase
end
end
def before_request_phase(&block)
if block_given?
@before_request_phase = block
else
@before_request_phase
end
end
2014-01-16 04:00:46 +00:00
def add_mock(provider, mock = {})
2011-09-03 18:08:07 +00:00
# Stringify keys recursively one level.
mock.keys.each do |key|
mock[key.to_s] = mock.delete(key)
end
mock.each_pair do |_key, val|
2011-09-03 18:08:07 +00:00
if val.is_a? Hash
val.keys.each do |subkey|
val[subkey.to_s] = val.delete(subkey)
end
else
next
2011-09-03 18:08:07 +00:00
end
end
# Merge with the default mock and ensure provider is correct.
2014-01-16 04:00:46 +00:00
mock = mock_auth[:default].dup.merge(mock)
mock['provider'] = provider.to_s
2011-09-03 18:08:07 +00:00
# Add it to the mocks.
2014-01-16 04:00:46 +00:00
mock_auth[provider.to_sym] = mock
2011-09-03 18:08:07 +00:00
end
# This is a convenience method to be used by strategy authors
# so that they can add special cases to the camelization utility
# method that allows OmniAuth::Builder to work.
#
# @param name [String] The underscored name, e.g. `oauth`
# @param camelized [String] The properly camelized name, e.g. 'OAuth'
def add_camelization(name, camelized)
2014-01-16 04:00:46 +00:00
camelizations[name.to_s] = camelized.to_s
end
attr_writer :on_failure, :before_callback_phase, :before_options_phase, :before_request_phase
2013-06-20 21:17:16 +00:00
attr_accessor :failure_raise_out_environments, :path_prefix, :allowed_request_methods, :form_css, :test_mode, :mock_auth, :full_host, :camelizations, :logger
2011-09-03 18:08:07 +00:00
end
def self.config
Configuration.instance
end
def self.configure
yield config
end
2012-04-12 20:50:13 +00:00
def self.logger
config.logger
end
2011-09-03 18:08:07 +00:00
def self.mock_auth_for(provider)
config.mock_auth[provider.to_sym] || config.mock_auth[:default]
end
module Utils
2014-10-01 19:35:17 +00:00
module_function
2011-09-03 18:08:07 +00:00
def form_css
"<style type='text/css'>#{OmniAuth.config.form_css}</style>"
end
def deep_merge(hash, other_hash)
target = hash.dup
other_hash.keys.each do |key|
2014-01-16 04:00:46 +00:00
if other_hash[key].is_a?(::Hash) && hash[key].is_a?(::Hash)
target[key] = deep_merge(target[key], other_hash[key])
2011-09-03 18:08:07 +00:00
next
end
target[key] = other_hash[key]
end
target
end
def camelize(word, first_letter_in_uppercase = true)
return OmniAuth.config.camelizations[word.to_s] if OmniAuth.config.camelizations[word.to_s]
2011-09-03 18:08:07 +00:00
if first_letter_in_uppercase
word.to_s.gsub(%r{/(.?)}) { '::' + Regexp.last_match[1].upcase }.gsub(/(^|_)(.)/) { Regexp.last_match[2].upcase }
2011-09-03 18:08:07 +00:00
else
word.first + camelize(word)[1..-1]
end
end
end
end