hassox-warden 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.textile +1 -0
- data/Rakefile +57 -0
- data/TODO.textile +2 -0
- data/lib/warden.rb +14 -0
- data/lib/warden/authentication/hooks.rb +125 -0
- data/lib/warden/authentication/strategies.rb +58 -0
- data/lib/warden/authentication/strategy_base.rb +124 -0
- data/lib/warden/errors.rb +70 -0
- data/lib/warden/manager.rb +134 -0
- data/lib/warden/mixins/common.rb +25 -0
- data/lib/warden/proxy.rb +200 -0
- data/spec/helpers/request_helper.rb +51 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/warden/authenticated_data_store_spec.rb +111 -0
- data/spec/warden/errors_spec.rb +46 -0
- data/spec/warden/hooks_spec.rb +103 -0
- data/spec/warden/manager_spec.rb +158 -0
- data/spec/warden/proxy_spec.rb +218 -0
- data/spec/warden/strategies/failz.rb +9 -0
- data/spec/warden/strategies/invalid.rb +7 -0
- data/spec/warden/strategies/pass.rb +7 -0
- data/spec/warden/strategies/pass_without_user.rb +7 -0
- data/spec/warden/strategies/password.rb +12 -0
- data/spec/warden/strategies_spec.rb +78 -0
- data/spec/warden/strategy_base_spec.rb +259 -0
- data/spec/warden_spec.rb +4 -0
- metadata +87 -0
@@ -0,0 +1,12 @@
|
|
1
|
+
Warden::Strategies.add(:password) do
|
2
|
+
def authenticate!
|
3
|
+
request.env['warden.spec.strategies'] ||= []
|
4
|
+
request.env['warden.spec.strategies'] << :password
|
5
|
+
if params["password"] || params["username"]
|
6
|
+
params["password"] == "sekrit" && params["username"] == "fred" ?
|
7
|
+
success!("Authenticated User") : fail!("Username or password is incorrect")
|
8
|
+
else
|
9
|
+
pass
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe Warden::Strategies do
|
4
|
+
it "should let me add a strategy via a block" do
|
5
|
+
Warden::Strategies.add(:strategy1) do
|
6
|
+
def authenticate!
|
7
|
+
success("foo")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
Warden::Strategies[:strategy1].ancestors.should include(Warden::Strategies::Base)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should raise an error if I add a strategy via a block, that does not have an autheniticate! method" do
|
14
|
+
lambda do
|
15
|
+
Warden::Strategies.add(:strategy2) do
|
16
|
+
end
|
17
|
+
end.should raise_error
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should allow me to get access to a particular middleware" do
|
21
|
+
Warden::Strategies.add(:strategy3) do
|
22
|
+
def authenticate!; end
|
23
|
+
end
|
24
|
+
strategy = Warden::Strategies[:strategy3]
|
25
|
+
strategy.should_not be_nil
|
26
|
+
strategy.ancestors.should include(Warden::Strategies::Base)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should allow me to add a strategy with the required methods" do
|
30
|
+
class MyStrategy < Warden::Strategies::Base
|
31
|
+
def authenticate!; end
|
32
|
+
end
|
33
|
+
lambda do
|
34
|
+
Warden::Strategies.add(:strategy4, MyStrategy)
|
35
|
+
end.should_not raise_error
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should not allow a strategy that does not have a call(env) and initialize(app, config={}) method" do
|
39
|
+
class MyOtherStrategy
|
40
|
+
end
|
41
|
+
lambda do
|
42
|
+
Warden::Strategies.add(:strategy5, MyOtherStrategy)
|
43
|
+
end.should raise_error
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should allow me to inherit from a class when providing a block and class" do
|
47
|
+
class MyStrategy < Warden::Strategies::Base
|
48
|
+
def authenticate!
|
49
|
+
self.call
|
50
|
+
end
|
51
|
+
|
52
|
+
def call
|
53
|
+
request.env['warden.spec.strategies'] ||= []
|
54
|
+
request.env['warden.spec.strategies'] << :inherited
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
Warden::Strategies.add(:foo, MyStrategy) do
|
59
|
+
def authenticate!
|
60
|
+
self.call
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
Warden::Strategies[:foo].ancestors.should include(MyStrategy)
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should allow me to clear the strategies" do
|
69
|
+
Warden::Strategies.add(:foobar) do
|
70
|
+
def authenticate!
|
71
|
+
:foo
|
72
|
+
end
|
73
|
+
end
|
74
|
+
Warden::Strategies[:foobar].should_not be_nil
|
75
|
+
Warden::Strategies.clear!
|
76
|
+
Warden::Strategies[:foobar].should be_nil
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,259 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe Warden::Strategies::Base do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
RAS = Warden::Strategies unless defined?(RAS)
|
7
|
+
Warden::Strategies.clear!
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "headers" do
|
11
|
+
it "should have headers" do
|
12
|
+
Warden::Strategies.add(:foo) do
|
13
|
+
def authenticate!
|
14
|
+
headers("foo" => "bar")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
strategy = Warden::Strategies[:foo].new(env_with_params)
|
18
|
+
strategy._run!
|
19
|
+
strategy.headers["foo"].should == "bar"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should allow us to clear the headers" do
|
23
|
+
Warden::Strategies.add(:foo) do
|
24
|
+
def authenticate!
|
25
|
+
headers("foo" => "bar")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
strategy = Warden::Strategies[:foo].new(env_with_params)
|
29
|
+
strategy._run!
|
30
|
+
strategy.headers["foo"].should == "bar"
|
31
|
+
strategy.headers.clear
|
32
|
+
strategy.headers.should be_empty
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should have a user object" do
|
37
|
+
RAS.add(:foobar) do
|
38
|
+
def authenticate!
|
39
|
+
success!("foo")
|
40
|
+
end
|
41
|
+
end
|
42
|
+
strategy = RAS[:foobar].new(env_with_params)
|
43
|
+
strategy._run!
|
44
|
+
strategy.user.should == "foo"
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should allow you to set a message" do
|
48
|
+
RAS.add(:foobar) do
|
49
|
+
def authenticate!
|
50
|
+
self.message = "foo message"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
strategy = RAS[:foobar].new(env_with_params)
|
54
|
+
strategy._run!
|
55
|
+
strategy.message.should == "foo message"
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should provide access to the errors" do
|
59
|
+
RAS.add(:foobar) do
|
60
|
+
def authenticate!
|
61
|
+
errors.add(:foo, "foo has an error")
|
62
|
+
end
|
63
|
+
end
|
64
|
+
env = env_with_params
|
65
|
+
env['warden.errors'] = Warden::Proxy::Errors.new
|
66
|
+
strategy = RAS[:foobar].new(env)
|
67
|
+
strategy._run!
|
68
|
+
strategy.errors.on(:foo).should == ["foo has an error"]
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "halting" do
|
72
|
+
it "should allow you to halt a strategy" do
|
73
|
+
RAS.add(:foobar) do
|
74
|
+
def authenticate!
|
75
|
+
halt!
|
76
|
+
end
|
77
|
+
end
|
78
|
+
str = RAS[:foobar].new(env_with_params)
|
79
|
+
str._run!
|
80
|
+
str.should be_halted
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should not be halted if halt was not called" do
|
84
|
+
RAS.add(:foobar) do
|
85
|
+
def authenticate!
|
86
|
+
"foo"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
str = RAS[:foobar].new(env_with_params)
|
90
|
+
str._run!
|
91
|
+
str.should_not be_halted
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "pass" do
|
97
|
+
it "should allow you to pass" do
|
98
|
+
RAS.add(:foobar) do
|
99
|
+
def authenticate!
|
100
|
+
pass
|
101
|
+
end
|
102
|
+
end
|
103
|
+
str = RAS[:foobar].new(env_with_params)
|
104
|
+
str._run!
|
105
|
+
str.should_not be_halted
|
106
|
+
str.user.should be_nil
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe "redirect" do
|
111
|
+
it "should allow you to set a redirection" do
|
112
|
+
RAS.add(:foobar) do
|
113
|
+
def authenticate!
|
114
|
+
redirect!("/foo/bar")
|
115
|
+
end
|
116
|
+
end
|
117
|
+
str = RAS[:foobar].new(env_with_params)
|
118
|
+
str._run!
|
119
|
+
str.user.should be_nil
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should mark the strategy as halted when redirecting" do
|
123
|
+
RAS.add(:foobar) do
|
124
|
+
def authenticate!
|
125
|
+
redirect!("/foo/bar")
|
126
|
+
end
|
127
|
+
end
|
128
|
+
str = RAS[:foobar].new(env_with_params)
|
129
|
+
str._run!
|
130
|
+
str.should be_halted
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should escape redirected url parameters" do
|
134
|
+
RAS.add(:foobar) do
|
135
|
+
def authenticate!
|
136
|
+
redirect!("/foo/bar", :foo => "bar")
|
137
|
+
end
|
138
|
+
end
|
139
|
+
str = RAS[:foobar].new(env_with_params)
|
140
|
+
str._run!
|
141
|
+
str.headers["Location"].should == "/foo/bar?foo=bar"
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should allow you to set a message" do
|
145
|
+
RAS.add(:foobar) do
|
146
|
+
def authenticate!
|
147
|
+
redirect!("/foo/bar", {:foo => "bar"}, :message => "You are being redirected foo")
|
148
|
+
end
|
149
|
+
end
|
150
|
+
str = RAS[:foobar].new(env_with_params)
|
151
|
+
str._run!
|
152
|
+
str.headers["Location"].should == "/foo/bar?foo=bar"
|
153
|
+
str.message.should == "You are being redirected foo"
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should set the action as :redirect" do
|
157
|
+
RAS.add(:foobar) do
|
158
|
+
def authenticate!
|
159
|
+
redirect!("/foo/bar", {:foo => "bar"}, :message => "foo")
|
160
|
+
end
|
161
|
+
end
|
162
|
+
str = RAS[:foobar].new(env_with_params)
|
163
|
+
str._run!
|
164
|
+
str.result.should == :redirect
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
describe "failure" do
|
169
|
+
|
170
|
+
before(:each) do
|
171
|
+
RAS.add(:foobar) do
|
172
|
+
def authenticate!
|
173
|
+
fail!("You are not cool enough")
|
174
|
+
end
|
175
|
+
end
|
176
|
+
@str = RAS[:foobar].new(env_with_params)
|
177
|
+
end
|
178
|
+
|
179
|
+
it "should allow you to fail" do
|
180
|
+
@str._run!
|
181
|
+
@str.user.should be_nil
|
182
|
+
end
|
183
|
+
|
184
|
+
it "should halt the strategies when failing" do
|
185
|
+
@str._run!
|
186
|
+
@str.should be_halted
|
187
|
+
end
|
188
|
+
|
189
|
+
it "should allow you to set a message when failing" do
|
190
|
+
@str._run!
|
191
|
+
@str.message.should == "You are not cool enough"
|
192
|
+
end
|
193
|
+
|
194
|
+
it "should set the action as :failure" do
|
195
|
+
@str._run!
|
196
|
+
@str.result.should == :failure
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
describe "success" do
|
201
|
+
before(:each) do
|
202
|
+
RAS.add(:foobar) do
|
203
|
+
def authenticate!
|
204
|
+
success!("Foo User")
|
205
|
+
end
|
206
|
+
end
|
207
|
+
@str = RAS[:foobar].new(env_with_params)
|
208
|
+
end
|
209
|
+
|
210
|
+
it "should allow you to succeed" do
|
211
|
+
@str._run!
|
212
|
+
end
|
213
|
+
|
214
|
+
it "should be authenticated after success" do
|
215
|
+
@str._run!
|
216
|
+
@str.user.should_not be_nil
|
217
|
+
end
|
218
|
+
|
219
|
+
it "should store the user" do
|
220
|
+
@str._run!
|
221
|
+
@str.user.should == "Foo User"
|
222
|
+
end
|
223
|
+
|
224
|
+
it "should set the action as :success" do
|
225
|
+
@str._run!
|
226
|
+
@str.result.should == :success
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
describe "custom response" do
|
231
|
+
before(:each) do
|
232
|
+
RAS.add(:foobar) do
|
233
|
+
def authenticate!
|
234
|
+
custom!([521, {"foo" => "bar"}, ["BAD"]])
|
235
|
+
end
|
236
|
+
end
|
237
|
+
@str = RAS[:foobar].new(env_with_params)
|
238
|
+
@str._run!
|
239
|
+
end
|
240
|
+
|
241
|
+
it "should allow me to set a custom rack response" do
|
242
|
+
@str.user.should be_nil
|
243
|
+
end
|
244
|
+
|
245
|
+
it "should halt the strategy" do
|
246
|
+
@str.should be_halted
|
247
|
+
end
|
248
|
+
|
249
|
+
it "should provide access to the custom rack response" do
|
250
|
+
@str.custom_response.should == [521, {"foo" => "bar"}, ["BAD"]]
|
251
|
+
end
|
252
|
+
|
253
|
+
it "should set the action as :custom" do
|
254
|
+
@str._run!
|
255
|
+
@str.result.should == :custom
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
end
|
data/spec/warden_spec.rb
ADDED
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hassox-warden
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Neighman
|
8
|
+
autorequire: warden
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-27 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Rack middleware that provides authentication for rack applications
|
17
|
+
email: has.sox@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.textile
|
24
|
+
- LICENSE
|
25
|
+
- TODO.textile
|
26
|
+
files:
|
27
|
+
- LICENSE
|
28
|
+
- README.textile
|
29
|
+
- Rakefile
|
30
|
+
- TODO.textile
|
31
|
+
- lib/warden
|
32
|
+
- lib/warden/authentication
|
33
|
+
- lib/warden/authentication/hooks.rb
|
34
|
+
- lib/warden/authentication/strategies.rb
|
35
|
+
- lib/warden/authentication/strategy_base.rb
|
36
|
+
- lib/warden/errors.rb
|
37
|
+
- lib/warden/manager.rb
|
38
|
+
- lib/warden/mixins
|
39
|
+
- lib/warden/mixins/common.rb
|
40
|
+
- lib/warden/proxy.rb
|
41
|
+
- lib/warden.rb
|
42
|
+
- spec/helpers
|
43
|
+
- spec/helpers/request_helper.rb
|
44
|
+
- spec/spec_helper.rb
|
45
|
+
- spec/warden
|
46
|
+
- spec/warden/authenticated_data_store_spec.rb
|
47
|
+
- spec/warden/errors_spec.rb
|
48
|
+
- spec/warden/hooks_spec.rb
|
49
|
+
- spec/warden/manager_spec.rb
|
50
|
+
- spec/warden/proxy_spec.rb
|
51
|
+
- spec/warden/strategies
|
52
|
+
- spec/warden/strategies/failz.rb
|
53
|
+
- spec/warden/strategies/invalid.rb
|
54
|
+
- spec/warden/strategies/pass.rb
|
55
|
+
- spec/warden/strategies/pass_without_user.rb
|
56
|
+
- spec/warden/strategies/password.rb
|
57
|
+
- spec/warden/strategies_spec.rb
|
58
|
+
- spec/warden/strategy_base_spec.rb
|
59
|
+
- spec/warden_spec.rb
|
60
|
+
has_rdoc: false
|
61
|
+
homepage: http://github.com/hassox/warden
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: "0"
|
72
|
+
version:
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: "0"
|
78
|
+
version:
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.2.0
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Rack middleware that provides authentication for rack applications
|
86
|
+
test_files: []
|
87
|
+
|