omniauth 1.1.4 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of omniauth might be problematic. Click here for more details.

@@ -6,7 +6,9 @@ module OmniAuth
6
6
  # is able to provide into the InfoHash (stored as the `'info'`
7
7
  # key).
8
8
  class AuthHash < Hashie::Mash
9
- def self.subkey_class; Hashie::Mash end
9
+ def self.subkey_class
10
+ Hashie::Mash
11
+ end
10
12
 
11
13
  # Tells you if this is considered to be a valid
12
14
  # OmniAuth AuthHash. The requirements for that
@@ -25,7 +27,9 @@ module OmniAuth
25
27
  end
26
28
 
27
29
  class InfoHash < Hashie::Mash
28
- def self.subkey_class; Hashie::Mash end
30
+ def self.subkey_class
31
+ Hashie::Mash
32
+ end
29
33
 
30
34
  def name
31
35
  return self[:name] if self[:name]
@@ -35,11 +39,10 @@ module OmniAuth
35
39
  nil
36
40
  end
37
41
 
38
- def name?; !!name end
39
-
40
- def valid?
41
- name?
42
+ def name?
43
+ !!name
42
44
  end
45
+ alias_method :valid?, :name?
43
46
 
44
47
  def to_hash
45
48
  hash = super
@@ -1,5 +1,3 @@
1
- require 'omniauth'
2
-
3
1
  module OmniAuth
4
2
  class Builder < ::Rack::Builder
5
3
  def initialize(app, &block)
@@ -21,6 +19,18 @@ module OmniAuth
21
19
  OmniAuth.config.on_failure = block
22
20
  end
23
21
 
22
+ def before_options_phase(&block)
23
+ OmniAuth.config.before_options_phase = block
24
+ end
25
+
26
+ def before_request_phase(&block)
27
+ OmniAuth.config.before_request_phase = block
28
+ end
29
+
30
+ def before_callback_phase(&block)
31
+ OmniAuth.config.before_callback_phase = block
32
+ end
33
+
24
34
  def configure(&block)
25
35
  OmniAuth.configure(&block)
26
36
  end
@@ -17,27 +17,27 @@ module OmniAuth
17
17
  end
18
18
 
19
19
  def call
20
- raise_out! if ENV['RACK_ENV'].to_s == 'development'
20
+ raise_out! if OmniAuth.config.failure_raise_out_environments.include?(ENV['RACK_ENV'].to_s)
21
21
  redirect_to_failure
22
22
  end
23
23
 
24
24
  def raise_out!
25
- raise env['omniauth.error'] || OmniAuth::Error.new(env['omniauth.error.type'])
25
+ fail(env['omniauth.error'] || OmniAuth::Error.new(env['omniauth.error.type']))
26
26
  end
27
27
 
28
28
  def redirect_to_failure
29
29
  message_key = env['omniauth.error.type']
30
30
  new_path = "#{env['SCRIPT_NAME']}#{OmniAuth.config.path_prefix}/failure?message=#{message_key}#{origin_query_param}#{strategy_name_query_param}"
31
- Rack::Response.new(["302 Moved"], 302, 'Location' => new_path).finish
31
+ Rack::Response.new(['302 Moved'], 302, 'Location' => new_path).finish
32
32
  end
33
33
 
34
34
  def strategy_name_query_param
35
- return "" unless env['omniauth.error.strategy']
35
+ return '' unless env['omniauth.error.strategy']
36
36
  "&strategy=#{env['omniauth.error.strategy'].name}"
37
37
  end
38
38
 
39
39
  def origin_query_param
40
- return "" unless env['omniauth.origin']
40
+ return '' unless env['omniauth.origin']
41
41
  "&origin=#{Rack::Utils.escape(env['omniauth.origin'])}"
42
42
  end
43
43
  end
@@ -0,0 +1,81 @@
1
+ body {
2
+ background: #ccc;
3
+ font-family: "Lucida Grande", "Lucida Sans", Helvetica, Arial, sans-serif;
4
+ }
5
+
6
+ h1 {
7
+ text-align: center;
8
+ margin: 30px auto 0px;
9
+ font-size: 18px;
10
+ padding: 10px 10px 15px;
11
+ background: #555;
12
+ color: white;
13
+ width: 320px;
14
+ border: 10px solid #444;
15
+ border-bottom: 0;
16
+ -moz-border-radius-topleft: 10px;
17
+ -moz-border-radius-topright: 10px;
18
+ -webkit-border-top-left-radius: 10px;
19
+ -webkit-border-top-right-radius: 10px;
20
+ border-top-left-radius: 10px;
21
+ border-top-right-radius: 10px;
22
+ }
23
+
24
+ h1, form {
25
+ -moz-box-shadow: 2px 2px 7px rgba(0,0,0,0.3);
26
+ -webkit-box-shadow: 2px 2px 7px rgba(0,0,0,0.3);
27
+ }
28
+
29
+ form {
30
+ background: white;
31
+ border: 10px solid #eee;
32
+ border-top: 0;
33
+ padding: 20px;
34
+ margin: 0px auto 40px;
35
+ width: 300px;
36
+ -moz-border-radius-bottomleft: 10px;
37
+ -moz-border-radius-bottomright: 10px;
38
+ -webkit-border-bottom-left-radius: 10px;
39
+ -webkit-border-bottom-right-radius: 10px;
40
+ border-bottom-left-radius: 10px;
41
+ border-bottom-right-radius: 10px;
42
+ }
43
+
44
+ label {
45
+ display: block;
46
+ font-weight: bold;
47
+ margin-bottom: 5px;
48
+ }
49
+
50
+ input {
51
+ font-size: 18px;
52
+ padding: 4px 8px;
53
+ display: block;
54
+ margin-bottom: 10px;
55
+ width: 280px;
56
+ }
57
+
58
+ input#identifier, input#openid_url {
59
+ background: url(http://openid.net/login-bg.gif) no-repeat;
60
+ background-position: 0 50%;
61
+ padding-left: 18px;
62
+ }
63
+
64
+ button {
65
+ font-size: 22px;
66
+ padding: 4px 8px;
67
+ display: block;
68
+ margin: 20px auto 0;
69
+ }
70
+
71
+ fieldset {
72
+ border: 1px solid #ccc;
73
+ border-left: 0;
74
+ border-right: 0;
75
+ padding: 10px 0;
76
+ }
77
+
78
+ fieldset input {
79
+ width: 260px;
80
+ font-size: 16px;
81
+ }
@@ -1,105 +1,21 @@
1
- require 'omniauth'
2
-
3
1
  module OmniAuth
4
- class Form
5
- DEFAULT_CSS = <<-CSS
6
- body {
7
- background: #ccc;
8
- font-family: "Lucida Grande", "Lucida Sans", Helvetica, Arial, sans-serif;
9
- }
10
-
11
- h1 {
12
- text-align: center;
13
- margin: 30px auto 0px;
14
- font-size: 18px;
15
- padding: 10px 10px 15px;
16
- background: #555;
17
- color: white;
18
- width: 320px;
19
- border: 10px solid #444;
20
- border-bottom: 0;
21
- -moz-border-radius-topleft: 10px;
22
- -moz-border-radius-topright: 10px;
23
- -webkit-border-top-left-radius: 10px;
24
- -webkit-border-top-right-radius: 10px;
25
- border-top-left-radius: 10px;
26
- border-top-right-radius: 10px;
27
- }
28
-
29
- h1, form {
30
- -moz-box-shadow: 2px 2px 7px rgba(0,0,0,0.3);
31
- -webkit-box-shadow: 2px 2px 7px rgba(0,0,0,0.3);
32
- }
33
-
34
- form {
35
- background: white;
36
- border: 10px solid #eee;
37
- border-top: 0;
38
- padding: 20px;
39
- margin: 0px auto 40px;
40
- width: 300px;
41
- -moz-border-radius-bottomleft: 10px;
42
- -moz-border-radius-bottomright: 10px;
43
- -webkit-border-bottom-left-radius: 10px;
44
- -webkit-border-bottom-right-radius: 10px;
45
- border-bottom-left-radius: 10px;
46
- border-bottom-right-radius: 10px;
47
- }
48
-
49
- label {
50
- display: block;
51
- font-weight: bold;
52
- margin-bottom: 5px;
53
- }
54
-
55
- input {
56
- font-size: 18px;
57
- padding: 4px 8px;
58
- display: block;
59
- margin-bottom: 10px;
60
- width: 280px;
61
- }
62
-
63
- input#identifier, input#openid_url {
64
- background: url(http://openid.net/login-bg.gif) no-repeat;
65
- background-position: 0 50%;
66
- padding-left: 18px;
67
- }
68
-
69
- button {
70
- font-size: 22px;
71
- padding: 4px 8px;
72
- display: block;
73
- margin: 20px auto 0;
74
- }
75
-
76
- fieldset {
77
- border: 1px solid #ccc;
78
- border-left: 0;
79
- border-right: 0;
80
- padding: 10px 0;
81
- }
82
-
83
- fieldset input {
84
- width: 260px;
85
- font-size: 16px;
86
- }
87
- CSS
2
+ class Form # rubocop:disable ClassLength
3
+ DEFAULT_CSS = File.read(File.expand_path('../form.css', __FILE__))
88
4
 
89
5
  attr_accessor :options
90
6
 
91
7
  def initialize(options = {})
92
- options[:title] ||= "Authentication Info Required"
93
- options[:header_info] ||= ""
8
+ options[:title] ||= 'Authentication Info Required'
9
+ options[:header_info] ||= ''
94
10
  self.options = options
95
11
 
96
- @html = ""
12
+ @html = ''
97
13
  @with_custom_button = false
98
14
  @footer = nil
99
- header(options[:title],options[:header_info])
15
+ header(options[:title], options[:header_info])
100
16
  end
101
17
 
102
- def self.build(options = {},&block)
18
+ def self.build(options = {}, &block)
103
19
  form = OmniAuth::Form.new(options)
104
20
  if block.arity > 0
105
21
  yield form
@@ -142,12 +58,12 @@ module OmniAuth
142
58
 
143
59
  def fieldset(legend, options = {}, &block)
144
60
  @html << "\n<fieldset#{" style='#{options[:style]}'" if options[:style]}#{" id='#{options[:id]}'" if options[:id]}>\n <legend>#{legend}</legend>\n"
145
- self.instance_eval(&block)
61
+ instance_eval(&block)
146
62
  @html << "\n</fieldset>"
147
63
  self
148
64
  end
149
65
 
150
- def header(title,header_info)
66
+ def header(title, header_info)
151
67
  @html << <<-HTML
152
68
  <!DOCTYPE html>
153
69
  <html>
@@ -183,10 +99,10 @@ module OmniAuth
183
99
 
184
100
  def to_response
185
101
  footer
186
- Rack::Response.new(@html, 200, {"content-type" => "text/html"}).finish
102
+ Rack::Response.new(@html, 200, 'content-type' => 'text/html').finish
187
103
  end
188
104
 
189
- protected
105
+ protected
190
106
 
191
107
  def css
192
108
  "\n<style type='text/css'>#{OmniAuth.config.form_css}</style>"
@@ -1,5 +1,3 @@
1
- require 'omniauth'
2
-
3
1
  module OmniAuth
4
2
  module Strategies
5
3
  # The Developer strategy is a very simple strategy that can be used as a
@@ -37,11 +35,11 @@ module OmniAuth
37
35
  option :uid_field, :email
38
36
 
39
37
  def request_phase
40
- form = OmniAuth::Form.new(:title => "User Info", :url => callback_path)
38
+ form = OmniAuth::Form.new(:title => 'User Info', :url => callback_path)
41
39
  options.fields.each do |field|
42
- form.text_field field.to_s.capitalize.gsub("_", " "), field.to_s
40
+ form.text_field field.to_s.capitalize.gsub('_', ' '), field.to_s
43
41
  end
44
- form.button "Sign In"
42
+ form.button 'Sign In'
45
43
  form.to_response
46
44
  end
47
45
 
@@ -1,4 +1,3 @@
1
- require 'omniauth'
2
1
  require 'hashie/mash'
3
2
 
4
3
  module OmniAuth
@@ -85,7 +84,7 @@ module OmniAuth
85
84
  return
86
85
  end
87
86
  existing = superclass.respond_to?(:args) ? superclass.args : []
88
- return (instance_variable_defined?(:@args) && @args) || existing
87
+ (instance_variable_defined?(:@args) && @args) || existing
89
88
  end
90
89
 
91
90
  %w(uid info extra credentials).each do |fetcher|
@@ -136,11 +135,12 @@ module OmniAuth
136
135
  options.name ||= self.class.to_s.split('::').last.downcase
137
136
 
138
137
  self.class.args.each do |arg|
138
+ break if args.empty?
139
139
  options[arg] = args.shift
140
140
  end
141
141
 
142
142
  # Make sure that all of the args have been dealt with, otherwise error out.
143
- raise ArgumentError, "Received wrong number of arguments. #{args.inspect}" unless args.empty?
143
+ fail(ArgumentError, "Received wrong number of arguments. #{args.inspect}") unless args.empty?
144
144
 
145
145
  yield options if block_given?
146
146
  end
@@ -169,14 +169,16 @@ module OmniAuth
169
169
  # the request path is recognized.
170
170
  #
171
171
  # @param env [Hash] The Rack environment.
172
- def call!(env)
173
- raise OmniAuth::NoSessionError.new("You must provide a session to use OmniAuth.") unless env['rack.session']
172
+ def call!(env) # rubocop:disable CyclomaticComplexity
173
+ unless env['rack.session']
174
+ error = OmniAuth::NoSessionError.new('You must provide a session to use OmniAuth.')
175
+ fail(error)
176
+ end
174
177
 
175
178
  @env = env
176
179
  @env['omniauth.strategy'] = self if on_auth_path?
177
180
 
178
181
  return mock_call!(env) if OmniAuth.config.test_mode
179
-
180
182
  return options_call if on_auth_path? && options_request?
181
183
  return request_call if on_request_path? && OmniAuth.config.allowed_request_methods.include?(request.request_method.downcase.to_sym)
182
184
  return callback_call if on_callback_path?
@@ -186,24 +188,23 @@ module OmniAuth
186
188
 
187
189
  # Responds to an OPTIONS request.
188
190
  def options_call
189
- verbs = OmniAuth.config.allowed_request_methods.map(&:to_s).map(&:upcase).join(', ')
190
- return [ 200, { 'Allow' => verbs }, [] ]
191
+ OmniAuth.config.before_options_phase.call(env) if OmniAuth.config.before_options_phase
192
+ verbs = OmniAuth.config.allowed_request_methods.collect(&:to_s).collect(&:upcase).join(', ')
193
+ [200, {'Allow' => verbs}, []]
191
194
  end
192
195
 
193
196
  # Performs the steps necessary to run the request phase of a strategy.
194
- def request_call
197
+ def request_call # rubocop:disable CyclomaticComplexity, MethodLength
195
198
  setup_phase
196
-
197
- log :info, "Request phase initiated."
198
-
199
- #store query params from the request url, extracted in the callback_phase
199
+ log :info, 'Request phase initiated.'
200
+ # store query params from the request url, extracted in the callback_phase
200
201
  session['omniauth.params'] = request.params
201
-
202
+ OmniAuth.config.before_request_phase.call(env) if OmniAuth.config.before_request_phase
202
203
  if options.form.respond_to?(:call)
203
- log :info, "Rendering form from supplied Rack endpoint."
204
+ log :info, 'Rendering form from supplied Rack endpoint.'
204
205
  options.form.call(env)
205
206
  elsif options.form
206
- log :info, "Rendering form from underlying application."
207
+ log :info, 'Rendering form from underlying application.'
207
208
  call_app!
208
209
  else
209
210
  if request.params['origin']
@@ -218,11 +219,11 @@ module OmniAuth
218
219
  # Performs the steps necessary to run the callback phase of a strategy.
219
220
  def callback_call
220
221
  setup_phase
221
-
222
- log :info, "Callback phase initiated."
222
+ log :info, 'Callback phase initiated.'
223
223
  @env['omniauth.origin'] = session.delete('omniauth.origin')
224
224
  @env['omniauth.origin'] = nil if env['omniauth.origin'] == ''
225
225
  @env['omniauth.params'] = session.delete('omniauth.params') || {}
226
+ OmniAuth.config.before_callback_phase.call(@env) if OmniAuth.config.before_callback_phase
226
227
  callback_phase
227
228
  end
228
229
 
@@ -241,11 +242,7 @@ module OmniAuth
241
242
  end
242
243
 
243
244
  def on_callback_path?
244
- if options.callback_path.respond_to?(:call)
245
- options.callback_path.call(env)
246
- else
247
- on_path?(callback_path)
248
- end
245
+ on_path?(callback_path)
249
246
  end
250
247
 
251
248
  def on_path?(path)
@@ -269,13 +266,14 @@ module OmniAuth
269
266
  setup_phase
270
267
 
271
268
  session['omniauth.params'] = request.params
272
-
269
+ OmniAuth.config.before_request_phase.call(env) if OmniAuth.config.before_request_phase
273
270
  if request.params['origin']
274
271
  @env['rack.session']['omniauth.origin'] = request.params['origin']
275
272
  elsif env['HTTP_REFERER'] && !env['HTTP_REFERER'].match(/#{request_path}$/)
276
273
  @env['rack.session']['omniauth.origin'] = env['HTTP_REFERER']
277
274
  end
278
- redirect(script_name + callback_path + query_string)
275
+
276
+ redirect(callback_url)
279
277
  end
280
278
 
281
279
  def mock_callback_call
@@ -288,6 +286,7 @@ module OmniAuth
288
286
  @env['omniauth.params'] = session.delete('omniauth.params') || {}
289
287
  @env['omniauth.origin'] = session.delete('omniauth.origin')
290
288
  @env['omniauth.origin'] = nil if env['omniauth.origin'] == ''
289
+ OmniAuth.config.before_callback_phase.call(@env) if OmniAuth.config.before_callback_phase
291
290
  call_app!
292
291
  end
293
292
  end
@@ -298,10 +297,10 @@ module OmniAuth
298
297
  # underlying application. This will default to `/auth/:provider/setup`.
299
298
  def setup_phase
300
299
  if options[:setup].respond_to?(:call)
301
- log :info, "Setup endpoint detected, running now."
300
+ log :info, 'Setup endpoint detected, running now.'
302
301
  options[:setup].call(env)
303
302
  elsif options.setup?
304
- log :info, "Calling through to underlying application for setup."
303
+ log :info, 'Calling through to underlying application for setup.'
305
304
  setup_env = env.merge('PATH_INFO' => setup_path, 'REQUEST_METHOD' => 'GET')
306
305
  call_app!(setup_env)
307
306
  end
@@ -311,7 +310,7 @@ module OmniAuth
311
310
  # perform any information gathering you need to be able to authenticate
312
311
  # the user in this phase.
313
312
  def request_phase
314
- raise NotImplementedError
313
+ fail(NotImplementedError)
315
314
  end
316
315
 
317
316
  def uid
@@ -359,7 +358,7 @@ module OmniAuth
359
358
  end
360
359
 
361
360
  def callback_phase
362
- self.env['omniauth.auth'] = auth_hash
361
+ env['omniauth.auth'] = auth_hash
363
362
  call_app!
364
363
  end
365
364
 
@@ -382,7 +381,11 @@ module OmniAuth
382
381
  end
383
382
 
384
383
  def callback_path
385
- options[:callback_path].is_a?(String) ? options[:callback_path] : (custom_path(:request_path) || "#{path_prefix}/#{name}/callback")
384
+ path = options[:callback_path] if options[:callback_path].is_a?(String)
385
+ path ||= current_path if options[:callback_path].respond_to?(:call) && options[:callback_path].call(env)
386
+ path ||= custom_path(:request_path)
387
+ path ||= "#{path_prefix}/#{name}/callback"
388
+ path
386
389
  end
387
390
 
388
391
  def setup_path
@@ -390,11 +393,11 @@ module OmniAuth
390
393
  end
391
394
 
392
395
  def current_path
393
- request.path_info.downcase.sub(/\/$/,'')
396
+ request.path_info.downcase.sub(/\/$/, '')
394
397
  end
395
398
 
396
399
  def query_string
397
- request.query_string.empty? ? "" : "?#{request.query_string}"
400
+ request.query_string.empty? ? '' : "?#{request.query_string}"
398
401
  end
399
402
 
400
403
  def call_app!(env = @env)
@@ -403,16 +406,21 @@ module OmniAuth
403
406
 
404
407
  def full_host
405
408
  case OmniAuth.config.full_host
406
- when String
407
- OmniAuth.config.full_host
408
- when Proc
409
- OmniAuth.config.full_host.call(env)
410
- else
411
- uri = URI.parse(request.url.gsub(/\?.*$/,''))
409
+ when String
410
+ OmniAuth.config.full_host
411
+ when Proc
412
+ OmniAuth.config.full_host.call(env)
413
+ else
414
+ # in Rack 1.3.x, request.url explodes if scheme is nil
415
+ if request.scheme && request.url.match(URI::ABS_URI)
416
+ uri = URI.parse(request.url.gsub(/\?.*$/, ''))
412
417
  uri.path = ''
413
- #sometimes the url is actually showing http inside rails because the other layers (like nginx) have handled the ssl termination.
414
- uri.scheme = 'https' if ssl?
418
+ # sometimes the url is actually showing http inside rails because the
419
+ # other layers (like nginx) have handled the ssl termination.
420
+ uri.scheme = 'https' if ssl? # rubocop:disable BlockNesting
415
421
  uri.to_s
422
+ else ''
423
+ end
416
424
  end
417
425
  end
418
426
 
@@ -449,12 +457,14 @@ module OmniAuth
449
457
  r.finish
450
458
  end
451
459
 
452
- def user_info; {} end
460
+ def user_info
461
+ {}
462
+ end
453
463
 
454
464
  def fail!(message_key, exception = nil)
455
- self.env['omniauth.error'] = exception
456
- self.env['omniauth.error.type'] = message_key.to_sym
457
- self.env['omniauth.error.strategy'] = self
465
+ env['omniauth.error'] = exception
466
+ env['omniauth.error.type'] = message_key.to_sym
467
+ env['omniauth.error.strategy'] = self
458
468
 
459
469
  if exception
460
470
  log :error, "Authentication failure! #{message_key}: #{exception.class.to_s}, #{exception.message}"
@@ -462,16 +472,20 @@ module OmniAuth
462
472
  log :error, "Authentication failure! #{message_key} encountered."
463
473
  end
464
474
 
465
- OmniAuth.config.on_failure.call(self.env)
475
+ OmniAuth.config.on_failure.call(env)
466
476
  end
467
477
 
468
478
  class Options < Hashie::Mash; end
469
479
 
470
- protected
480
+ protected
471
481
 
472
482
  def merge_stack(stack)
473
- stack.inject({}){|c,h| c.merge!(h); c}
483
+ stack.inject({}) do |a, e|
484
+ a.merge!(e)
485
+ a
486
+ end
474
487
  end
488
+
475
489
  def ssl?
476
490
  request.env['HTTPS'] == 'on' ||
477
491
  request.env['HTTP_X_FORWARDED_SSL'] == 'on' ||