class OmniAuth::SAML::MultiProvider::Handler

Attributes

callback_path_regex[R]
identity_provider_id_regex[R]
identity_provider_options_generator[R]
path_prefix[R]
provider_path_prefix[R]
request_path_regex[R]
saml_path_regex[R]

Public Class Methods

new(path_prefix: OmniAuth.config.path_prefix, identity_provider_id_regex: /\w+/, &identity_provider_options_generator) click to toggle source
# File lib/omniauth/saml/multi_provider/handler.rb, line 10
def initialize(path_prefix: OmniAuth.config.path_prefix,
               identity_provider_id_regex: /\w+/, &identity_provider_options_generator)
  raise 'Missing provider options generator block' unless block_given?

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

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

Public Instance Methods

provider_options() click to toggle source
# File lib/omniauth/saml/multi_provider/handler.rb, line 25
def provider_options
  {
      request_path: method(:request_path?),
      callback_path: method(:callback_path?),
      setup: method(:setup)
  }
end

Private Instance Methods

add_identity_provider_options(strategy, env, identity_provider_id) click to toggle source
# File lib/omniauth/saml/multi_provider/handler.rb, line 54
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 => 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/omniauth/saml/multi_provider/handler.rb, line 47
def add_path_options(strategy, identity_provider_id)
  strategy.options.merge!(
      request_path: "#{provider_path_prefix}/#{identity_provider_id}",
      callback_path: "#{provider_path_prefix}/#{identity_provider_id}/callback"
  )
end
callback_path?(env) click to toggle source
# File lib/omniauth/saml/multi_provider/handler.rb, line 67
def callback_path?(env)
  path = current_path(env)
  !!callback_path_regex.match(path)
end
current_path(env) click to toggle source
# File lib/omniauth/saml/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/omniauth/saml/multi_provider/handler.rb, line 76
def extract_identity_provider_id(env)
  path = current_path(env)
  match = saml_path_regex.match(path)
  match ? match[:identity_provider_id] : nil
end
request_path?(env) click to toggle source
# File lib/omniauth/saml/multi_provider/handler.rb, line 62
def request_path?(env)
  path = current_path(env)
  !!request_path_regex.match(path)
end
setup(env) click to toggle source
# File lib/omniauth/saml/multi_provider/handler.rb, line 38
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