warden 1.2.3 → 1.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +1 -1
- data/lib/warden/config.rb +2 -3
- data/lib/warden/hooks.rb +9 -9
- data/lib/warden/manager.rb +21 -9
- data/lib/warden/mixins/common.rb +1 -1
- data/lib/warden/proxy.rb +12 -8
- data/lib/warden/strategies/base.rb +12 -7
- data/lib/warden/test/helpers.rb +1 -1
- data/lib/warden/test/warden_helpers.rb +1 -1
- data/lib/warden/version.rb +1 -1
- data/spec/helpers/strategies/fail_with_user.rb +10 -0
- data/spec/helpers/strategies/invalid.rb +1 -1
- data/spec/warden/authenticated_data_store_spec.rb +20 -20
- data/spec/warden/config_spec.rb +10 -10
- data/spec/warden/errors_spec.rb +7 -7
- data/spec/warden/hooks_spec.rb +40 -40
- data/spec/warden/manager_spec.rb +81 -57
- data/spec/warden/proxy_spec.rb +177 -168
- data/spec/warden/scoped_session_serializer.rb +9 -9
- data/spec/warden/session_serializer_spec.rb +13 -13
- data/spec/warden/strategies/base_spec.rb +37 -37
- data/spec/warden/strategies_spec.rb +14 -13
- data/spec/warden/test/helpers_spec.rb +23 -23
- data/spec/warden/test/test_mode_spec.rb +12 -13
- metadata +19 -23
checksums.yaml
ADDED
@@ -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
data/lib/warden/config.rb
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
module Warden
|
4
|
-
# This
|
5
|
-
#
|
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
|
#
|
data/lib/warden/hooks.rb
CHANGED
@@ -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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
data/lib/warden/manager.rb
CHANGED
@@ -4,7 +4,7 @@ require 'warden/config'
|
|
4
4
|
|
5
5
|
module Warden
|
6
6
|
# The middleware for Rack Authentication
|
7
|
-
# The
|
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
|
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
|
-
|
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
|
-
#
|
76
|
-
# Use the results of user_session_key to
|
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
|
data/lib/warden/mixins/common.rb
CHANGED
data/lib/warden/proxy.rb
CHANGED
@@ -69,7 +69,7 @@ module Warden
|
|
69
69
|
#
|
70
70
|
# :api: public
|
71
71
|
def clear_strategies_cache!(*args)
|
72
|
-
scope,
|
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
|
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,
|
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
|
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
|
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
|
-
|
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.
|
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
|
-
#
|
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
|
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
|
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
|
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
|
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
|
-
#
|
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
|
-
#
|
148
|
-
# opts <Hash> - Any options to
|
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
|
data/lib/warden/test/helpers.rb
CHANGED
@@ -10,7 +10,7 @@ module Warden
|
|
10
10
|
::Warden.test_mode!
|
11
11
|
end
|
12
12
|
|
13
|
-
# A helper method that will
|
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
|
data/lib/warden/version.rb
CHANGED
@@ -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'].
|
20
|
-
e['warden'].
|
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'].
|
28
|
-
@env['rack.session']['warden.user.foo.session'].
|
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'].
|
37
|
+
expect(@env['rack.session']['warden.user.foo.session']).to eq(key: "value")
|
38
38
|
end
|
39
39
|
|
40
|
-
it "should store the data
|
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'].
|
48
|
-
@env['rack.session']['warden.user.foo.session' ].
|
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'].
|
60
|
-
@env['rack.session']['warden.user.foo.session' ].
|
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'].
|
72
|
-
@env['rack.session']['warden.user.foo.session' ].
|
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'].
|
84
|
-
@env['rack.session']['warden.user.foo.session' ].
|
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
|
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'].
|
99
|
-
@env['rack.session']['warden.user.foo.session' ].
|
100
|
-
@env['rack.session']['warden.user.bar.session' ].
|
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
|
-
|
110
|
+
expect {
|
111
111
|
setup_rack(app).call(@env)
|
112
|
-
|
112
|
+
}.to raise_error(Warden::NotAuthenticated)
|
113
113
|
end
|
114
114
|
end
|