class OmniAuth::MultiProvider::Handler

Attributes

callback_path_regex[R]
callback_suffix[R]
identity_provider_options_generator[R]
path_prefix[R]
provider_instance_path_regex[R]
request_path_regex[R]

Public Class Methods

new(path_prefix:, identity_provider_id_regex:, callback_suffix: 'callback', **_options, &identity_provider_options_generator) click to toggle source
# File lib/omni_auth/multi_provider/handler.rb, line 10
def initialize(path_prefix:,
               identity_provider_id_regex:,
               callback_suffix: 'callback',
               **_options,
               &identity_provider_options_generator)
  raise 'Missing provider options generator block' unless block_given?

  @path_prefix = path_prefix
  @identity_provider_options_generator = identity_provider_options_generator
  @identity_provider_id_regex = identity_provider_id_regex
  @callback_suffix = callback_suffix

  # Eagerly compute these since lazy evaluation will not be threadsafe
  @provider_instance_path_regex = /^#{@path_prefix}\/(?<identity_provider_id>#{@identity_provider_id_regex})/
  @request_path_regex = /#{@provider_instance_path_regex}\/?$/
  @callback_path_regex = /#{@provider_instance_path_regex}\/#{@callback_suffix}\/?$/
end

Public Instance Methods

callback_path?(env) click to toggle source
# File lib/omni_auth/multi_provider/handler.rb, line 41
def callback_path?(env)
  path = current_path(env)
  !!callback_path_regex.match(path)
end
provider_options() click to toggle source
# File lib/omni_auth/multi_provider/handler.rb, line 28
def provider_options
  {
    request_path: method(:request_path?),
    callback_path: method(:callback_path?),
    setup: method(:setup)
  }
end
request_path?(env) click to toggle source
# File lib/omni_auth/multi_provider/handler.rb, line 36
def request_path?(env)
  path = current_path(env)
  !!request_path_regex.match(path)
end
setup(env) click to toggle source
# File lib/omni_auth/multi_provider/handler.rb, line 46
def setup(env)
  identity_provider_id = extract_identity_provider_id(env)
  if identity_provider_id
    strategy = env['omniauth.strategy']
    add_path_options(strategy, identity_provider_id)
    add_identity_provider_options(strategy, env, identity_provider_id)
  end
end

Private Instance Methods

add_identity_provider_options(strategy, env, identity_provider_id) click to toggle source
# File lib/omni_auth/multi_provider/handler.rb, line 64
def add_identity_provider_options(strategy, env, identity_provider_id)
  identity_provider_options = identity_provider_options_generator.call(identity_provider_id, env) || {}
  strategy.options.merge!(identity_provider_options)
rescue StandardError => e
  result = strategy.fail!(:invalid_identity_provider, e)
  throw :warden, result
end
add_path_options(strategy, identity_provider_id) click to toggle source
# File lib/omni_auth/multi_provider/handler.rb, line 57
def add_path_options(strategy, identity_provider_id)
  strategy.options.merge!(
    request_path: "#{path_prefix}/#{identity_provider_id}",
    callback_path: "#{path_prefix}/#{identity_provider_id}/#{callback_suffix}"
  )
end
current_path(env) click to toggle source
# File lib/omni_auth/multi_provider/handler.rb, line 72
def current_path(env)
  env['PATH_INFO']
end
extract_identity_provider_id(env) click to toggle source
# File lib/omni_auth/multi_provider/handler.rb, line 76
def extract_identity_provider_id(env)
  path = current_path(env)
  match = provider_instance_path_regex.match(path)
  match ? match[:identity_provider_id] : nil
end