Module: OmniAuth::Strategy
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.
Defined Under Namespace
Modules: ClassMethods
Classes: Options
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(base) ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/omniauth/strategy.rb', line 11
def self.included(base)
OmniAuth.strategies << base
base.extend ClassMethods
base.class_eval do
attr_reader :app, :env, :options, :response
option :setup, false
option :skip_info, false
end
end
|
Instance Method Details
#auth_hash ⇒ Object
302
303
304
305
306
307
308
|
# File 'lib/omniauth/strategy.rb', line 302
def auth_hash
hash = AuthHash.new(:provider => name, :uid => uid)
hash.info = info unless skip_info?
hash.credentials = credentials if credentials
hash. = if
hash
end
|
#call(env) ⇒ Object
Duplicates this instance and runs #call! on it.
147
148
149
|
# File 'lib/omniauth/strategy.rb', line 147
def call(env)
dup.call!(env)
end
|
#call!(env) ⇒ Object
The logic for dispatching any additional actions that need
to be taken. For instance, calling the request phase if
the request path is recognized.
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/omniauth/strategy.rb', line 156
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 options_call if on_auth_path? && options_request?
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
367
368
369
|
# File 'lib/omniauth/strategy.rb', line 367
def call_app!(env = @env)
@app.call(env)
end
|
#call_through_to_app ⇒ Object
359
360
361
362
363
364
365
|
# File 'lib/omniauth/strategy.rb', line 359
def call_through_to_app
status, , body = *call_app!
session['query_params'] = Rack::Request.new(env).params
@response = Rack::Response.new(body, status, )
status == 404 ? nil : @response.finish
end
|
#callback_call ⇒ Object
Performs the steps necessary to run the callback phase of a strategy.
199
200
201
202
203
204
205
|
# File 'lib/omniauth/strategy.rb', line 199
def callback_call
setup_phase
@env['omniauth.origin'] = session.delete('omniauth.origin')
@env['omniauth.origin'] = nil if env['omniauth.origin'] == ''
@env['omniauth.params'] = session.delete('omniauth.params') || {}
callback_phase
end
|
#callback_path ⇒ Object
343
344
345
|
# File 'lib/omniauth/strategy.rb', line 343
def callback_path
options[:callback_path] || "#{path_prefix}/#{name}/callback"
end
|
#callback_phase ⇒ Object
330
331
332
333
|
# File 'lib/omniauth/strategy.rb', line 330
def callback_phase
self.env['omniauth.auth'] = auth_hash
call_app!
end
|
#callback_url ⇒ Object
387
388
389
|
# File 'lib/omniauth/strategy.rb', line 387
def callback_url
full_host + script_name + callback_path + query_string
end
|
#credentials ⇒ Object
294
295
296
|
# File 'lib/omniauth/strategy.rb', line 294
def credentials
merge_stack(self.class.credentials_stack(self))
end
|
#current_path ⇒ Object
351
352
353
|
# File 'lib/omniauth/strategy.rb', line 351
def current_path
request.path_info.downcase.sub(/\/$/,'')
end
|
298
299
300
|
# File 'lib/omniauth/strategy.rb', line 298
def
merge_stack(self.class.(self))
end
|
#fail!(message_key, exception = nil) ⇒ Object
422
423
424
425
426
427
428
|
# File 'lib/omniauth/strategy.rb', line 422
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_host ⇒ Object
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
# File 'lib/omniauth/strategy.rb', line 371
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.scheme = 'https' if(request.env['HTTP_X_FORWARDED_PROTO'] == 'https')
uri.to_s
end
end
|
#info ⇒ Object
290
291
292
|
# File 'lib/omniauth/strategy.rb', line 290
def info
merge_stack(self.class.info_stack(self))
end
|
#new(app, options = {}) ⇒ Object
#new(app, *args, options = {}) ⇒ Object
Initializes the strategy by passing in the Rack endpoint,
the unique URL segment name for this strategy, and any
additional arguments. An options
hash is automatically
created from the last argument if it is a hash.
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/omniauth/strategy.rb', line 124
def initialize(app, *args, &block)
@app = app
@options = self.class.default_options.dup
options.deep_merge!(args.pop) if args.last.is_a?(Hash)
options.name ||= self.class.to_s.split('::').last.downcase
self.class.args.each do |arg|
options[arg] = args.shift
end
raise ArgumentError, "Received wrong number of arguments. #{args.inspect}" unless args.empty?
yield options if block_given?
end
|
#inspect ⇒ Object
141
142
143
|
# File 'lib/omniauth/strategy.rb', line 141
def inspect
"#<#{self.class.to_s}>"
end
|
#mock_call!(env) ⇒ Object
This is called in lieu of the normal request process
in the event that OmniAuth has been configured to be
in test mode.
232
233
234
235
236
|
# File 'lib/omniauth/strategy.rb', line 232
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_call ⇒ Object
252
253
254
255
256
257
258
259
260
261
262
263
264
|
# File 'lib/omniauth/strategy.rb', line 252
def mock_callback_call
setup_phase
mocked_auth = OmniAuth.mock_auth_for(name.to_s)
if mocked_auth.is_a?(Symbol)
fail!(mocked_auth)
else
@env['omniauth.auth'] = mocked_auth
@env['omniauth.params'] = session.delete('query_params') || {}
@env['omniauth.origin'] = session.delete('omniauth.origin')
@env['omniauth.origin'] = nil if env['omniauth.origin'] == ''
call_app!
end
end
|
#mock_request_call ⇒ Object
238
239
240
241
242
243
244
245
246
247
248
249
250
|
# File 'lib/omniauth/strategy.rb', line 238
def mock_request_call
setup_phase
if response = call_through_to_app
return response
end
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
|
#name ⇒ Object
403
404
405
|
# File 'lib/omniauth/strategy.rb', line 403
def name
options.name
end
|
#on_auth_path? ⇒ Boolean
Returns true if the environment recognizes either the
request or callback path.
209
210
211
|
# File 'lib/omniauth/strategy.rb', line 209
def on_auth_path?
on_request_path? || on_callback_path?
end
|
#on_callback_path? ⇒ Boolean
217
218
219
|
# File 'lib/omniauth/strategy.rb', line 217
def on_callback_path?
on_path?(callback_path)
end
|
#on_path?(path) ⇒ Boolean
221
222
223
|
# File 'lib/omniauth/strategy.rb', line 221
def on_path?(path)
current_path.casecmp(path) == 0
end
|
#on_request_path? ⇒ Boolean
213
214
215
|
# File 'lib/omniauth/strategy.rb', line 213
def on_request_path?
on_path?(request_path)
end
|
#options_call ⇒ Object
Responds to an OPTIONS request.
172
173
174
175
|
# File 'lib/omniauth/strategy.rb', line 172
def options_call
verbs = OmniAuth.config.allowed_request_methods.map(&:to_s).map(&:upcase).join(', ')
return [ 200, { 'Allow' => verbs }, [] ]
end
|
#options_request? ⇒ Boolean
225
226
227
|
# File 'lib/omniauth/strategy.rb', line 225
def options_request?
request.request_method == 'OPTIONS'
end
|
#path_prefix ⇒ Object
335
336
337
|
# File 'lib/omniauth/strategy.rb', line 335
def path_prefix
options[:path_prefix] || OmniAuth.config.path_prefix
end
|
#query_string ⇒ Object
355
356
357
|
# File 'lib/omniauth/strategy.rb', line 355
def query_string
request.query_string.empty? ? "" : "?#{request.query_string}"
end
|
#redirect(uri) ⇒ Object
407
408
409
410
411
412
413
414
415
416
417
418
|
# File 'lib/omniauth/strategy.rb', line 407
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
|
#request ⇒ Object
399
400
401
|
# File 'lib/omniauth/strategy.rb', line 399
def request
@request ||= Rack::Request.new(@env)
end
|
#request_call ⇒ Object
Performs the steps necessary to run the request phase of a strategy.
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/omniauth/strategy.rb', line 178
def request_call
setup_phase
session['omniauth.params'] = request.params
if options.form.respond_to?(:call)
options.form.call(env)
elsif options.form
call_app!
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_path ⇒ Object
339
340
341
|
# File 'lib/omniauth/strategy.rb', line 339
def request_path
options[:request_path] || "#{path_prefix}/#{name}"
end
|
#request_phase ⇒ Object
This method is abstract.
This method is called when the user is on the request path. You should
perform any information gathering you need to be able to authenticate
the user in this phase.
282
283
284
|
# File 'lib/omniauth/strategy.rb', line 282
def request_phase
raise NotImplementedError
end
|
#script_name ⇒ Object
391
392
393
|
# File 'lib/omniauth/strategy.rb', line 391
def script_name
@env['SCRIPT_NAME'] || ''
end
|
#session ⇒ Object
395
396
397
|
# File 'lib/omniauth/strategy.rb', line 395
def session
@env['rack.session']
end
|
#setup_path ⇒ Object
347
348
349
|
# File 'lib/omniauth/strategy.rb', line 347
def setup_path
options[:setup_path] || "#{path_prefix}/#{name}/setup"
end
|
#setup_phase ⇒ Object
The setup phase looks for the :setup
option to exist and,
if it is, will call either the Rack endpoint supplied to the
:setup
option or it will call out to the setup path of the
underlying application. This will default to /auth/:provider/setup
.
270
271
272
273
274
275
276
277
|
# File 'lib/omniauth/strategy.rb', line 270
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
|
#skip_info? ⇒ Boolean
Determines whether or not user info should be retrieved. This
allows some strategies to save a call to an external API service
for existing users. You can use it either by setting the :skip_info
to true or by setting :skip_info
to a Proc that takes a uid and
evaluates to true when you would like to skip info.
319
320
321
322
323
324
325
326
327
328
|
# File 'lib/omniauth/strategy.rb', line 319
def skip_info?
if options.skip_info?
if options.skip_info.respond_to?(:call)
return options.skip_info.call(uid)
else
return true
end
end
false
end
|
#uid ⇒ Object
286
287
288
|
# File 'lib/omniauth/strategy.rb', line 286
def uid
self.class.uid_stack(self).last
end
|
#user_info ⇒ Object
420
|
# File 'lib/omniauth/strategy.rb', line 420
def user_info; {} end
|