warden 0.8.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+ module Warden
3
+ class SessionSerializer
4
+ attr_reader :env
5
+ include ::Warden::Mixins::Common
6
+
7
+ def initialize(env)
8
+ @env = env
9
+ end
10
+
11
+ def key_for(scope)
12
+ "warden.user.#{scope}.key"
13
+ end
14
+
15
+ def serialize(user)
16
+ user
17
+ end
18
+
19
+ def deserialize(key)
20
+ key
21
+ end
22
+
23
+ def store(user, scope)
24
+ return unless user
25
+ session[key_for(scope)] = serialize(user)
26
+ end
27
+
28
+ def fetch(scope)
29
+ key = session[key_for(scope)]
30
+ return nil unless key
31
+ user = deserialize(key)
32
+ delete(scope) unless user
33
+ user
34
+ end
35
+
36
+ def stored?(scope)
37
+ !!session[key_for(scope)]
38
+ end
39
+
40
+ def delete(scope, user=nil)
41
+ session.delete(key_for(scope))
42
+ end
43
+ end # SessionSerializer
44
+ end # Warden
@@ -1,18 +1,46 @@
1
1
  # encoding: utf-8
2
- require 'warden/declarable'
3
-
4
2
  module Warden
5
3
  module Strategies
6
- extend Warden::Declarable
7
-
8
4
  class << self
9
- def check_validity!(label, strategy)
10
- return if strategy.method_defined?(:authenticate!)
11
- raise NoMethodError, "authenticate! is not declared in the #{label.inspect} strategy"
5
+ # Add a strategy and store it in a hash.
6
+ def add(label, strategy = nil, &block)
7
+ strategy ||= Class.new(Warden::Strategies::Base)
8
+ strategy.class_eval(&block) if block_given?
9
+
10
+ unless strategy.method_defined?(:authenticate!)
11
+ raise NoMethodError, "authenticate! is not declared in the #{label.inspect} strategy"
12
+ end
13
+
14
+ unless strategy.ancestors.include?(Warden::Strategies::Base)
15
+ raise "#{label.inspect} is not a #{base}"
16
+ end
17
+
18
+ _strategies[label] = strategy
19
+ end
20
+
21
+ # Update a previously given strategy.
22
+ def update(label, &block)
23
+ strategy = _strategies[label]
24
+ raise "Unknown strategy #{label.inspect}" unless strategy
25
+ add(label, strategy, &block)
12
26
  end
13
-
14
- alias :_strategies :_declarations
15
- end # << self
16
27
 
28
+ # Provides access to strategies by label
29
+ # :api: public
30
+ def [](label)
31
+ _strategies[label]
32
+ end
33
+
34
+ # Clears all declared.
35
+ # :api: public
36
+ def clear!
37
+ _strategies.clear
38
+ end
39
+
40
+ # :api: private
41
+ def _strategies
42
+ @strategies ||= {}
43
+ end
44
+ end # << self
17
45
  end # Strategies
18
46
  end # Warden
@@ -31,32 +31,41 @@ module Warden
31
31
  # :api: public
32
32
  attr_accessor :user, :message
33
33
 
34
- #:api: private
35
- attr_accessor :result, :custom_response
36
-
37
- # Setup for redirection
38
34
  # :api: private
39
- attr_reader :_status
35
+ attr_accessor :result, :custom_response
40
36
 
41
- # Accessor for the rack env
42
37
  # :api: public
43
- attr_reader :env, :scope
38
+ attr_reader :env, :scope, :status
39
+
44
40
  include ::Warden::Mixins::Common
45
41
 
46
42
  # :api: private
47
43
  def initialize(env, scope=nil) # :nodoc:
48
44
  @env, @scope = env, scope
49
- @_status, @headers = nil, {}
50
- @halted = false
45
+ @status, @headers = nil, {}
46
+ @halted, @performed = false, false
51
47
  end
52
48
 
53
49
  # The method that is called from above. This method calls the underlying authenticate! method
54
50
  # :api: private
55
51
  def _run! # :nodoc:
56
- result = authenticate!
52
+ @performed = true
53
+ authenticate!
57
54
  self
58
55
  end
59
56
 
57
+ # Returns if this strategy was already performed.
58
+ # :api: private
59
+ def performed? #:nodoc:
60
+ @performed
61
+ end
62
+
63
+ # Marks this strategy as not performed.
64
+ # :api: private
65
+ def clear!
66
+ @performed = false
67
+ end
68
+
60
69
  # Acts as a guarding method for the strategy.
61
70
  # If #valid? responds false, the strategy will not be executed
62
71
  # Overwrite with your own logic
@@ -127,7 +136,7 @@ module Warden
127
136
  # :api: public
128
137
  def redirect!(url, params = {}, opts = {})
129
138
  halt!
130
- @_status = opts[:permanent] ? 301 : 302
139
+ @status = opts[:permanent] ? 301 : 302
131
140
  headers["Location"] = url
132
141
  headers["Location"] << "?" << Rack::Utils.build_query(params) unless params.empty?
133
142
  headers["Content-Type"] = opts[:content_type] || 'text/plain'
@@ -1,3 +1,3 @@
1
1
  module Warden
2
- VERSION = "0.8.1".freeze
2
+ VERSION = "0.9.0".freeze
3
3
  end
@@ -1,9 +1,11 @@
1
1
  $TESTING=true
2
- require 'rubygems'
3
- require 'rack'
2
+
4
3
  $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
5
4
  require 'warden'
6
5
 
6
+ require 'rubygems'
7
+ require 'rack'
8
+
7
9
  Dir[File.join(File.dirname(__FILE__), "helpers", "**/*.rb")].each do |f|
8
10
  require f
9
11
  end
@@ -23,26 +23,11 @@ describe Warden::Config do
23
23
  @config.default_strategies.should == [:foo, :bar]
24
24
  end
25
25
 
26
- it "should allow to read and set default serializers" do
27
- @config.default_serializers :foo, :bar
28
- @config.default_serializers.should == [:foo, :bar]
29
- end
30
-
31
- it "should ship with default serializers and strategies" do
32
- @config.default_strategies.should == []
33
- @config.default_serializers.should == [:session]
34
- end
35
-
36
26
  it "should allow to silence missing strategies" do
37
27
  @config.silence_missing_strategies!
38
28
  @config.silence_missing_strategies?.should be_true
39
29
  end
40
30
 
41
- it "should allow to silence missing serializers" do
42
- @config.silence_missing_serializers!
43
- @config.silence_missing_serializers?.should be_true
44
- end
45
-
46
31
  it "should set the default_scope" do
47
32
  @config.default_scope.should == :default
48
33
  @config.default_scope = :foo
@@ -93,7 +93,7 @@ describe "standard authentication hooks" do
93
93
  RAM.after_fetch{|u,a,o| a.env['warden.spec.hook.paz'] = "run paz"}
94
94
  RAM.after_fetch{|u,a,o| o[:event].should == :fetch }
95
95
  env = env_with_params
96
- setup_rack(lambda { valid_response }).call(env)
96
+ setup_rack(lambda { |e| valid_response }).call(env)
97
97
  env['rack.session']['warden.user.default.key'] = "Foo"
98
98
  env['warden'].user.should == "Foo"
99
99
  env['warden.spec.hook.baz'].should == 'run baz'
@@ -113,7 +113,7 @@ describe "standard authentication hooks" do
113
113
  it "should not be invoked if fetched user is nil" do
114
114
  RAM.after_fetch{|u,a,o| fail}
115
115
  env = env_with_params
116
- setup_rack(lambda { valid_response }).call(env)
116
+ setup_rack(lambda { |e| valid_response }).call(env)
117
117
  env['rack.session']['warden.user.default.key'] = nil
118
118
  env['warden'].user.should be_nil
119
119
  end
@@ -168,7 +168,7 @@ describe "standard authentication hooks" do
168
168
  RAM._before_logout.should have(1).item
169
169
  end
170
170
 
171
- it "should allow me to add multiple after_authetnication hooks" do
171
+ it "should allow me to add multiple after_authentication hooks" do
172
172
  RAM.before_logout{|u,a,o| "bar"}
173
173
  RAM.before_logout{|u,a,o| "baz"}
174
174
  RAM._before_logout.should have(2).items
@@ -185,7 +185,7 @@ describe "standard authentication hooks" do
185
185
  env['warden.spec.hook.ipsum'].should == 'run ipsum'
186
186
  end
187
187
 
188
- it "should run before_logout hook for a socufued scope" do
188
+ it "should run before_logout hook for a specified scope" do
189
189
  RAM.before_logout(:scope => :scope1){|u,a,o| a.env["warden.spec.hook.a"] << :scope1 }
190
190
  RAM.before_logout(:scope => [:scope2]){|u,a,o| a.env["warden.spec.hook.b"] << :scope2 }
191
191
 
@@ -218,15 +218,7 @@ describe Warden::Manager do
218
218
  end
219
219
  end
220
220
 
221
- it "should allow me to access serializers through manager" do
222
- Rack::Builder.new do
223
- use Warden::Manager do |manager|
224
- manager.serializers.should == Warden::Serializers
225
- end
226
- end
227
- end
228
-
229
- it "should allow me to access serializers through manager" do
221
+ it "should allow me to access strategies through manager" do
230
222
  Rack::Builder.new do
231
223
  use Warden::Manager do |manager|
232
224
  manager.strategies.should == Warden::Strategies
@@ -46,236 +46,219 @@ describe Warden::Proxy do
46
46
  resp.first.should == 200
47
47
  end
48
48
 
49
- describe "authenticate!" do
50
-
51
- it "should allow authentication in my application" do
52
- env = env_with_params('/', :username => "fred", :password => "sekrit")
53
- app = lambda do |env|
54
- env['warden'].authenticate
55
- env['warden'].should be_authenticated
56
- env['warden.spec.strategies'].should == [:password]
57
- valid_response
58
- end
59
- setup_rack(app).call(env)
60
- end
61
-
62
- it "should be false in my application" do
63
- env = env_with_params("/", :foo => "bar")
64
- app = lambda do |env|
65
- env['warden'].authenticate
66
- env['warden'].should_not be_authenticated
67
- env['warden.spec.strategies'].should == [:password]
68
- valid_response
69
- end
70
- setup_rack(app).call(env)
49
+ it "should allow authentication in my application" do
50
+ env = env_with_params('/', :username => "fred", :password => "sekrit")
51
+ app = lambda do |env|
52
+ env['warden'].authenticate
53
+ env['warden'].should be_authenticated
54
+ env['warden.spec.strategies'].should == [:password]
55
+ valid_response
71
56
  end
57
+ setup_rack(app).call(env)
58
+ end
72
59
 
73
- it "should allow me to select which strategies I use in my appliction" do
74
- env = env_with_params("/", :foo => "bar")
75
- app = lambda do |env|
76
- env['warden'].authenticate(:failz)
77
- env['warden'].should_not be_authenticated
78
- env['warden.spec.strategies'].should == [:failz]
79
- valid_response
80
- end
81
- setup_rack(app).call(env)
60
+ it "should allow me to select which strategies I use in my appliction" do
61
+ env = env_with_params("/", :foo => "bar")
62
+ app = lambda do |env|
63
+ env['warden'].authenticate(:failz)
64
+ env['warden'].should_not be_authenticated
65
+ env['warden.spec.strategies'].should == [:failz]
66
+ valid_response
82
67
  end
68
+ setup_rack(app).call(env)
69
+ end
83
70
 
84
- it "should raise error on missing strategies" do
85
- env = env_with_params('/')
86
- app = lambda do |env|
87
- env['warden'].authenticate(:unknown)
88
- end
89
- lambda {
90
- setup_rack(app).call(env)
91
- }.should raise_error(RuntimeError, "Invalid strategy unknown")
71
+ it "should raise error on missing strategies" do
72
+ env = env_with_params('/')
73
+ app = lambda do |env|
74
+ env['warden'].authenticate(:unknown)
92
75
  end
76
+ lambda {
77
+ setup_rack(app).call(env)
78
+ }.should raise_error(RuntimeError, "Invalid strategy unknown")
79
+ end
93
80
 
94
- it "should not raise error on default missing strategies if silencing" do
95
- env = env_with_params('/')
96
- app = lambda do |env|
97
- env['warden'].authenticate
98
- valid_response
99
- end
100
- lambda {
101
- setup_rack(app, :silence_missing_strategies => true, :default_strategies => [:unknown]).call(env)
102
- }.should_not raise_error
81
+ it "should not raise error on missing strategies if silencing" do
82
+ env = env_with_params('/')
83
+ app = lambda do |env|
84
+ env['warden'].authenticate
85
+ valid_response
103
86
  end
87
+ lambda {
88
+ setup_rack(app, :silence_missing_strategies => true, :default_strategies => [:unknown]).call(env)
89
+ }.should_not raise_error
90
+ end
104
91
 
105
- it "should allow me to get access to the user at warden.user." do
106
- env = env_with_params("/")
107
- app = lambda do |env|
108
- env['warden'].authenticate(:pass)
109
- env['warden'].should be_authenticated
110
- env['warden.spec.strategies'].should == [:pass]
111
- valid_response
112
- end
113
- setup_rack(app).call(env)
92
+ it "should allow me to get access to the user at warden.user." do
93
+ env = env_with_params("/")
94
+ app = lambda do |env|
95
+ env['warden'].authenticate(:pass)
96
+ env['warden'].should be_authenticated
97
+ env['warden.spec.strategies'].should == [:pass]
98
+ valid_response
114
99
  end
100
+ setup_rack(app).call(env)
101
+ end
115
102
 
116
- it "should properly sent the scope to the strategy" do
117
- env = env_with_params("/")
118
- app = lambda do |env|
119
- env['warden'].authenticate!(:pass, :scope => :failz)
120
- env['warden'].should_not be_authenticated
121
- env['warden.spec.strategies'].should == [:pass]
122
- valid_response
123
- end
124
- setup_rack(app).call(env)
103
+ it "should run strategies when authenticate? is asked" do
104
+ env = env_with_params("/")
105
+ app = lambda do |env|
106
+ env['warden'].should_not be_authenticated
107
+ env['warden'].authenticate?(:pass)
108
+ env['warden'].should be_authenticated
109
+ env['warden.spec.strategies'].should == [:pass]
110
+ valid_response
125
111
  end
112
+ setup_rack(app).call(env)
113
+ end
126
114
 
127
- it "should try multiple authentication strategies" do
128
- env = env_with_params("/")
129
- app = lambda do |env|
130
- env['warden'].authenticate(:password,:pass)
131
- env['warden'].should be_authenticated
132
- env['warden.spec.strategies'].should == [:password, :pass]
133
- valid_response
134
- end
135
- setup_rack(app).call(env)
115
+ it "should properly send the scope to the strategy" do
116
+ env = env_with_params("/")
117
+ app = lambda do |env|
118
+ env['warden'].authenticate(:pass, :scope => :failz)
119
+ env['warden'].should_not be_authenticated
120
+ env['warden.spec.strategies'].should == [:pass]
121
+ valid_response
136
122
  end
123
+ setup_rack(app).call(env)
124
+ end
137
125
 
138
- it "should look for an active user in the session with authenticate!" do
139
- app = lambda do |env|
140
- env['rack.session']["warden.user.default.key"] = "foo as a user"
141
- env['warden'].authenticate!(:pass)
142
- valid_response
143
- end
144
- env = env_with_params
145
- setup_rack(app).call(env)
146
- env['warden'].user.should == "foo as a user"
126
+ it "should try multiple authentication strategies" do
127
+ env = env_with_params("/")
128
+ app = lambda do |env|
129
+ env['warden'].authenticate(:password,:pass)
130
+ env['warden'].should be_authenticated
131
+ env['warden.spec.strategies'].should == [:password, :pass]
132
+ valid_response
147
133
  end
134
+ setup_rack(app).call(env)
135
+ end
148
136
 
149
- it "should look for an active user in the session with authenticate?" do
150
- app = lambda do |env|
151
- env['rack.session']['warden.user.foo_scope.key'] = "a foo user"
152
- env['warden'].authenticate(:pass, :scope => :foo_scope)
153
- env['warden'].authenticated?(:foo_scope)
154
- valid_response
155
- end
156
- env = env_with_params
157
- setup_rack(app).call(env)
158
- env['warden'].user(:foo_scope).should == "a foo user"
137
+ it "should look for an active user in the session with authenticate" do
138
+ app = lambda do |env|
139
+ env['rack.session']["warden.user.default.key"] = "foo as a user"
140
+ env['warden'].authenticate(:pass)
141
+ valid_response
159
142
  end
143
+ env = env_with_params
144
+ setup_rack(app).call(env)
145
+ env['warden'].user.should == "foo as a user"
146
+ end
160
147
 
161
- it "should login 2 different users from the session" do
162
- app = lambda do |env|
163
- env['rack.session']['warden.user.foo.key'] = 'foo user'
164
- env['rack.session']['warden.user.bar.key'] = 'bar user'
165
- env['warden'].authenticate(:pass, :scope => :foo)
166
- env['warden'].authenticate(:pass, :scope => :bar)
167
- env['warden'].authenticate(:password)
168
- env['warden'].authenticated?(:foo).should == true
169
- env['warden'].authenticated?(:bar).should == true
170
- env['warden'].authenticated?.should be_false
171
- valid_response
172
- end
173
- env = env_with_params
174
- setup_rack(app).call(env)
175
- env['warden'].user(:foo).should == 'foo user'
176
- env['warden'].user(:bar).should == 'bar user'
177
- env['warden'].user.should be_nil
148
+ it "should look for an active user in the session with authenticate?" do
149
+ app = lambda do |env|
150
+ env['rack.session']['warden.user.foo_scope.key'] = "a foo user"
151
+ env['warden'].authenticate?(:pass, :scope => :foo_scope)
152
+ valid_response
178
153
  end
154
+ env = env_with_params
155
+ setup_rack(app).call(env)
156
+ env['warden'].user(:foo_scope).should == "a foo user"
157
+ end
179
158
 
180
- it "should not be authenticated if scope cannot be retrieved from session" do
181
- begin
182
- Warden::Manager.serialize_from_session { |k| nil }
183
- app = lambda do |env|
184
- env['rack.session']['warden.user.foo_scope.key'] = "a foo user"
185
- env['warden'].authenticated?(:foo_scope)
186
- valid_response
187
- end
188
- env = env_with_params
189
- setup_rack(app).call(env)
190
- env['warden'].user(:foo_scope).should be_nil
191
- ensure
192
- Warden::Manager.serialize_from_session { |k| k }
193
- end
159
+ it "should look for an active user in the session with authenticate!" do
160
+ app = lambda do |env|
161
+ env['rack.session']['warden.user.foo_scope.key'] = "a foo user"
162
+ env['warden'].authenticate!(:pass, :scope => :foo_scope)
163
+ valid_response
194
164
  end
165
+ env = env_with_params
166
+ setup_rack(app).call(env)
167
+ env['warden'].user(:foo_scope).should == "a foo user"
195
168
  end
196
- end # describe "authentication"
197
169
 
198
- describe "stored?" do
199
- before(:each) do
200
- @env['rack.session'] ||= {}
201
- @env['rack.session']['warden.user.default.key'] = "User"
170
+ it "should throw an error when authenticate!" do
171
+ app = lambda do |env|
172
+ env['warden'].authenticate!(:pass, :scope => :failz)
173
+ raise "OMG"
174
+ end
175
+ env = env_with_params
176
+ setup_rack(app).call(env)
202
177
  end
203
178
 
204
- it "returns true if user key is stored in session" do
179
+ it "should login 2 different users from the session" do
205
180
  app = lambda do |env|
206
- env['warden'].stored?.should == true
181
+ env['rack.session']['warden.user.foo.key'] = 'foo user'
182
+ env['rack.session']['warden.user.bar.key'] = 'bar user'
183
+ env['warden'].authenticate(:pass, :scope => :foo)
184
+ env['warden'].authenticate(:pass, :scope => :bar)
185
+ env['warden'].authenticate(:password)
186
+ env['warden'].should be_authenticated(:foo)
187
+ env['warden'].should be_authenticated(:bar)
188
+ env['warden'].should_not be_authenticated # default scope
207
189
  valid_response
208
190
  end
209
- setup_rack(app).call(@env)
191
+ env = env_with_params
192
+ setup_rack(app).call(env)
193
+ env['warden'].user(:foo).should == 'foo user'
194
+ env['warden'].user(:bar).should == 'bar user'
195
+ env['warden'].user.should be_nil
210
196
  end
197
+ end
211
198
 
212
- it "returns false if user key is not stored in session" do
213
- @env['rack.session'].delete("warden.user.default.key")
199
+ describe "authentication cache" do
200
+ it "should run strategies just once for a given scope" do
201
+ env = env_with_params("/")
214
202
  app = lambda do |env|
215
- env['warden'].stored?.should == false
203
+ env['warden'].authenticate(:password, :pass, :scope => :failz)
204
+ env['warden'].should_not be_authenticated(:failz)
205
+ env['warden'].authenticate(:password, :pass, :scope => :failz)
206
+ env['warden'].should_not be_authenticated(:failz)
207
+ env['warden.spec.strategies'].should == [:password, :pass]
216
208
  valid_response
217
209
  end
218
- setup_rack(app).call(@env)
210
+ setup_rack(app).call(env)
219
211
  end
220
212
 
221
- it "returns false if scope given is not stored in session" do
213
+ it "should run strategies for a given scope several times if cache is cleaned" do
214
+ env = env_with_params("/")
222
215
  app = lambda do |env|
223
- env['warden'].stored?.should be_true
224
- env['warden'].stored?(:another).should be_false
216
+ env['warden'].authenticate(:password, :pass, :scope => :failz)
217
+ env['warden'].clear_strategies_cache!(:scope => :failz)
218
+ env['warden'].authenticate(:password, :pass, :scope => :failz)
219
+ env['warden.spec.strategies'].should == [:password, :pass, :password, :pass]
225
220
  valid_response
226
221
  end
227
- setup_rack(app).call(@env)
222
+ setup_rack(app).call(env)
228
223
  end
229
224
 
230
- it "returns based on the given store" do
231
- @env['rack.session'].delete("warden.user.default.key")
232
- @env["HTTP_COOKIE"] = "warden.user.default.key=user;"
225
+ it "should clear the cache for a specified strategy" do
226
+ env = env_with_params("/")
233
227
  app = lambda do |env|
234
- env['warden'].stored?(:default, :session).should be_false
235
- env['warden'].stored?(:default, :cookie).should be_true
228
+ env['warden'].authenticate(:password, :pass, :scope => :failz)
229
+ env['warden'].clear_strategies_cache!(:password, :scope => :failz)
230
+ env['warden'].authenticate(:password, :pass, :scope => :failz)
231
+ env['warden.spec.strategies'].should == [:password, :pass, :password]
236
232
  valid_response
237
233
  end
238
- setup_rack(app, :default_serializers => [:session, :cookie]).call(@env)
234
+ setup_rack(app).call(env)
239
235
  end
240
- end
241
236
 
242
- it "should not raise errors with missing serializers" do
243
- env = env_with_params('/')
244
- app = lambda do |env|
245
- env['warden'].authenticate
246
- env['warden'].serializers.size.should == 1
247
- env['warden'].serializers.first.should be_kind_of(Warden::Serializers[:session])
248
- valid_response
249
- end
250
- lambda {
251
- setup_rack(app, :silence_missing_serializers => true, :default_serializers => [:session, :unknown]).call(env)
252
- }.should_not raise_error
253
- end
254
-
255
- describe "set user" do
256
- it "should store the user into the session" do
237
+ it "should run the strategies several times for different scopes" do
257
238
  env = env_with_params("/")
258
239
  app = lambda do |env|
259
- env['warden'].authenticate(:pass)
240
+ env['warden'].authenticate(:password, :pass, :scope => :failz)
241
+ env['warden'].should_not be_authenticated(:failz)
242
+ env['warden'].authenticate(:password, :pass)
260
243
  env['warden'].should be_authenticated
261
- env['warden'].user.should == "Valid User"
262
- env['rack.session']["warden.user.default.key"].should == "Valid User"
244
+ env['warden.spec.strategies'].should == [:password, :pass, :password, :pass]
263
245
  valid_response
264
246
  end
265
247
  setup_rack(app).call(env)
266
248
  end
249
+ end
267
250
 
268
- it "should store the user in all serializers" do
251
+ describe "set user" do
252
+ it "should store the user into the session" do
269
253
  env = env_with_params("/")
270
254
  app = lambda do |env|
271
255
  env['warden'].authenticate(:pass)
272
256
  env['warden'].should be_authenticated
273
257
  env['warden'].user.should == "Valid User"
258
+ env['rack.session']["warden.user.default.key"].should == "Valid User"
274
259
  valid_response
275
260
  end
276
- setup_rack(app, :default_serializers => [:session, :cookie]).call(env)
277
- headers = env['warden'].serializers.last.response.headers
278
- headers['Set-Cookie'].split(";").first.should == "warden.user.default.key=Valid+User"
261
+ setup_rack(app).call(env)
279
262
  end
280
263
 
281
264
  it "should not store the user if the :store option is set to false" do
@@ -314,17 +297,7 @@ describe Warden::Proxy do
314
297
  setup_rack(app).call(@env)
315
298
  end
316
299
 
317
- it "should load user from others serializers" do
318
- @env["HTTP_COOKIE"] = "warden.user.default.key=user;"
319
- app = lambda do |env|
320
- env['warden'].user.should == "user"
321
- valid_response
322
- end
323
- setup_rack(app, :default_serializers => [:session, :cookie]).call(@env)
324
- end
325
-
326
300
  describe "previously logged in" do
327
-
328
301
  before(:each) do
329
302
  @env['rack.session']['warden.user.default.key'] = "A Previous User"
330
303
  @env['warden.spec.strategies'] = []
@@ -412,15 +385,6 @@ describe Warden::Proxy do
412
385
  setup_rack(app).call(@env)
413
386
  end
414
387
 
415
- it "should clear the session in all serializers" do
416
- @app = setup_rack(@app, :default_serializers => [:session, :cookie])
417
- @env['warden.spec.which_logout'] = :default
418
- @app.call(@env)
419
- @env['warden'].serializers.last.should_not be_nil
420
- headers = @env['warden'].serializers.last.response.headers
421
- headers['Set-Cookie'].first.split(";").first.should == "warden.user.default.key="
422
- end
423
-
424
388
  it "should clear out the session by calling reset_session! so that plugins can setup their own session clearing" do
425
389
  @env['rack.session'].should_not be_nil
426
390
  app = lambda do |e|
@@ -455,10 +419,11 @@ describe Warden::Proxy do
455
419
  end
456
420
 
457
421
  describe "when all strategies are not valid?" do
458
- it "should return false for authenticated when there are no valid? strategies" do
422
+ it "should return false for authenticated? when there are no valid? strategies" do
459
423
  @env['rack.session'] = {}
460
424
  app = lambda do |e|
461
- e['warden'].authenticated?(:invalid).should be_false
425
+ e['warden'].authenticate(:invalid).should be_nil
426
+ e['warden'].should_not be_authenticated
462
427
  end
463
428
  setup_rack(app).call(@env)
464
429
  end
@@ -471,6 +436,14 @@ describe Warden::Proxy do
471
436
  setup_rack(app).call(@env)
472
437
  end
473
438
 
439
+ it "should return false for authenticate? when there are no valid strategies" do
440
+ @env['rack.session'] = {}
441
+ app = lambda do |e|
442
+ e['warden'].authenticate?(:invalid).should be_false
443
+ end
444
+ setup_rack(app).call(@env)
445
+ end
446
+
474
447
  it "should respond with a 401 when authenticate! cannot find any valid strategies" do
475
448
  @env['rack.session'] = {}
476
449
  app = lambda do |e|
@@ -479,153 +452,167 @@ describe Warden::Proxy do
479
452
  result = setup_rack(app).call(@env)
480
453
  result.first.should == 401
481
454
  end
455
+ end
482
456
 
483
- describe "authenticated?" do
484
- describe "positive authentication" do
485
- before do
486
- @env['rack.session'] = {'warden.user.default.key' => 'defult_key'}
487
- $captures = []
488
- end
457
+ describe "authenticated?" do
458
+ describe "positive authentication" do
459
+ before do
460
+ @env['rack.session'] = {'warden.user.default.key' => 'defult_key'}
461
+ $captures = []
462
+ end
489
463
 
490
- it "should return true when authenticated in the session" do
491
- app = lambda do |e|
492
- e['warden'].should be_authenticated
493
- end
494
- result = setup_rack(app).call(@env)
464
+ it "should return true when authenticated in the session" do
465
+ app = lambda do |e|
466
+ e['warden'].should be_authenticated
495
467
  end
468
+ result = setup_rack(app).call(@env)
469
+ end
496
470
 
497
- it "should yield to a block when the block is passed and authenticated" do
498
- app = lambda do |e|
499
- e['warden'].authenticated? do
500
- $captures << :in_the_block
501
- end
471
+ it "should yield to a block when the block is passed and authenticated" do
472
+ app = lambda do |e|
473
+ e['warden'].authenticated? do
474
+ $captures << :in_the_block
502
475
  end
503
- setup_rack(app).call(@env)
504
- $captures.should == [:in_the_block]
505
476
  end
477
+ setup_rack(app).call(@env)
478
+ $captures.should == [:in_the_block]
479
+ end
506
480
 
507
- it "should authenticate for a user in a different scope" do
508
- @env['rack.session'] = {'warden.user.foo.key' => 'foo_key'}
509
- app = lambda do |e|
510
- e['warden'].authenticated?(:foo) do
511
- $captures << :in_the_foo_block
512
- end
481
+ it "should authenticate for a user in a different scope" do
482
+ @env['rack.session'] = {'warden.user.foo.key' => 'foo_key'}
483
+ app = lambda do |e|
484
+ e['warden'].authenticated?(:foo) do
485
+ $captures << :in_the_foo_block
513
486
  end
514
- setup_rack(app).call(@env)
515
- $captures.should == [:in_the_foo_block]
516
487
  end
488
+ setup_rack(app).call(@env)
489
+ $captures.should == [:in_the_foo_block]
490
+ end
491
+ end
492
+
493
+ describe "negative authentication" do
494
+ before do
495
+ @env['rack.session'] = {'warden.foo.default.key' => 'foo_key'}
496
+ $captures = []
517
497
  end
518
498
 
519
- describe "negative authentication" do
520
- before do
521
- @env['rack.session'] = {'warden.foo.default.key' => 'foo_key'}
522
- $captures = []
499
+ it "should return false when authenticated in the session" do
500
+ app = lambda do |e|
501
+ e['warden'].should_not be_authenticated
523
502
  end
503
+ result = setup_rack(app).call(@env)
504
+ end
524
505
 
525
- it "should return false when authenticated in the session" do
526
- app = lambda do |e|
527
- e['warden'].should_not be_authenticated
506
+ it "should return false if scope cannot be retrieved from session" do
507
+ begin
508
+ Warden::Manager.serialize_from_session { |k| nil }
509
+ app = lambda do |env|
510
+ env['rack.session']['warden.user.foo_scope.key'] = "a foo user"
511
+ env['warden'].authenticated?(:foo_scope)
512
+ valid_response
528
513
  end
529
- result = setup_rack(app).call(@env)
514
+ env = env_with_params
515
+ setup_rack(app).call(env)
516
+ env['warden'].user(:foo_scope).should be_nil
517
+ ensure
518
+ Warden::Manager.serialize_from_session { |k| k }
530
519
  end
520
+ end
531
521
 
532
- it "should not yield to a block when the block is passed and authenticated" do
533
- app = lambda do |e|
534
- e['warden'].authenticated? do
535
- $captures << :in_the_block
536
- end
522
+ it "should not yield to a block when the block is passed and authenticated" do
523
+ app = lambda do |e|
524
+ e['warden'].authenticated? do
525
+ $captures << :in_the_block
537
526
  end
538
- setup_rack(app).call(@env)
539
- $captures.should == []
540
527
  end
528
+ setup_rack(app).call(@env)
529
+ $captures.should == []
530
+ end
541
531
 
542
- it "should not yield for a user in a different scope" do
543
- app = lambda do |e|
544
- e['warden'].authenticated?(:bar) do
545
- $captures << :in_the_bar_block
546
- end
532
+ it "should not yield for a user in a different scope" do
533
+ app = lambda do |e|
534
+ e['warden'].authenticated?(:bar) do
535
+ $captures << :in_the_bar_block
547
536
  end
548
- setup_rack(app).call(@env)
549
- $captures.should == []
550
537
  end
538
+ setup_rack(app).call(@env)
539
+ $captures.should == []
551
540
  end
552
541
  end
542
+ end
553
543
 
544
+ describe "unauthenticated?" do
545
+ describe "negative unauthentication" do
546
+ before do
547
+ @env['rack.session'] = {'warden.user.default.key' => 'defult_key'}
548
+ $captures = []
549
+ end
554
550
 
555
- describe "unauthenticated?" do
556
- describe "negative unauthentication" do
557
- before do
558
- @env['rack.session'] = {'warden.user.default.key' => 'defult_key'}
559
- $captures = []
560
- end
561
-
562
- it "should return false when authenticated in the session" do
563
- app = lambda do |e|
564
- e['warden'].should_not be_unauthenticated
565
- end
566
- result = setup_rack(app).call(@env)
551
+ it "should return false when authenticated in the session" do
552
+ app = lambda do |e|
553
+ e['warden'].should_not be_unauthenticated
567
554
  end
555
+ result = setup_rack(app).call(@env)
556
+ end
568
557
 
569
- it "should not yield to a block when the block is passed and authenticated" do
570
- app = lambda do |e|
571
- e['warden'].unauthenticated? do
572
- $captures << :in_the_block
573
- end
558
+ it "should not yield to a block when the block is passed and authenticated" do
559
+ app = lambda do |e|
560
+ e['warden'].unauthenticated? do
561
+ $captures << :in_the_block
574
562
  end
575
- setup_rack(app).call(@env)
576
- $captures.should == []
577
563
  end
564
+ setup_rack(app).call(@env)
565
+ $captures.should == []
566
+ end
578
567
 
579
- it "should not yield to the block for a user in a different scope" do
580
- @env['rack.session'] = {'warden.user.foo.key' => 'foo_key'}
581
- app = lambda do |e|
582
- e['warden'].unauthenticated?(:foo) do
583
- $captures << :in_the_foo_block
584
- end
568
+ it "should not yield to the block for a user in a different scope" do
569
+ @env['rack.session'] = {'warden.user.foo.key' => 'foo_key'}
570
+ app = lambda do |e|
571
+ e['warden'].unauthenticated?(:foo) do
572
+ $captures << :in_the_foo_block
585
573
  end
586
- setup_rack(app).call(@env)
587
- $captures.should == []
588
574
  end
575
+ setup_rack(app).call(@env)
576
+ $captures.should == []
589
577
  end
578
+ end
590
579
 
591
- describe "positive unauthentication" do
592
- before do
593
- @env['rack.session'] = {'warden.foo.default.key' => 'foo_key'}
594
- $captures = []
595
- end
580
+ describe "positive unauthentication" do
581
+ before do
582
+ @env['rack.session'] = {'warden.foo.default.key' => 'foo_key'}
583
+ $captures = []
584
+ end
596
585
 
597
- it "should return false when unauthenticated in the session" do
598
- app = lambda do |e|
599
- e['warden'].should be_unauthenticated
600
- end
601
- result = setup_rack(app).call(@env)
586
+ it "should return false when unauthenticated in the session" do
587
+ app = lambda do |e|
588
+ e['warden'].should be_unauthenticated
602
589
  end
590
+ result = setup_rack(app).call(@env)
591
+ end
603
592
 
604
- it "should yield to a block when the block is passed and authenticated" do
605
- app = lambda do |e|
606
- e['warden'].unauthenticated? do
607
- $captures << :in_the_block
608
- end
593
+ it "should yield to a block when the block is passed and authenticated" do
594
+ app = lambda do |e|
595
+ e['warden'].unauthenticated? do
596
+ $captures << :in_the_block
609
597
  end
610
- setup_rack(app).call(@env)
611
- $captures.should == [:in_the_block]
612
598
  end
599
+ setup_rack(app).call(@env)
600
+ $captures.should == [:in_the_block]
601
+ end
613
602
 
614
- it "should yield for a user in a different scope" do
615
- app = lambda do |e|
616
- e['warden'].unauthenticated?(:bar) do
617
- $captures << :in_the_bar_block
618
- end
603
+ it "should yield for a user in a different scope" do
604
+ app = lambda do |e|
605
+ e['warden'].unauthenticated?(:bar) do
606
+ $captures << :in_the_bar_block
619
607
  end
620
- setup_rack(app).call(@env)
621
- $captures.should == [:in_the_bar_block]
622
608
  end
609
+ setup_rack(app).call(@env)
610
+ $captures.should == [:in_the_bar_block]
623
611
  end
624
612
  end
625
613
  end
626
614
 
627
615
  describe "attributes" do
628
-
629
616
  def def_app(&blk)
630
617
  @app = setup_rack(blk)
631
618
  end