Module: OmniAuth::Strategy::Paths

Included in:
OmniAuth::Strategy
Defined in:
lib/omniauth/strategy/paths.rb,
sig/omniauth.rbs

Overview

Works out the request, callback, and setup paths for a strategy and whether the current request is on one of them, plus the full URLs built from them.

Constant Summary collapse

CURRENT_PATH_REGEX =

Returns:

  • (Regexp)
%r{/$}.freeze
FORMAT_EXTENSION_REGEX =

Returns:

  • (Regexp)
/\.\w+\z/.freeze
EMPTY_STRING =

Returns:

  • (String)
''.freeze
SSL_ENV_VALUES =

Rack env values that mean the request came in over HTTPS.

Returns:

  • (Hash[String, String])
{
  'HTTPS' => 'on',
  'HTTP_X_FORWARDED_SSL' => 'on',
  'HTTP_X_FORWARDED_SCHEME' => 'https',
  'rack.url_scheme' => 'https'
}.freeze

Instance Method Summary collapse

Instance Method Details

#callback_path ⇒ String

Returns:

  • (String)


53
54
55
# File 'lib/omniauth/strategy/paths.rb', line 53

def callback_path
  @callback_path ||= callback_path_option || custom_path(:request_path) || "#{script_name}#{path_prefix}/#{name}/callback"
end

#callback_url ⇒ String

Returns:

  • (String)


86
87
88
# File 'lib/omniauth/strategy/paths.rb', line 86

def callback_url
  full_host + callback_path + query_string
end

#current_path ⇒ String

Repeated slashes are collapsed, as Rails routing does, so a proxy that sends //auth/:provider still reaches the strategy.

Returns:

  • (String)


66
67
68
# File 'lib/omniauth/strategy/paths.rb', line 66

def current_path
  @current_path ||= request.path.downcase.squeeze('/').sub(CURRENT_PATH_REGEX, EMPTY_STRING)
end

#custom_path(kind) ⇒ String?

Parameters:

  • kind (Symbol)

Returns:

  • (String, nil)


33
34
35
36
37
38
39
40
41
42
# File 'lib/omniauth/strategy/paths.rb', line 33

def custom_path(kind)
  if options[kind].respond_to?(:call)
    result = options.fetch(kind).call(env)
    return nil unless result.is_a?(String)

    result
  else
    options[kind]
  end
end

#full_host ⇒ String

The scheme and host for URLs such as callback_url: the strategy's :full_host option, else OmniAuth.config.full_host, else the request's. Either setting can be a string or a proc taking the env.

Returns:

  • (String)


77
78
79
80
81
82
83
84
# File 'lib/omniauth/strategy/paths.rb', line 77

def full_host
  host = options[:full_host] || OmniAuth.config.full_host
  case host
  when String then host
  when Proc then host.call(env)
  else full_host_from_request
  end
end

#on_auth_path? ⇒ Boolean

Returns true if the environment recognizes either the request or callback path.

Returns:

  • (Boolean)


9
10
11
# File 'lib/omniauth/strategy/paths.rb', line 9

def on_auth_path?
  on_request_path? || on_callback_path?
end

#on_callback_path? ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/omniauth/strategy/paths.rb', line 21

def on_callback_path?
  on_path?(callback_path) || current_path.sub(FORMAT_EXTENSION_REGEX, EMPTY_STRING).casecmp(callback_path).zero?
end

#on_path?(path) ⇒ Boolean

Parameters:

  • path (String)

Returns:

  • (Boolean)


25
26
27
# File 'lib/omniauth/strategy/paths.rb', line 25

def on_path?(path)
  current_path.casecmp(path).zero?
end

#on_request_path? ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
# File 'lib/omniauth/strategy/paths.rb', line 13

def on_request_path?
  if options[:request_path].respond_to?(:call)
    options.fetch(:request_path).call(env)
  else
    on_path?(request_path)
  end
end

#path_prefix ⇒ String

Returns:

  • (String)


29
30
31
# File 'lib/omniauth/strategy/paths.rb', line 29

def path_prefix
  options[:path_prefix] || OmniAuth.config.path_prefix
end

#query_string ⇒ String

Returns:

  • (String)


70
71
72
# File 'lib/omniauth/strategy/paths.rb', line 70

def query_string
  request.query_string.empty? ? '' : "?#{request.query_string}"
end

#request_path ⇒ String

Returns:

  • (String)


44
45
46
47
48
49
50
51
# File 'lib/omniauth/strategy/paths.rb', line 44

def request_path
  @request_path ||=
    if options[:request_path].is_a?(String)
      options.fetch(:request_path)
    else
      "#{script_name}#{path_prefix}/#{name}"
    end
end

#script_name ⇒ String

Returns:

  • (String)


90
91
92
93
94
95
# File 'lib/omniauth/strategy/paths.rb', line 90

def script_name
  current_env = @env
  return '' if current_env.nil?

  current_env['SCRIPT_NAME'] || ''
end

#setup_path ⇒ String

Returns:

  • (String)


57
58
59
# File 'lib/omniauth/strategy/paths.rb', line 57

def setup_path
  options[:setup_path] || "#{path_prefix}/#{name}/setup"
end