Class: OmniAuth::MultiProvider::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/omni_auth/multi_provider/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_prefix:, identity_provider_id_regex:, callback_suffix: 'callback', **_options, &identity_provider_options_generator) ⇒ Handler

Returns a new instance of Handler.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/omni_auth/multi_provider/handler.rb', line 8

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

Instance Attribute Details

#callback_path_regexObject (readonly)

Returns the value of attribute callback_path_regex.



4
5
6
# File 'lib/omni_auth/multi_provider/handler.rb', line 4

def callback_path_regex
  @callback_path_regex
end

#callback_suffixObject (readonly)

Returns the value of attribute callback_suffix.



4
5
6
# File 'lib/omni_auth/multi_provider/handler.rb', line 4

def callback_suffix
  @callback_suffix
end

#identity_provider_options_generatorObject (readonly)

Returns the value of attribute identity_provider_options_generator.



4
5
6
# File 'lib/omni_auth/multi_provider/handler.rb', line 4

def identity_provider_options_generator
  @identity_provider_options_generator
end

#path_prefixObject (readonly)

Returns the value of attribute path_prefix.



4
5
6
# File 'lib/omni_auth/multi_provider/handler.rb', line 4

def path_prefix
  @path_prefix
end

#provider_instance_path_regexObject (readonly)

Returns the value of attribute provider_instance_path_regex.



4
5
6
# File 'lib/omni_auth/multi_provider/handler.rb', line 4

def provider_instance_path_regex
  @provider_instance_path_regex
end

#request_path_regexObject (readonly)

Returns the value of attribute request_path_regex.



4
5
6
# File 'lib/omni_auth/multi_provider/handler.rb', line 4

def request_path_regex
  @request_path_regex
end

Instance Method Details

#callback_path?(env) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/omni_auth/multi_provider/handler.rb', line 39

def callback_path?(env)
  path = current_path(env)
  !!callback_path_regex.match(path)
end

#provider_optionsObject



26
27
28
29
30
31
32
# File 'lib/omni_auth/multi_provider/handler.rb', line 26

def provider_options
  {
    request_path: method(:request_path?),
    callback_path: method(:callback_path?),
    setup: method(:setup)
  }
end

#request_path?(env) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/omni_auth/multi_provider/handler.rb', line 34

def request_path?(env)
  path = current_path(env)
  !!request_path_regex.match(path)
end

#setup(env) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/omni_auth/multi_provider/handler.rb', line 44

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