rack-screen-door 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -1
- data/lib/rack/screen_door.rb +10 -4
- data/lib/rack/screen_door/version.rb +1 -1
- data/spec/screen_door_spec.rb +38 -9
- metadata +28 -28
data/README.md
CHANGED
@@ -30,6 +30,7 @@ Additional options can be supplied:
|
|
30
30
|
:template_path => 'path/to/my_screen_door.html.erb',
|
31
31
|
:cookie_key => 'my_cookie_name',
|
32
32
|
:cookie_hash => { :domain => '.example.org', :secure => true },
|
33
|
-
:expires => 1.year
|
33
|
+
:expires => 1.year,
|
34
|
+
:bypass_if => lambda { |request| request.path == '/safe_area' }
|
34
35
|
|
35
36
|
See [default_template.html.erb](https://github.com/patdeegan/rack-screen-door/blob/master/default_template.html.erb) as an example template file.
|
data/lib/rack/screen_door.rb
CHANGED
@@ -15,7 +15,7 @@ module Rack
|
|
15
15
|
DEFAULT_SALT = 'SaltySalt'
|
16
16
|
|
17
17
|
attr_reader :app, :cookie_key, :cookie_hash, :expires
|
18
|
-
attr_reader :template_path, :salt, :answer
|
18
|
+
attr_reader :template_path, :salt, :answer, :bypass_if
|
19
19
|
attr_reader :redirect_url, :error
|
20
20
|
|
21
21
|
# Creates the middleware.
|
@@ -24,10 +24,14 @@ module Rack
|
|
24
24
|
# @param [String] answer secret answer
|
25
25
|
# @param [Hash] options options
|
26
26
|
# @option options [String] :salt (DEFAULT_SALT) a server-side secret
|
27
|
-
# @option options [String] :template_path (DEFAULT_TEMPLATE_PATH) the path to
|
27
|
+
# @option options [String] :template_path (DEFAULT_TEMPLATE_PATH) the path to
|
28
|
+
# an HTML or ERB file to render
|
28
29
|
# @option options [String] :cookie_key (DEFAULT_COOKIE_KEY) the cookie key (name)
|
29
|
-
# @option options [Hash] :cookie_hash (DEFAULT_COOKIE_HASH) cookie
|
30
|
+
# @option options [Hash] :cookie_hash (DEFAULT_COOKIE_HASH) cookie
|
31
|
+
# options that will be passed to Rack::Response#set_cookie
|
30
32
|
# @option options [Integer] :expires (DEFAULT_EXPIRES) how long a cookie will persist
|
33
|
+
# @option options [Proc] :bypass_if a block that gets passed `request`;
|
34
|
+
# truthy return values will be allowed even without a cookie.
|
31
35
|
def initialize(app, answer, options = {})
|
32
36
|
@app = app
|
33
37
|
@answer = answer
|
@@ -36,6 +40,7 @@ module Rack
|
|
36
40
|
@cookie_key = options[:cookie_key] || DEFAULT_COOKIE_KEY
|
37
41
|
@cookie_hash = DEFAULT_COOKIE_HASH.merge(options[:cookie_hash] || {})
|
38
42
|
@expires = options[:expires] || DEFAULT_EXPIRES
|
43
|
+
@bypass_if = options[:bypass_if]
|
39
44
|
end
|
40
45
|
|
41
46
|
# Rack middleware chain.
|
@@ -49,7 +54,8 @@ module Rack
|
|
49
54
|
|
50
55
|
def _call(env)
|
51
56
|
request = Rack::Request.new(env)
|
52
|
-
return app.call(env) if verified_cookie?(request)
|
57
|
+
return app.call(env) if verified_cookie?(request) ||
|
58
|
+
(bypass_if && bypass_if.call(request))
|
53
59
|
response = Rack::Response.new
|
54
60
|
if request.post?
|
55
61
|
@redirect_url = request.params['redirect'] || '/'
|
data/spec/screen_door_spec.rb
CHANGED
@@ -29,6 +29,7 @@ describe Rack::ScreenDoor do
|
|
29
29
|
its(:expires) { should == 60 * 60 * 24 * 30 }
|
30
30
|
its(:answer) { should == penguin_answer }
|
31
31
|
its(:hashed_answer) { should == penguin_hash }
|
32
|
+
its(:bypass_if) { should be_nil }
|
32
33
|
|
33
34
|
end
|
34
35
|
|
@@ -38,14 +39,6 @@ describe Rack::ScreenDoor do
|
|
38
39
|
|
39
40
|
before { clear_cookies }
|
40
41
|
|
41
|
-
def cookie_value
|
42
|
-
rack_mock_session.cookie_jar[default_cookie_key]
|
43
|
-
end
|
44
|
-
|
45
|
-
def headers
|
46
|
-
last_response.headers
|
47
|
-
end
|
48
|
-
|
49
42
|
context 'GET /blah' do
|
50
43
|
|
51
44
|
before { get '/blah' }
|
@@ -121,6 +114,42 @@ describe Rack::ScreenDoor do
|
|
121
114
|
|
122
115
|
end
|
123
116
|
|
124
|
-
|
117
|
+
context 'bypass_if is set' do
|
118
|
+
|
119
|
+
let(:app) do
|
120
|
+
Rack::ScreenDoor.new(default_app, penguin_answer,
|
121
|
+
:bypass_if => lambda { |request| request.path == '/safe_area' }
|
122
|
+
)
|
123
|
+
end
|
124
|
+
|
125
|
+
context 'GET /blah' do
|
126
|
+
|
127
|
+
before { get '/blah' }
|
128
|
+
it { should be_ok }
|
129
|
+
its(:body) { should include(default_question) }
|
130
|
+
specify { cookie_value.should be_nil }
|
131
|
+
|
132
|
+
end
|
133
|
+
|
134
|
+
context 'GET /safe_area' do
|
135
|
+
|
136
|
+
before { get '/safe_area' }
|
137
|
+
it { should be_ok }
|
138
|
+
its(:body) { should == default_app_body }
|
139
|
+
specify { cookie_value.should be_nil }
|
140
|
+
|
141
|
+
end
|
125
142
|
|
143
|
+
end
|
144
|
+
|
145
|
+
def cookie_value
|
146
|
+
rack_mock_session.cookie_jar[default_cookie_key]
|
147
|
+
end
|
148
|
+
|
149
|
+
def headers
|
150
|
+
last_response.headers
|
151
|
+
end
|
152
|
+
|
153
|
+
end
|
154
|
+
|
126
155
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-screen-door
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- John Nishinaga
|
@@ -15,10 +15,11 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-02-14 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
|
-
|
21
|
+
type: :runtime
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
22
23
|
none: false
|
23
24
|
requirements:
|
24
25
|
- - ">="
|
@@ -27,12 +28,12 @@ dependencies:
|
|
27
28
|
segments:
|
28
29
|
- 0
|
29
30
|
version: "0"
|
30
|
-
requirement: *id001
|
31
|
-
prerelease: false
|
32
31
|
name: rack
|
33
|
-
|
32
|
+
version_requirements: *id001
|
33
|
+
prerelease: false
|
34
34
|
- !ruby/object:Gem::Dependency
|
35
|
-
|
35
|
+
type: :development
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
37
|
none: false
|
37
38
|
requirements:
|
38
39
|
- - ">="
|
@@ -41,12 +42,12 @@ dependencies:
|
|
41
42
|
segments:
|
42
43
|
- 0
|
43
44
|
version: "0"
|
44
|
-
requirement: *id002
|
45
|
-
prerelease: false
|
46
45
|
name: rake
|
47
|
-
|
46
|
+
version_requirements: *id002
|
47
|
+
prerelease: false
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
|
-
|
49
|
+
type: :development
|
50
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
50
51
|
none: false
|
51
52
|
requirements:
|
52
53
|
- - ">="
|
@@ -55,12 +56,12 @@ dependencies:
|
|
55
56
|
segments:
|
56
57
|
- 0
|
57
58
|
version: "0"
|
58
|
-
requirement: *id003
|
59
|
-
prerelease: false
|
60
59
|
name: rack-test
|
61
|
-
|
60
|
+
version_requirements: *id003
|
61
|
+
prerelease: false
|
62
62
|
- !ruby/object:Gem::Dependency
|
63
|
-
|
63
|
+
type: :development
|
64
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
64
65
|
none: false
|
65
66
|
requirements:
|
66
67
|
- - ">="
|
@@ -69,12 +70,12 @@ dependencies:
|
|
69
70
|
segments:
|
70
71
|
- 0
|
71
72
|
version: "0"
|
72
|
-
requirement: *id004
|
73
|
-
prerelease: false
|
74
73
|
name: rspec
|
75
|
-
|
74
|
+
version_requirements: *id004
|
75
|
+
prerelease: false
|
76
76
|
- !ruby/object:Gem::Dependency
|
77
|
-
|
77
|
+
type: :development
|
78
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
78
79
|
none: false
|
79
80
|
requirements:
|
80
81
|
- - ">="
|
@@ -83,12 +84,12 @@ dependencies:
|
|
83
84
|
segments:
|
84
85
|
- 0
|
85
86
|
version: "0"
|
86
|
-
requirement: *id005
|
87
|
-
prerelease: false
|
88
87
|
name: yard
|
89
|
-
|
88
|
+
version_requirements: *id005
|
89
|
+
prerelease: false
|
90
90
|
- !ruby/object:Gem::Dependency
|
91
|
-
|
91
|
+
type: :development
|
92
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
92
93
|
none: false
|
93
94
|
requirements:
|
94
95
|
- - ">="
|
@@ -97,10 +98,9 @@ dependencies:
|
|
97
98
|
segments:
|
98
99
|
- 0
|
99
100
|
version: "0"
|
100
|
-
requirement: *id006
|
101
|
-
prerelease: false
|
102
101
|
name: redcarpet
|
103
|
-
|
102
|
+
version_requirements: *id006
|
103
|
+
prerelease: false
|
104
104
|
description: Rack middleware for simple question and answer authorization.
|
105
105
|
email:
|
106
106
|
- jingoro@casa-z.org
|