warden 1.2.3 → 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c615ae4a3d5433cece12ea5d28a93e8f5ed3f8ff
4
+ data.tar.gz: fc9bae1ddd9b9872aa890235d905147ef7898b72
5
+ SHA512:
6
+ metadata.gz: c16efdf510c03e0f5455c496044f81b050d9f8887cc88dbb656ea920a01d722bd1557e824f34f107e7dcee4f9d8dbffbbeb3e37d4d77217f65cbe2d822a016c2
7
+ data.tar.gz: f83439dd79f0edc6b822750838a131c0f64c772a1f038f0ae371e2362c0ff401e5ef47f4e184390422c3e53f25a484cc64e86c8b01e26733c51f1f4b0d8df4cf
data/Gemfile CHANGED
@@ -6,6 +6,6 @@ gem 'rake'
6
6
  gem 'rack', '1.3'
7
7
 
8
8
  group :test do
9
- gem 'rspec', '~>2'
9
+ gem 'rspec', '~>3'
10
10
  gem 'rack-test'
11
11
  end
@@ -1,9 +1,8 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Warden
4
- # This is a class which is yielded on use Warden::Manager. If you have a plugin
5
- # and wants to add more configuration to warden, you just need to extend this
6
- # class.
4
+ # This class is yielded inside Warden::Manager. If you have a plugin and want to
5
+ # add more configuration to warden, you just need to extend this class.
7
6
  class Config < Hash
8
7
  # Creates an accessor that simply sets and reads a key in the hash:
9
8
  #
@@ -18,21 +18,21 @@ module Warden
18
18
  # A callback hook set to run every time after a user is set.
19
19
  # This callback is triggered the first time one of those three events happens
20
20
  # during a request: :authentication, :fetch (from session) and :set_user (when manually set).
21
- # You can supply as many hooks as you like, and they will be run in order of decleration.
21
+ # You can supply as many hooks as you like, and they will be run in order of declaration.
22
22
  #
23
23
  # If you want to run the callbacks for a given scope and/or event, you can specify them as options.
24
24
  # See parameters and example below.
25
25
  #
26
26
  # Parameters:
27
27
  # <options> Some options which specify when the callback should be executed
28
- # scope - Executes the callback only if it maches the scope(s) given
28
+ # scope - Executes the callback only if it matches the scope(s) given
29
29
  # only - Executes the callback only if it matches the event(s) given
30
30
  # except - Executes the callback except if it matches the event(s) given
31
31
  # <block> A block where you can set arbitrary logic to run every time a user is set
32
32
  # Block Parameters: |user, auth, opts|
33
33
  # user - The user object that is being set
34
34
  # auth - The raw authentication proxy object.
35
- # opts - any options passed into the set_user call includeing :scope
35
+ # opts - any options passed into the set_user call including :scope
36
36
  #
37
37
  # Example:
38
38
  # Warden::Manager.after_set_user do |user,auth,opts|
@@ -77,7 +77,7 @@ module Warden
77
77
  end
78
78
 
79
79
  # after_fetch is just a wrapper to after_set_user, which is only invoked
80
- # when the user is fetched from sesion. The options and yielded arguments
80
+ # when the user is fetched from session. The options and yielded arguments
81
81
  # are the same as in after_set_user.
82
82
  #
83
83
  # :api: public
@@ -85,18 +85,18 @@ module Warden
85
85
  after_set_user(options.merge(:event => :fetch), method, &block)
86
86
  end
87
87
 
88
- # A callback that runs just prior to the failur application being called.
88
+ # A callback that runs just prior to the failure application being called.
89
89
  # This callback occurs after PATH_INFO has been modified for the failure (default /unauthenticated)
90
90
  # In this callback you can mutate the environment as required by the failure application
91
91
  # If a Rails controller were used for the failure_app for example, you would need to set request[:params][:action] = :unauthenticated
92
92
  #
93
93
  # Parameters:
94
94
  # <options> Some options which specify when the callback should be executed
95
- # scope - Executes the callback only if it maches the scope(s) given
95
+ # scope - Executes the callback only if it matches the scope(s) given
96
96
  # <block> A block to contain logic for the callback
97
97
  # Block Parameters: |env, opts|
98
98
  # env - The rack env hash
99
- # opts - any options passed into the authenticate call includeing :scope
99
+ # opts - any options passed into the authenticate call including :scope
100
100
  #
101
101
  # Example:
102
102
  # Warden::Manager.before_failure do |env, opts|
@@ -121,7 +121,7 @@ module Warden
121
121
  #
122
122
  # Parameters:
123
123
  # <options> Some options which specify when the callback should be executed
124
- # scope - Executes the callback only if it maches the scope(s) given
124
+ # scope - Executes the callback only if it matches the scope(s) given
125
125
  # <block> A block to contain logic for the callback
126
126
  # Block Parameters: |user, auth, scope|
127
127
  # user - The authenticated user for the current scope
@@ -149,7 +149,7 @@ module Warden
149
149
  #
150
150
  # Parameters:
151
151
  # <options> Some options which specify when the callback should be executed
152
- # scope - Executes the callback only if it maches the scope(s) given
152
+ # scope - Executes the callback only if it matches the scope(s) given
153
153
  # <block> A block to contain logic for the callback
154
154
  # Block Parameters: |user, auth, scope|
155
155
  # user - The authenticated user for the current scope
@@ -4,7 +4,7 @@ require 'warden/config'
4
4
 
5
5
  module Warden
6
6
  # The middleware for Rack Authentication
7
- # The middlware requires that there is a session upstream
7
+ # The middleware requires that there is a session upstream
8
8
  # The middleware injects an authentication object into
9
9
  # the rack environment hash
10
10
  class Manager
@@ -19,7 +19,7 @@ module Warden
19
19
  default_strategies = options.delete(:default_strategies)
20
20
 
21
21
  @app, @config = app, Warden::Config.new(options)
22
- @config.default_strategies *default_strategies if default_strategies
22
+ @config.default_strategies(*default_strategies) if default_strategies
23
23
  yield @config if block_given?
24
24
  self
25
25
  end
@@ -38,13 +38,11 @@ module Warden
38
38
  result ||= {}
39
39
  case result
40
40
  when Array
41
- if result.first == 401 && intercept_401?(env)
42
- process_unauthenticated(env)
43
- else
44
- result
45
- end
41
+ handle_chain_result(result.first, result, env)
46
42
  when Hash
47
43
  process_unauthenticated(env, result)
44
+ when Rack::Response
45
+ handle_chain_result(result.status, result, env)
48
46
  end
49
47
  end
50
48
 
@@ -72,8 +70,8 @@ module Warden
72
70
  Warden::SessionSerializer.send :define_method, method_name, &block
73
71
  end
74
72
 
75
- # Reconstitues the user from the session.
76
- # Use the results of user_session_key to reconstitue the user from the session on requests after the initial login
73
+ # Reconstitutes the user from the session.
74
+ # Use the results of user_session_key to reconstitute the user from the session on requests after the initial login
77
75
  # You can supply different methods of de-serialization for different scopes by passing a scope symbol
78
76
  #
79
77
  # Example:
@@ -84,12 +82,25 @@ module Warden
84
82
  # :api: public
85
83
  def serialize_from_session(scope = nil, &block)
86
84
  method_name = scope.nil? ? :deserialize : "#{scope}_deserialize"
85
+
86
+ if Warden::SessionSerializer.method_defined? method_name
87
+ Warden::SessionSerializer.send :remove_method, method_name
88
+ end
89
+
87
90
  Warden::SessionSerializer.send :define_method, method_name, &block
88
91
  end
89
92
  end
90
93
 
91
94
  private
92
95
 
96
+ def handle_chain_result(status, result, env)
97
+ if status == 401 && intercept_401?(env)
98
+ process_unauthenticated(env)
99
+ else
100
+ result
101
+ end
102
+ end
103
+
93
104
  def intercept_401?(env)
94
105
  config[:intercept_401] && !env['warden'].custom_failure?
95
106
  end
@@ -113,6 +124,7 @@ module Warden
113
124
  when :custom
114
125
  proxy.custom_response
115
126
  else
127
+ options[:message] ||= proxy.message
116
128
  call_failure_app(env, options)
117
129
  end
118
130
  end
@@ -3,7 +3,7 @@ module Warden
3
3
  module Mixins
4
4
  module Common
5
5
 
6
- # Convinience method to access the session
6
+ # Convenience method to access the session
7
7
  # :api: public
8
8
  def session
9
9
  env['rack.session']
@@ -69,7 +69,7 @@ module Warden
69
69
  #
70
70
  # :api: public
71
71
  def clear_strategies_cache!(*args)
72
- scope, opts = _retrieve_scope_and_opts(args)
72
+ scope, _opts = _retrieve_scope_and_opts(args)
73
73
 
74
74
  @winning_strategies.delete(scope)
75
75
  @strategies[scope].each do |k, v|
@@ -87,7 +87,7 @@ module Warden
87
87
  @locked = true
88
88
  end
89
89
 
90
- # Run the authentiation strategies for the given strategies.
90
+ # Run the authentication strategies for the given strategies.
91
91
  # If there is already a user logged in for a given scope, the strategies are not run
92
92
  # This does not halt the flow of control and is a passive attempt to authenticate only
93
93
  # When scope is not specified, the default_scope is assumed.
@@ -101,7 +101,7 @@ module Warden
101
101
  #
102
102
  # :api: public
103
103
  def authenticate(*args)
104
- user, opts = _perform_authentication(*args)
104
+ user, _opts = _perform_authentication(*args)
105
105
  user
106
106
  end
107
107
 
@@ -181,7 +181,7 @@ module Warden
181
181
  @users[scope]
182
182
  end
183
183
 
184
- # Provides acccess to the user object in a given scope for a request.
184
+ # Provides access to the user object in a given scope for a request.
185
185
  # Will be nil if not logged in. Please notice that this method does not
186
186
  # perform strategies.
187
187
  #
@@ -280,7 +280,7 @@ module Warden
280
280
  winning_strategy && winning_strategy.message
281
281
  end
282
282
 
283
- # Provides a way to return a 401 without warden defering to the failure app
283
+ # Provides a way to return a 401 without warden deferring to the failure app
284
284
  # The result is a direct passthrough of your own response
285
285
  # :api: public
286
286
  def custom_failure!
@@ -290,7 +290,11 @@ module Warden
290
290
  # Check to see if the custom failure flag has been set
291
291
  # :api: public
292
292
  def custom_failure?
293
- !!@custom_failure
293
+ if instance_variable_defined?(:@custom_failure)
294
+ !!@custom_failure
295
+ else
296
+ false
297
+ end
294
298
  end
295
299
 
296
300
  # Check to see if this is an asset request
@@ -318,7 +322,7 @@ module Warden
318
322
  return user, opts if user = user(opts.merge(:scope => scope))
319
323
  _run_strategies_for(scope, args)
320
324
 
321
- if winning_strategy && winning_strategy.user
325
+ if winning_strategy && winning_strategy.successful?
322
326
  opts[:store] = opts.fetch(:store, winning_strategy.store?)
323
327
  set_user(winning_strategy.user, opts.merge!(:event => :authentication))
324
328
  end
@@ -356,7 +360,7 @@ module Warden
356
360
  end
357
361
  end
358
362
 
359
- # Fetchs strategies and keep them in a hash cache.
363
+ # Fetches strategies and keep them in a hash cache.
360
364
  def _fetch_strategy(name, scope)
361
365
  @strategies[scope][name] ||= if klass = Warden::Strategies[name]
362
366
  klass.new(@env, scope)
@@ -9,11 +9,11 @@ module Warden
9
9
  # You _may_ provide a @valid?@ method.
10
10
  # The valid method should return true or false depending on if the strategy is a valid one for the request.
11
11
  #
12
- # The parameters for Warden::Strategies.add method is:
12
+ # The parameters for Warden::Strategies.add method are:
13
13
  # <label: Symbol> The label is the name given to a strategy. Use the label to refer to the strategy when authenticating
14
- # <strategy: Class|nil> The optional stragtegy argument if set _must_ be a class that inherits from Warden::Strategies::Base and _must_
14
+ # <strategy: Class|nil> The optional strategy argument if set _must_ be a class that inherits from Warden::Strategies::Base and _must_
15
15
  # implement an @authenticate!@ method
16
- # <block> The block acts as a convinient way to declare your strategy. Inside is the class definition of a strategy.
16
+ # <block> The block acts as a convenient way to declare your strategy. Inside is the class definition of a strategy.
17
17
  #
18
18
  # Examples:
19
19
  #
@@ -108,8 +108,13 @@ module Warden
108
108
  # :api: public
109
109
  def pass; end
110
110
 
111
+ # Returns true only if the result is a success and a user was assigned.
112
+ def successful?
113
+ @result == :success && !user.nil?
114
+ end
115
+
111
116
  # Whenever you want to provide a user object as "authenticated" use the +success!+ method.
112
- # This will halt the strategy, and set the user in the approprieate scope.
117
+ # This will halt the strategy, and set the user in the appropriate scope.
113
118
  # It is the "login" method
114
119
  #
115
120
  # Parameters:
@@ -133,7 +138,7 @@ module Warden
133
138
  @result = :failure
134
139
  end
135
140
 
136
- # Casuses the strategy to fail, but not halt. The strategies will cascade after this failure and warden will check the next strategy. The last strategy to fail will have it's message displayed.
141
+ # Causes the strategy to fail, but not halt. The strategies will cascade after this failure and warden will check the next strategy. The last strategy to fail will have it's message displayed.
137
142
  # :api: public
138
143
  def fail(message = "Failed to Login")
139
144
  @message = message
@@ -144,8 +149,8 @@ module Warden
144
149
  #
145
150
  # Parameters:
146
151
  # url <String> - The string representing the URL to be redirected to
147
- # pararms <Hash> - Any parameters to encode into the URL
148
- # opts <Hash> - Any options to recirect with.
152
+ # params <Hash> - Any parameters to encode into the URL
153
+ # opts <Hash> - Any options to redirect with.
149
154
  # available options: permanent => (true || false)
150
155
  #
151
156
  # :api: public
@@ -10,7 +10,7 @@ module Warden
10
10
  ::Warden.test_mode!
11
11
  end
12
12
 
13
- # A helper method that will peform a login of a user in warden for the next request
13
+ # A helper method that will perform a login of a user in warden for the next request.
14
14
  # Provide it the same options as you would to Warden::Proxy#set_user
15
15
  # @see Warden::Proxy#set_user
16
16
  # @api public
@@ -32,7 +32,7 @@ module Warden
32
32
  _on_next_request.clear
33
33
  end
34
34
 
35
- # A containter for the on_next_request items.
35
+ # A container for the on_next_request items.
36
36
  # @api private
37
37
  def _on_next_request
38
38
  @_on_next_request ||= []
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Warden
3
- VERSION = "1.2.3".freeze
3
+ VERSION = "1.2.4".freeze
4
4
  end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+ Warden::Strategies.add(:fail_with_user) do
3
+ def authenticate!
4
+ request.env['warden.spec.strategies'] ||= []
5
+ request.env['warden.spec.strategies'] << :fail_with_user
6
+ self.user = 'Valid User'
7
+ fail!
8
+ end
9
+ end
10
+
@@ -5,4 +5,4 @@ Warden::Strategies.add(:invalid) do
5
5
  end
6
6
 
7
7
  def authenticate!; end
8
- end
8
+ end
@@ -16,16 +16,16 @@ describe "authenticated data store" do
16
16
  app = lambda do |e|
17
17
  e['warden'].authenticate(:pass)
18
18
  e['warden'].authenticate(:pass, :scope => :foo)
19
- e['warden'].should be_authenticated
20
- e['warden'].should be_authenticated(:foo)
19
+ expect(e['warden']).to be_authenticated
20
+ expect(e['warden']).to be_authenticated(:foo)
21
21
 
22
22
  # Store the data for :default
23
23
  e['warden'].session[:key] = "value"
24
24
  valid_response
25
25
  end
26
26
  setup_rack(app).call(@env)
27
- @env['rack.session']['warden.user.default.session'].should == {:key => "value"}
28
- @env['rack.session']['warden.user.foo.session'].should be_nil
27
+ expect(@env['rack.session']['warden.user.default.session']).to eq(key: "value")
28
+ expect(@env['rack.session']['warden.user.foo.session']).to be_nil
29
29
  end
30
30
 
31
31
  it "should store data for the foo user" do
@@ -34,18 +34,18 @@ describe "authenticated data store" do
34
34
  valid_response
35
35
  end
36
36
  setup_rack(app).call(@env)
37
- @env['rack.session']['warden.user.foo.session'].should == {:key => "value"}
37
+ expect(@env['rack.session']['warden.user.foo.session']).to eq(key: "value")
38
38
  end
39
39
 
40
- it "should store the data seperately" do
40
+ it "should store the data separately" do
41
41
  app = lambda do |e|
42
42
  e['warden'].session[:key] = "value"
43
43
  e['warden'].session(:foo)[:key] = "another value"
44
44
  valid_response
45
45
  end
46
46
  setup_rack(app).call(@env)
47
- @env['rack.session']['warden.user.default.session'].should == {:key => "value"}
48
- @env['rack.session']['warden.user.foo.session' ].should == {:key => "another value"}
47
+ expect(@env['rack.session']['warden.user.default.session']).to eq(key: "value")
48
+ expect(@env['rack.session']['warden.user.foo.session' ]).to eq(key: "another value")
49
49
  end
50
50
 
51
51
  it "should clear the foo scoped data when foo logs out" do
@@ -56,8 +56,8 @@ describe "authenticated data store" do
56
56
  valid_response
57
57
  end
58
58
  setup_rack(app).call(@env)
59
- @env['rack.session']['warden.user.default.session'].should == {:key => "value"}
60
- @env['rack.session']['warden.user.foo.session' ].should be_nil
59
+ expect(@env['rack.session']['warden.user.default.session']).to eq(key: "value")
60
+ expect(@env['rack.session']['warden.user.foo.session' ]).to be_nil
61
61
  end
62
62
 
63
63
  it "should clear out the default data when :default logs out" do
@@ -68,8 +68,8 @@ describe "authenticated data store" do
68
68
  valid_response
69
69
  end
70
70
  setup_rack(app).call(@env)
71
- @env['rack.session']['warden.user.default.session'].should be_nil
72
- @env['rack.session']['warden.user.foo.session' ].should == {:key => "another value"}
71
+ expect(@env['rack.session']['warden.user.default.session']).to be_nil
72
+ expect(@env['rack.session']['warden.user.foo.session' ]).to eq(key: "another value")
73
73
  end
74
74
 
75
75
  it "should clear out all data when a general logout is performed" do
@@ -80,11 +80,11 @@ describe "authenticated data store" do
80
80
  valid_response
81
81
  end
82
82
  setup_rack(app).call(@env)
83
- @env['rack.session']['warden.user.default.session'].should be_nil
84
- @env['rack.session']['warden.user.foo.session' ].should be_nil
83
+ expect(@env['rack.session']['warden.user.default.session']).to be_nil
84
+ expect(@env['rack.session']['warden.user.foo.session' ]).to be_nil
85
85
  end
86
86
 
87
- it "should logout multuiple personas at once" do
87
+ it "should logout multiple persons at once" do
88
88
  @env['rack.session']['warden.user.bar.key'] = "bar user"
89
89
 
90
90
  app = lambda do |e|
@@ -95,9 +95,9 @@ describe "authenticated data store" do
95
95
  valid_response
96
96
  end
97
97
  setup_rack(app).call(@env)
98
- @env['rack.session']['warden.user.default.session'].should be_nil
99
- @env['rack.session']['warden.user.foo.session' ].should == {:key => "another value"}
100
- @env['rack.session']['warden.user.bar.session' ].should be_nil
98
+ expect(@env['rack.session']['warden.user.default.session']).to be_nil
99
+ expect(@env['rack.session']['warden.user.foo.session' ]).to eq(key: "another value")
100
+ expect(@env['rack.session']['warden.user.bar.session' ]).to be_nil
101
101
  end
102
102
 
103
103
  it "should not store data for a user who is not logged in" do
@@ -107,8 +107,8 @@ describe "authenticated data store" do
107
107
  valid_response
108
108
  end
109
109
 
110
- lambda do
110
+ expect {
111
111
  setup_rack(app).call(@env)
112
- end.should raise_error(Warden::NotAuthenticated)
112
+ }.to raise_error(Warden::NotAuthenticated)
113
113
  end
114
114
  end