2017-12-21 12:36:29 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2010-10-14 14:04:02 -04:00
|
|
|
module Devise
|
|
|
|
module OmniAuth
|
2011-11-20 14:42:02 -05:00
|
|
|
class StrategyNotFound < NameError
|
|
|
|
def initialize(strategy)
|
|
|
|
@strategy = strategy
|
|
|
|
super("Could not find a strategy with name `#{strategy}'. " \
|
2011-11-24 03:32:48 -05:00
|
|
|
"Please ensure it is required or explicitly set it using the :strategy_class option.")
|
2011-11-20 14:42:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-14 14:04:02 -04:00
|
|
|
class Config
|
|
|
|
attr_accessor :strategy
|
2011-11-20 14:42:02 -05:00
|
|
|
attr_reader :args, :options, :provider, :strategy_name
|
2010-10-14 14:04:02 -04:00
|
|
|
|
|
|
|
def initialize(provider, args)
|
2011-11-20 14:42:02 -05:00
|
|
|
@provider = provider
|
|
|
|
@args = args
|
|
|
|
@options = @args.last.is_a?(Hash) ? @args.last : {}
|
|
|
|
@strategy = nil
|
|
|
|
@strategy_name = options[:name] || @provider
|
|
|
|
@strategy_class = options.delete(:strategy_class)
|
2011-05-24 15:59:36 -04:00
|
|
|
end
|
|
|
|
|
2010-10-14 14:04:02 -04:00
|
|
|
def strategy_class
|
2011-11-20 14:42:02 -05:00
|
|
|
@strategy_class ||= find_strategy || autoload_strategy
|
2011-10-17 13:42:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_strategy
|
|
|
|
::OmniAuth.strategies.find do |strategy_class|
|
|
|
|
strategy_class.to_s =~ /#{::OmniAuth::Utils.camelize(strategy_name)}$/ ||
|
|
|
|
strategy_class.default_options[:name] == strategy_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-20 14:42:02 -05:00
|
|
|
def autoload_strategy
|
|
|
|
name = ::OmniAuth::Utils.camelize(provider.to_s)
|
|
|
|
if ::OmniAuth::Strategies.const_defined?(name)
|
|
|
|
::OmniAuth::Strategies.const_get(name)
|
2011-10-17 13:42:40 -04:00
|
|
|
else
|
2011-11-20 14:42:02 -05:00
|
|
|
raise StrategyNotFound, name
|
2011-10-17 13:42:40 -04:00
|
|
|
end
|
2010-10-14 14:04:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-11-20 14:42:02 -05:00
|
|
|
end
|