2011-09-03 14:08:07 -04:00
|
|
|
require 'rack'
|
|
|
|
require 'singleton'
|
2012-04-12 16:50:13 -04:00
|
|
|
require 'logger'
|
2011-09-03 14:08:07 -04:00
|
|
|
|
|
|
|
module OmniAuth
|
2012-03-05 23:11:35 -05:00
|
|
|
class Error < StandardError; end
|
2013-01-25 13:24:52 -05:00
|
|
|
|
2011-10-16 19:27:26 -04:00
|
|
|
module Strategies
|
|
|
|
autoload :Developer, 'omniauth/strategies/developer'
|
|
|
|
end
|
2011-09-03 14:08:07 -04:00
|
|
|
|
|
|
|
autoload :Builder, 'omniauth/builder'
|
|
|
|
autoload :Strategy, 'omniauth/strategy'
|
|
|
|
autoload :Test, 'omniauth/test'
|
|
|
|
autoload :Form, 'omniauth/form'
|
2011-09-03 20:26:57 -04:00
|
|
|
autoload :AuthHash, 'omniauth/auth_hash'
|
2012-03-05 20:27:08 -05:00
|
|
|
autoload :FailureEndpoint, 'omniauth/failure_endpoint'
|
2020-12-02 22:16:51 -05:00
|
|
|
autoload :AuthenticityTokenProtection, 'omniauth/authenticity_token_protection'
|
2011-09-03 14:08:07 -04:00
|
|
|
|
|
|
|
def self.strategies
|
2014-01-15 23:00:46 -05:00
|
|
|
@strategies ||= []
|
2011-09-03 14:08:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class Configuration
|
|
|
|
include Singleton
|
|
|
|
|
2012-04-12 16:50:13 -04:00
|
|
|
def self.default_logger
|
|
|
|
logger = Logger.new(STDOUT)
|
2014-01-15 23:00:46 -05:00
|
|
|
logger.progname = 'omniauth'
|
2012-04-12 16:50:13 -04:00
|
|
|
logger
|
|
|
|
end
|
|
|
|
|
2020-12-02 22:16:51 -05:00
|
|
|
def self.defaults # rubocop:disable MethodLength
|
2014-01-15 23:00:46 -05:00
|
|
|
@defaults ||= {
|
|
|
|
:camelizations => {},
|
|
|
|
:path_prefix => '/auth',
|
|
|
|
:on_failure => OmniAuth::FailureEndpoint,
|
|
|
|
:failure_raise_out_environments => ['development'],
|
2020-12-02 22:16:51 -05:00
|
|
|
:request_validation_phase => OmniAuth::AuthenticityTokenProtection,
|
2014-01-15 23:00:46 -05:00
|
|
|
:before_request_phase => nil,
|
|
|
|
:before_callback_phase => nil,
|
|
|
|
:before_options_phase => nil,
|
|
|
|
:form_css => Form::DEFAULT_CSS,
|
|
|
|
:test_mode => false,
|
|
|
|
:logger => default_logger,
|
2020-12-02 22:16:51 -05:00
|
|
|
:allowed_request_methods => %i[post],
|
|
|
|
:mock_auth => {:default => AuthHash.new('provider' => 'default', 'uid' => '1234', 'info' => {'name' => 'Example User'})},
|
|
|
|
:silence_get_warning => false
|
2014-01-15 23:00:46 -05:00
|
|
|
}
|
2011-09-03 14:08:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
2014-01-15 23:00:46 -05:00
|
|
|
self.class.defaults.each_pair { |k, v| send("#{k}=", v) }
|
2011-09-03 14:08:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_failure(&block)
|
|
|
|
if block_given?
|
|
|
|
@on_failure = block
|
|
|
|
else
|
|
|
|
@on_failure
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-02 21:05:18 -04:00
|
|
|
def before_callback_phase(&block)
|
2013-02-25 18:52:36 -05:00
|
|
|
if block_given?
|
2013-09-02 21:05:18 -04:00
|
|
|
@before_callback_phase = block
|
2013-02-25 18:52:36 -05:00
|
|
|
else
|
2013-09-02 21:05:18 -04:00
|
|
|
@before_callback_phase
|
2013-02-25 18:52:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-02 21:05:18 -04:00
|
|
|
def before_options_phase(&block)
|
2013-02-25 18:52:36 -05:00
|
|
|
if block_given?
|
2013-09-02 21:05:18 -04:00
|
|
|
@before_options_phase = block
|
2013-02-25 18:52:36 -05:00
|
|
|
else
|
2013-09-02 21:05:18 -04:00
|
|
|
@before_options_phase
|
2013-02-25 18:52:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-02 22:16:51 -05:00
|
|
|
def request_validation_phase(&block)
|
|
|
|
if block_given?
|
|
|
|
@request_validation_phase = block
|
|
|
|
else
|
|
|
|
@request_validation_phase
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-02 21:05:18 -04:00
|
|
|
def before_request_phase(&block)
|
2013-02-25 18:52:36 -05:00
|
|
|
if block_given?
|
2013-09-02 21:05:18 -04:00
|
|
|
@before_request_phase = block
|
2013-02-25 18:52:36 -05:00
|
|
|
else
|
2013-09-02 21:05:18 -04:00
|
|
|
@before_request_phase
|
2013-02-25 18:52:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-10 23:07:57 -05:00
|
|
|
def add_mock(provider, original = {})
|
|
|
|
# Create key-stringified new hash from given auth hash
|
|
|
|
mock = {}
|
|
|
|
original.each_pair do |key, val|
|
|
|
|
mock[key.to_s] = if val.is_a? Hash
|
2015-11-10 23:14:34 -05:00
|
|
|
Hash[val.each_pair { |k, v| [k.to_s, v] }]
|
2015-11-10 23:07:57 -05:00
|
|
|
else
|
|
|
|
val
|
|
|
|
end
|
2011-09-03 14:08:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Merge with the default mock and ensure provider is correct.
|
2014-01-15 23:00:46 -05:00
|
|
|
mock = mock_auth[:default].dup.merge(mock)
|
|
|
|
mock['provider'] = provider.to_s
|
2011-09-03 14:08:07 -04:00
|
|
|
|
|
|
|
# Add it to the mocks.
|
2014-01-15 23:00:46 -05:00
|
|
|
mock_auth[provider.to_sym] = mock
|
2011-09-03 14:08:07 -04:00
|
|
|
end
|
|
|
|
|
2011-09-26 12:44:17 -04:00
|
|
|
# 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'
|
2011-09-22 19:49:22 -04:00
|
|
|
def add_camelization(name, camelized)
|
2014-01-15 23:00:46 -05:00
|
|
|
camelizations[name.to_s] = camelized.to_s
|
2011-09-22 19:49:22 -04:00
|
|
|
end
|
|
|
|
|
2020-12-02 22:16:51 -05:00
|
|
|
attr_writer :on_failure, :before_callback_phase, :before_options_phase, :before_request_phase, :request_validation_phase
|
|
|
|
attr_accessor :failure_raise_out_environments, :path_prefix, :allowed_request_methods, :form_css,
|
|
|
|
:test_mode, :mock_auth, :full_host, :camelizations, :logger, :silence_get_warning
|
2011-09-03 14:08:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.config
|
|
|
|
Configuration.instance
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.configure
|
|
|
|
yield config
|
|
|
|
end
|
|
|
|
|
2012-04-12 16:50:13 -04:00
|
|
|
def self.logger
|
|
|
|
config.logger
|
|
|
|
end
|
|
|
|
|
2011-09-03 14:08:07 -04:00
|
|
|
def self.mock_auth_for(provider)
|
|
|
|
config.mock_auth[provider.to_sym] || config.mock_auth[:default]
|
|
|
|
end
|
|
|
|
|
|
|
|
module Utils
|
2019-11-10 22:32:54 -05:00
|
|
|
module_function # rubocop:disable Layout/IndentationWidth
|
2011-09-03 14:08:07 -04:00
|
|
|
|
|
|
|
def form_css
|
|
|
|
"<style type='text/css'>#{OmniAuth.config.form_css}</style>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def deep_merge(hash, other_hash)
|
|
|
|
target = hash.dup
|
|
|
|
|
2017-09-28 13:07:45 -04:00
|
|
|
other_hash.each_key do |key|
|
2014-01-15 23:00:46 -05: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 14:08:07 -04:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
target[key] = other_hash[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
target
|
|
|
|
end
|
|
|
|
|
|
|
|
def camelize(word, first_letter_in_uppercase = true)
|
2011-09-22 19:49:22 -04:00
|
|
|
return OmniAuth.config.camelizations[word.to_s] if OmniAuth.config.camelizations[word.to_s]
|
2011-09-03 14:08:07 -04:00
|
|
|
|
|
|
|
if first_letter_in_uppercase
|
2015-05-25 05:24:12 -04:00
|
|
|
word.to_s.gsub(%r{/(.?)}) { '::' + Regexp.last_match[1].upcase }.gsub(/(^|_)(.)/) { Regexp.last_match[2].upcase }
|
2011-09-03 14:08:07 -04:00
|
|
|
else
|
2020-03-02 15:07:25 -05:00
|
|
|
camelize(word).tap { |w| w[0] = w[0].downcase }
|
2011-09-03 14:08:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|