Module: OmniAuth::Strategy

Defined in:
lib/omniauth/strategy.rb

Overview

The Strategy is the base unit of OmniAuth's ability to wrangle multiple providers. Each strategy provided by OmniAuth includes this mixin to gain the default functionality necessary to be compatible with the OmniAuth library.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



10
11
12
13
14
15
# File 'lib/omniauth/strategy.rb', line 10

def self.included(base)
  OmniAuth.strategies << base
  base.class_eval do
    attr_reader :app, :name, :env, :options, :response
  end
end

Instance Method Details

#auth_hashObject



170
171
172
173
174
175
# File 'lib/omniauth/strategy.rb', line 170

def auth_hash
  {
    'provider' => name.to_s,
    'uid' => nil
  }
end

#call(env) ⇒ Object



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

def call(env)
  dup.call!(env)
end

#call!(env) ⇒ Object



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

def call!(env)
  raise OmniAuth::NoSessionError.new("You must provide a session to use OmniAuth.") unless env['rack.session']

  @env = env
  @env['omniauth.strategy'] = self if on_auth_path?

  return mock_call!(env) if OmniAuth.config.test_mode

  return request_call if on_request_path? && OmniAuth.config.allowed_request_methods.include?(request.request_method.downcase.to_sym)
  return callback_call if on_callback_path?
  return other_phase if respond_to?(:other_phase)
  @app.call(env)
end

#call_app!(env = @env) ⇒ Object



166
167
168
# File 'lib/omniauth/strategy.rb', line 166

def call_app!(env = @env)
  @app.call(env)
end

#call_through_to_appObject



158
159
160
161
162
163
164
# File 'lib/omniauth/strategy.rb', line 158

def call_through_to_app
  status, headers, body = *call_app!
  session['query_params'] = Rack::Request.new(env).params
  @response = Rack::Response.new(body, status, headers)

  status == 404 ? nil : @response.finish
end

#callback_callObject

Performs the steps necessary to run the callback phase of a strategy.



63
64
65
66
67
68
69
# File 'lib/omniauth/strategy.rb', line 63

def callback_call
  setup_phase
  @env['omniauth.origin'] = session.delete('omniauth.origin')
  @env['omniauth.origin'] = nil if env['omniauth.origin'] == ''

  callback_phase
end

#callback_pathObject



142
143
144
# File 'lib/omniauth/strategy.rb', line 142

def callback_path
  options[:callback_path] || "#{path_prefix}/#{name}/callback"
end

#callback_phaseObject



127
128
129
130
131
132
# File 'lib/omniauth/strategy.rb', line 127

def callback_phase
  @env['omniauth.auth'] = auth_hash
  @env['omniauth.params'] = session['query_params'] || {}
  session['query_params'] = nil if session['query_params']
  call_app!
end

#callback_urlObject



191
192
193
# File 'lib/omniauth/strategy.rb', line 191

def callback_url
  full_host + script_name + callback_path + query_string
end

#current_pathObject



150
151
152
# File 'lib/omniauth/strategy.rb', line 150

def current_path
  request.path_info.downcase.sub(/\/$/,'')
end

#fail!(message_key, exception = nil) ⇒ Object



222
223
224
225
226
227
228
# File 'lib/omniauth/strategy.rb', line 222

def fail!(message_key, exception = nil)
  self.env['omniauth.error'] = exception
  self.env['omniauth.error.type'] = message_key.to_sym
  self.env['omniauth.error.strategy'] = self

  OmniAuth.config.on_failure.call(self.env)
end

#full_hostObject



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/omniauth/strategy.rb', line 177

def full_host
  case OmniAuth.config.full_host
    when String
      OmniAuth.config.full_host
    when Proc
      OmniAuth.config.full_host.call(env)
    else
      uri = URI.parse(request.url.gsub(/\?.*$/,''))
      uri.path = ''
      uri.query = nil
      uri.to_s
  end
end

#initialize(app, name, *args) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



17
18
19
20
21
22
23
# File 'lib/omniauth/strategy.rb', line 17

def initialize(app, name, *args, &block)
  @app = app
  @name = name.to_sym
  @options = args.last.is_a?(Hash) ? args.pop : {}

  yield self if block_given?
end

#inspectObject



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

def inspect
  "#<#{self.class.to_s}>"
end

#mock_call!(env) ⇒ Object



83
84
85
86
87
# File 'lib/omniauth/strategy.rb', line 83

def mock_call!(env)
  return mock_request_call if on_request_path?
  return mock_callback_call if on_callback_path?
  call_app!
end

#mock_callback_callObject



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/omniauth/strategy.rb', line 101

def mock_callback_call
  setup_phase
  mocked_auth = OmniAuth.mock_auth_for(name.to_sym)
  if mocked_auth.is_a?(Symbol)
    fail!(mocked_auth)
  else
    @env['omniauth.auth'] = mocked_auth
    @env['omniauth.origin'] = session.delete('omniauth.origin')
    @env['omniauth.origin'] = nil if env['omniauth.origin'] == ''
    call_app!
  end
end

#mock_request_callObject



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/omniauth/strategy.rb', line 89

def mock_request_call
  setup_phase
  return response if response = call_through_to_app

  if request.params['origin']
    @env['rack.session']['omniauth.origin'] = request.params['origin']
  elsif env['HTTP_REFERER'] && !env['HTTP_REFERER'].match(/#{request_path}$/)
    @env['rack.session']['omniauth.origin'] = env['HTTP_REFERER']
  end
  redirect(script_name + callback_path + query_string)
end

#on_auth_path?Boolean

Returns:

  • (Boolean)


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

def on_auth_path?
  on_request_path? || on_callback_path?
end

#on_callback_path?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/omniauth/strategy.rb', line 79

def on_callback_path?
  current_path.casecmp(callback_path) == 0
end

#on_request_path?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/omniauth/strategy.rb', line 75

def on_request_path?
  current_path.casecmp(request_path) == 0
end

#path_prefixObject



134
135
136
# File 'lib/omniauth/strategy.rb', line 134

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

#query_stringObject



154
155
156
# File 'lib/omniauth/strategy.rb', line 154

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

#redirect(uri) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/omniauth/strategy.rb', line 207

def redirect(uri)
  r = Rack::Response.new

  if options[:iframe]
    r.write("<script type='text/javascript' charset='utf-8'>top.location.href = '#{uri}';</script>")
  else
    r.write("Redirecting to #{uri}...")
    r.redirect(uri)
  end

  r.finish
end

#requestObject



203
204
205
# File 'lib/omniauth/strategy.rb', line 203

def request
  @request ||= Rack::Request.new(@env)
end

#request_callObject

Performs the steps necessary to run the request phase of a strategy.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/omniauth/strategy.rb', line 48

def request_call
  setup_phase
  if response = call_through_to_app
    response
  else
    if request.params['origin']
      @env['rack.session']['omniauth.origin'] = request.params['origin']
    elsif env['HTTP_REFERER'] && !env['HTTP_REFERER'].match(/#{request_path}$/)
      @env['rack.session']['omniauth.origin'] = env['HTTP_REFERER']
    end
    request_phase
  end
end

#request_pathObject



138
139
140
# File 'lib/omniauth/strategy.rb', line 138

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

#request_phaseObject

Raises:

  • (NotImplementedError)


123
124
125
# File 'lib/omniauth/strategy.rb', line 123

def request_phase
  raise NotImplementedError
end

#script_nameObject



195
196
197
# File 'lib/omniauth/strategy.rb', line 195

def script_name
  @env['SCRIPT_NAME'] || ''
end

#sessionObject



199
200
201
# File 'lib/omniauth/strategy.rb', line 199

def session
  @env['rack.session']
end

#setup_pathObject



146
147
148
# File 'lib/omniauth/strategy.rb', line 146

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

#setup_phaseObject



114
115
116
117
118
119
120
121
# File 'lib/omniauth/strategy.rb', line 114

def setup_phase
  if options[:setup].respond_to?(:call)
    options[:setup].call(env)
  elsif options[:setup]
    setup_env = env.merge('PATH_INFO' => setup_path, 'REQUEST_METHOD' => 'GET')
    call_app!(setup_env)
  end
end

#user_infoObject



220
# File 'lib/omniauth/strategy.rb', line 220

def ; {} end