warden 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.rdoc CHANGED
@@ -1,3 +1,7 @@
1
+ == Version 1.2.0 / 2012-05-08
2
+ * Deprecate warden_cookies since it was never functional
3
+ * Add support to serialize_from_session and serialize_into_session per scope
4
+
1
5
  == Version 1.1.1 / 2012-02-16
2
6
  * Allow run_callbacks as an option to set_user and user
3
7
 
data/README.textile CHANGED
@@ -1,11 +1,8 @@
1
1
  Please see the "Warden Wiki":http://wiki.github.com/hassox/warden for overview documentation.
2
2
 
3
- h2. Contributors
4
-
5
- I'm going to try and keep a list of all the contributors to this project. If I've missed your name please just let me know and I'll update it.
3
+ h2. Maintainers
6
4
 
7
5
  * Daniel Neighman (hassox)
8
- * Mick Staugaard (staugaard)
9
6
  * José Valim (josevalim)
10
- * Carlos Antonio da Silva (carlosantoniodasilva)
11
- * Justin Smestad (jsmestad)
7
+
8
+ "A list of all contributors is available on Github.":https://github.com/hassox/warden/contributors
data/lib/warden.rb CHANGED
@@ -33,8 +33,10 @@ module Warden
33
33
  unless Warden::Test::WardenHelpers === Warden
34
34
  Warden.extend Warden::Test::WardenHelpers
35
35
  Warden::Manager.on_request do |proxy|
36
- while blk = Warden._on_next_request.shift
37
- blk.call(proxy)
36
+ unless proxy.asset_request?
37
+ while blk = Warden._on_next_request.shift
38
+ blk.call(proxy)
39
+ end
38
40
  end
39
41
  end
40
42
  end
@@ -59,23 +59,32 @@ module Warden
59
59
  # Generally however complex object should not be stored in the session.
60
60
  # If possible store only a "key" of the user object that will allow you to reconstitute it.
61
61
  #
62
+ # You can supply different methods of serialization for different scopes by passing a scope symbol
63
+ #
62
64
  # Example:
63
65
  # Warden::Manager.serialize_into_session{ |user| user.id }
66
+ # # With Scope:
67
+ # Warden::Manager.serialize_into_session(:admin) { |user| user.id }
64
68
  #
65
69
  # :api: public
66
- def serialize_into_session(&block)
67
- Warden::SessionSerializer.send :define_method, :serialize, &block
70
+ def serialize_into_session(scope = nil, &block)
71
+ method_name = scope.nil? ? :serialize : "#{scope}_serialize"
72
+ Warden::SessionSerializer.send :define_method, method_name, &block
68
73
  end
69
74
 
70
75
  # Reconstitues the user from the session.
71
76
  # Use the results of user_session_key to reconstitue the user from the session on requests after the initial login
77
+ # You can supply different methods of de-serialization for different scopes by passing a scope symbol
72
78
  #
73
79
  # Example:
74
80
  # Warden::Manager.serialize_from_session{ |id| User.get(id) }
81
+ # # With Scope:
82
+ # Warden::Manager.serialize_from_session(:admin) { |id| AdminUser.get(id) }
75
83
  #
76
84
  # :api: public
77
- def serialize_from_session(&block)
78
- Warden::SessionSerializer.send :define_method, :deserialize, &block
85
+ def serialize_from_session(scope = nil, &block)
86
+ method_name = scope.nil? ? :deserialize : "#{scope}_deserialize"
87
+ Warden::SessionSerializer.send :define_method, method_name, &block
79
88
  end
80
89
  end
81
90
 
@@ -85,7 +94,7 @@ module Warden
85
94
  config[:intercept_401] && !env['warden'].custom_failure?
86
95
  end
87
96
 
88
- # When a request is unauthentiated, here's where the processing occurs.
97
+ # When a request is unauthenticated, here's where the processing occurs.
89
98
  # It looks at the result of the proxy to see if it's been executed and what action to take.
90
99
  # :api: private
91
100
  def process_unauthenticated(env, options={})
@@ -22,6 +22,7 @@ module Warden
22
22
  # when the response is streamed back from the app.
23
23
  # :api: public
24
24
  def warden_cookies
25
+ warn "warden_cookies was never functional and is going to be removed in next versions"
25
26
  env['warden.cookies'] ||= {}
26
27
  end # response
27
28
 
data/lib/warden/proxy.rb CHANGED
@@ -268,12 +268,18 @@ module Warden
268
268
  @custom_failure = true
269
269
  end
270
270
 
271
- # Check to see if the custom failur flag has been set
271
+ # Check to see if the custom failure flag has been set
272
272
  # :api: public
273
273
  def custom_failure?
274
274
  !!@custom_failure
275
275
  end
276
276
 
277
+ # Check to see if this is an asset request
278
+ # :api: public
279
+ def asset_request?
280
+ ::Warden::asset_paths.any? { |r| env['PATH_INFO'].to_s.match(r) }
281
+ end
282
+
277
283
  def inspect(*args)
278
284
  "Warden::Proxy:#{object_id} @config=#{@config.inspect}"
279
285
  end
@@ -22,13 +22,17 @@ module Warden
22
22
 
23
23
  def store(user, scope)
24
24
  return unless user
25
- session[key_for(scope)] = serialize(user)
25
+ method_name = "#{scope}_serialize"
26
+ specialized = respond_to?(method_name)
27
+ session[key_for(scope)] = specialized ? send(method_name, user) : serialize(user)
26
28
  end
27
29
 
28
30
  def fetch(scope)
29
31
  key = session[key_for(scope)]
30
32
  return nil unless key
31
- user = deserialize(key)
33
+
34
+ method_name = "#{scope}_deserialize"
35
+ user = respond_to?(method_name) ? send(method_name, key) : deserialize(key)
32
36
  delete(scope) unless user
33
37
  user
34
38
  end
@@ -41,4 +45,4 @@ module Warden
41
45
  session.delete(key_for(scope))
42
46
  end
43
47
  end # SessionSerializer
44
- end # Warden
48
+ end # Warden
@@ -4,6 +4,20 @@ module Warden
4
4
 
5
5
  module Test
6
6
  module WardenHelpers
7
+ # Returns list of regex objects that match paths expected to be an asset
8
+ # @see Warden::Proxy#asset_request?
9
+ # @api public
10
+ def asset_paths
11
+ @asset_paths ||= [/^\/assets\//]
12
+ end
13
+
14
+ # Sets list of regex objects that match paths expected to be an asset
15
+ # @see Warden::Proxy#asset_request?
16
+ # @api public
17
+ def asset_paths=(*vals)
18
+ @asset_paths = vals
19
+ end
20
+
7
21
  # Adds a block to be executed on the next request when the stack reaches warden.
8
22
  # The warden proxy is yielded to the block
9
23
  # @api public
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Warden
3
- VERSION = "1.1.1".freeze
3
+ VERSION = "1.2.0".freeze
4
4
  end
@@ -956,4 +956,29 @@ describe "dynamic default_strategies" do
956
956
  session['warden.user.baz.key'].should == "User"
957
957
  end
958
958
  end
959
- end
959
+
960
+ describe "#asset_request?" do
961
+ before(:each) do
962
+ @asset_regex = /^\/assets\//
963
+ ::Warden.asset_paths = @asset_regex
964
+ end
965
+
966
+ it "should return true if PATH_INFO is in asset list" do
967
+ env = env_with_params('/assets/fun.gif')
968
+ setup_rack(success_app).call(env)
969
+ proxy = env["warden"]
970
+
971
+ proxy.env['PATH_INFO'].should match(@asset_regex)
972
+ proxy.should be_asset_request
973
+ end
974
+
975
+ it "should return false if PATH_INFO is not in asset list" do
976
+ env = env_with_params('/home')
977
+ setup_rack(success_app).call(env)
978
+ proxy = env["warden"]
979
+
980
+ proxy.env['PATH_INFO'].should_not match(@asset_regex)
981
+ proxy.should_not be_asset_request
982
+ end
983
+ end
984
+ end
@@ -0,0 +1,123 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Warden::Manager do
5
+ before(:each) do
6
+ @env = env_with_params
7
+ @env['rack.session'] ||= {}
8
+ Warden::Manager.serialize_from_session { |k| k }
9
+ Warden::Manager.serialize_into_session { |u| u }
10
+ begin
11
+ Warden::SessionSerializer.send :remove_method, :admin_serialize
12
+ rescue
13
+ end
14
+ begin
15
+ Warden::SessionSerializer.send :remove_method, :admin_deserialize
16
+ rescue
17
+ end
18
+ end
19
+ after(:each) do
20
+ Warden::Manager.serialize_from_session { |k| k }
21
+ Warden::Manager.serialize_into_session { |u| u }
22
+ begin
23
+ Warden::SessionSerializer.send :remove_method, :admin_deserialize
24
+ Warden::SessionSerializer.send :remove_method, :admin_serialize
25
+ rescue
26
+ end
27
+ end
28
+
29
+ def serializer_respond_to?(name)
30
+ Warden::SessionSerializer.new(@env).respond_to? name
31
+ end
32
+
33
+ it "should respond to :serialize" do
34
+ serializer_respond_to?(:serialize).should == true
35
+ end
36
+
37
+ it "should respond to :deserialize" do
38
+ serializer_respond_to?(:deserialize).should == true
39
+ end
40
+
41
+ it "should respond to {scope}_deserialize if Manager.serialize_from_session is called with scope" do
42
+ Rack::Builder.new do
43
+ Warden::Manager.serialize_from_session ( :admin ) { |n| n }
44
+ end
45
+ serializer_respond_to?(:admin_deserialize).should == true
46
+ end
47
+
48
+ it "should respond to {scope}_serialize if Manager.serialize_into_session is called with scope" do
49
+ Rack::Builder.new do
50
+ Warden::Manager.serialize_into_session(:admin) { |n| n }
51
+ end
52
+ serializer_respond_to?(:admin_serialize).should == true
53
+ end
54
+
55
+ def initialize_with_scope(scope, &block)
56
+ Rack::Builder.new do
57
+ Warden::Manager.serialize_into_session(scope, &block)
58
+ end
59
+ end
60
+
61
+ it "should execute serialize if no {scope}_serialize is present" do
62
+ serialized_object = nil
63
+ initialize_with_scope(nil) do |user|
64
+ serialized_object = user
65
+ user
66
+ end
67
+ serializer = Warden::SessionSerializer.new(@env)
68
+ serializer.store("user", :admin)
69
+ serialized_object.should == "user"
70
+ end
71
+
72
+ it "should not have a {scope}_serialize by default" do
73
+ serializer_respond_to?(:admin_serialize).should == false
74
+ end
75
+
76
+ it "should execute {scope}_serialize when calling store with a scope" do
77
+ serialized_object = nil
78
+ initialize_with_scope(:admin) do |user|
79
+ serialized_object = user
80
+ user
81
+ end
82
+
83
+ serializer = Warden::SessionSerializer.new(@env)
84
+ serializer.store("user", :admin)
85
+ serialized_object.should == "user"
86
+ end
87
+
88
+
89
+ it "should execute {scope}_deserialize when calling store with a scope" do
90
+ serialized_object = nil
91
+
92
+ Rack::Builder.new do
93
+ Warden::Manager.serialize_from_session(:admin) do |key|
94
+ serialized_object = key
95
+ key
96
+ end
97
+ end
98
+
99
+ serializer = Warden::SessionSerializer.new(@env)
100
+ @env['rack.session'][serializer.key_for(:admin)] = "test"
101
+ serializer.fetch(:admin)
102
+
103
+ serialized_object.should == "test"
104
+ end
105
+
106
+ it "should execute deserialize if {scope}_deserialize is not present" do
107
+ serialized_object = nil
108
+
109
+ Rack::Builder.new do
110
+ Warden::Manager.serialize_from_session do |key|
111
+ serialized_object = key
112
+ key
113
+ end
114
+ end
115
+
116
+ serializer = Warden::SessionSerializer.new(@env)
117
+ @env['rack.session'][serializer.key_for(:admin)] = "test"
118
+ serializer.fetch(:admin)
119
+
120
+ serialized_object.should == "test"
121
+ end
122
+
123
+ end
@@ -87,4 +87,9 @@ describe Warden::Test::Helpers do
87
87
  $captures.should == [:run]
88
88
  end
89
89
 
90
+ describe "#asset_paths" do
91
+ it "should default asset_paths to anything asset path regex" do
92
+ Warden.asset_paths.should == [/^\/assets\//]
93
+ end
94
+ end
90
95
  end
@@ -64,4 +64,13 @@ describe Warden::Test::WardenHelpers do
64
64
  app.call(env_with_params)
65
65
  $captures.should == []
66
66
  end
67
+
68
+ context "asset requests" do
69
+ it "should not execute on_next_request blocks if this is an asset request" do
70
+ app = setup_rack(@app)
71
+ Warden.on_next_request{|w| $captures << :first }
72
+ app.call(env_with_params("/assets/fun.gif"))
73
+ $captures.should == []
74
+ end
75
+ end
67
76
  end
data/warden.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
12
12
  "LICENSE",
13
13
  "README.textile"
14
14
  ]
15
- s.files = Dir["**/*"] - Dir["*.gem"]
15
+ s.files = Dir["**/*"] - Dir["*.gem"] - ["Gemfile.lock"]
16
16
  s.homepage = %q{http://github.com/hassox/warden}
17
17
  s.rdoc_options = ["--charset=UTF-8"]
18
18
  s.require_paths = ["lib"]
metadata CHANGED
@@ -1,50 +1,36 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: warden
3
- version: !ruby/object:Gem::Version
4
- hash: 17
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 1
9
- - 1
10
- version: 1.1.1
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Daniel Neighman
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-07-27 00:00:00 +02:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2011-07-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: rack
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &2156244180 !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 15
30
- segments:
31
- - 1
32
- - 0
33
- version: "1.0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
34
22
  type: :runtime
35
- version_requirements: *id001
23
+ prerelease: false
24
+ version_requirements: *2156244180
36
25
  description:
37
26
  email: has.sox@gmail.com
38
27
  executables: []
39
-
40
28
  extensions: []
41
-
42
- extra_rdoc_files:
29
+ extra_rdoc_files:
43
30
  - LICENSE
44
31
  - README.textile
45
- files:
32
+ files:
46
33
  - Gemfile
47
- - Gemfile.lock
48
34
  - History.rdoc
49
35
  - lib/warden/config.rb
50
36
  - lib/warden/errors.rb
@@ -76,46 +62,37 @@ files:
76
62
  - spec/warden/hooks_spec.rb
77
63
  - spec/warden/manager_spec.rb
78
64
  - spec/warden/proxy_spec.rb
65
+ - spec/warden/scoped_session_serializer.rb
79
66
  - spec/warden/session_serializer_spec.rb
80
67
  - spec/warden/strategies/base_spec.rb
81
68
  - spec/warden/strategies_spec.rb
82
69
  - spec/warden/test/helpers_spec.rb
83
70
  - spec/warden/test/test_mode_spec.rb
84
- - TODO.textile
85
71
  - warden.gemspec
86
- has_rdoc: true
87
72
  homepage: http://github.com/hassox/warden
88
73
  licenses: []
89
-
90
74
  post_install_message:
91
- rdoc_options:
75
+ rdoc_options:
92
76
  - --charset=UTF-8
93
- require_paths:
77
+ require_paths:
94
78
  - lib
95
- required_ruby_version: !ruby/object:Gem::Requirement
79
+ required_ruby_version: !ruby/object:Gem::Requirement
96
80
  none: false
97
- requirements:
98
- - - ">="
99
- - !ruby/object:Gem::Version
100
- hash: 3
101
- segments:
102
- - 0
103
- version: "0"
104
- required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
86
  none: false
106
- requirements:
107
- - - ">="
108
- - !ruby/object:Gem::Version
109
- hash: 3
110
- segments:
111
- - 0
112
- version: "0"
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
113
91
  requirements: []
114
-
115
92
  rubyforge_project: warden
116
- rubygems_version: 1.5.3
93
+ rubygems_version: 1.8.15
117
94
  signing_key:
118
95
  specification_version: 3
119
96
  summary: Rack middleware that provides authentication for rack applications
120
97
  test_files: []
121
-
98
+ has_rdoc:
data/Gemfile.lock DELETED
@@ -1,32 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- warden (1.1.1)
5
- rack (>= 1.0)
6
-
7
- GEM
8
- remote: http://rubygems.org/
9
- specs:
10
- diff-lcs (1.1.2)
11
- rack (1.3.0)
12
- rack-test (0.6.0)
13
- rack (>= 1.0)
14
- rake (0.8.7)
15
- rspec (2.5.0)
16
- rspec-core (~> 2.5.0)
17
- rspec-expectations (~> 2.5.0)
18
- rspec-mocks (~> 2.5.0)
19
- rspec-core (2.5.1)
20
- rspec-expectations (2.5.0)
21
- diff-lcs (~> 1.1.2)
22
- rspec-mocks (2.5.0)
23
-
24
- PLATFORMS
25
- ruby
26
-
27
- DEPENDENCIES
28
- rack (= 1.3)
29
- rack-test
30
- rake
31
- rspec (~> 2)
32
- warden!
data/TODO.textile DELETED
@@ -1,2 +0,0 @@
1
- * Allow a spec / test mode where a _spec_authenticate! method is called on a strategy instead if present
2
- * Implement back urls