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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8484b357ba7880ae4d782c8755c817b2929c46ed
4
- data.tar.gz: 80ab747a444db352fb75707b7e8133ff87f95b81
3
+ metadata.gz: 9eea18851c1a1f6b33c99f09d77b8ddfae731479
4
+ data.tar.gz: 26303d90ab38ae009ba45c2002d296586dfc765d
5
5
  SHA512:
6
- metadata.gz: e5873ec981c95c5f7712a7fd36ea02cab60f9ce520a0c6772961aad36a017fd5f376c2ef9a33c05ddfb0c644ee4df4dce5585376ab37703d2395cba5c0e3cd76
7
- data.tar.gz: 3fb7325a8437f54dd584d18a42939f1847394b4470da5f33487408667d2a21a6a15a509a68abb20372e00446c0f45ae9e40bca93020e283627468115d8776f64
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
@@ -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 user_id = query_params[url_parameter]
26
- session[session_key] = user_id.to_s
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
@@ -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.0"
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}
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-back_door
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Doyle