rack-back_door 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -0
- data/lib/rack/back_door.rb +14 -4
- data/rack-back_door.gemspec +1 -1
- data/test/rack/back_door_test.rb +11 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9eea18851c1a1f6b33c99f09d77b8ddfae731479
|
4
|
+
data.tar.gz: 26303d90ab38ae009ba45c2002d296586dfc765d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d9a3be51f5ad658b9f7339d4a8966bba3b8c46f6957f1b2455745a92f489522e257c1796cc64f47f44eaffaaedaa49a1f82510623662f6430d6486bb00f74b6
|
7
|
+
data.tar.gz: 5bcb095302ffebf29aec740416c6157cb540e9ffa362eefeee6b67d07b871797040f92dabab2d5031add4335cb171af8aa6e858a2bb8750182bc911e43900058
|
data/README.md
CHANGED
@@ -120,6 +120,17 @@ MyApplication::Application.configure do |config|
|
|
120
120
|
end
|
121
121
|
```
|
122
122
|
|
123
|
+
Additionally, you can override the middleware to alter the `rack` request any
|
124
|
+
way you see fit by configuring the `middleware` with a block.
|
125
|
+
|
126
|
+
For instance, in `sinatra`:
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
use Rack::BackDoor do |env, user_id|
|
130
|
+
env['my.user'] = User.find(user_id)
|
131
|
+
end
|
132
|
+
```
|
133
|
+
|
123
134
|
## Contributing
|
124
135
|
|
125
136
|
1. Fork it
|
data/lib/rack/back_door.rb
CHANGED
@@ -3,10 +3,14 @@ require "rack/utils"
|
|
3
3
|
# Force a User to be authenticated
|
4
4
|
# ala http://robots.thoughtbot.com/post/37907699673/faster-tests-sign-in-through-the-back-door
|
5
5
|
class Rack::BackDoor
|
6
|
-
def initialize(app, options = {})
|
6
|
+
def initialize(app, options = {}, &block)
|
7
7
|
@app = app
|
8
8
|
@url_parameter = options.fetch(:url_parameter, "as")
|
9
9
|
@session_key = options.fetch(:session_key, "user_id")
|
10
|
+
|
11
|
+
if block
|
12
|
+
@sign_in = Proc.new(&block)
|
13
|
+
end
|
10
14
|
end
|
11
15
|
|
12
16
|
def call(env)
|
@@ -19,14 +23,20 @@ class Rack::BackDoor
|
|
19
23
|
|
20
24
|
private
|
21
25
|
|
22
|
-
attr_reader :app, :url_parameter, :session_key, :env
|
26
|
+
attr_reader :app, :url_parameter, :session_key, :env, :sign_in
|
23
27
|
|
24
28
|
def sign_in_through_the_back_door
|
25
|
-
if
|
26
|
-
|
29
|
+
if sign_in
|
30
|
+
sign_in.call(env, user_id)
|
31
|
+
else
|
32
|
+
session[session_key] = user_id
|
27
33
|
end
|
28
34
|
end
|
29
35
|
|
36
|
+
def user_id
|
37
|
+
query_params[url_parameter]
|
38
|
+
end
|
39
|
+
|
30
40
|
def session
|
31
41
|
env['rack.session'] ||= {}
|
32
42
|
end
|
data/rack-back_door.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "rack-back_door"
|
7
|
-
spec.version = "0.1.
|
7
|
+
spec.version = "0.1.1"
|
8
8
|
spec.authors = ["Sean Doyle"]
|
9
9
|
spec.email = ["sean.p.doyle24@gmail.com"]
|
10
10
|
spec.summary = %q{Inject an authenticated user into a Rack session}
|
data/test/rack/back_door_test.rb
CHANGED
@@ -41,6 +41,17 @@ class Rack::BackDoorTest < Minitest::Test
|
|
41
41
|
assert env_authenticated_with?(env, "user" => 1)
|
42
42
|
end
|
43
43
|
|
44
|
+
def test_sign_into_back_door_configurable
|
45
|
+
middleware = Rack::BackDoor.new(app, session_key: "user") do |env, user_id|
|
46
|
+
env["custom.thingy"] = user_id
|
47
|
+
end
|
48
|
+
request = request_for("http://example.com?as=1")
|
49
|
+
|
50
|
+
_, env = middleware.call(request)
|
51
|
+
|
52
|
+
assert_equal "1", env["custom.thingy"]
|
53
|
+
end
|
54
|
+
|
44
55
|
def request_for(url, **options)
|
45
56
|
Rack::MockRequest.env_for(url, options)
|
46
57
|
end
|