warden 0.9.7 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,9 @@
1
+ == Version 0.10.0 / 2010-03-22
2
+ * Allow default strategies to be set on the proxy
3
+ * Provide each scope with it's own default strategies
4
+ * Provide each scope with default set_user opts
5
+ * depricate the Proxy#default_strategies= method
6
+
1
7
  == Version 0.9.5 / 2010-02-28
2
8
 
3
9
  * Add Warden.test_mode!
data/Rakefile CHANGED
@@ -28,5 +28,5 @@ task :default => :spec
28
28
  desc "Run specs"
29
29
  Spec::Rake::SpecTask.new do |t|
30
30
  t.spec_files = FileList['spec/**/*_spec.rb']
31
- t.spec_opts = %w(-fs --color)
31
+ t.spec_opts = %w(-fs --color --backtrace)
32
32
  end
@@ -36,8 +36,9 @@ module Warden
36
36
 
37
37
  def initialize(other={})
38
38
  merge!(other)
39
- self[:default_scope] ||= :default
40
- self[:default_strategies] ||= []
39
+ self[:default_scope] ||= :default
40
+ self[:default_scope_options] ||= {}
41
+ self[:default_strategies] ||= {}
41
42
  end
42
43
 
43
44
  # Do not raise an error if a missing strategy is given by default.
@@ -53,13 +54,35 @@ module Warden
53
54
  # Set the default strategies to use.
54
55
  # :api: public
55
56
  def default_strategies(*strategies)
57
+ opts = Hash === strategies.last ? strategies.pop : {}
58
+ scope = opts[:scope] || default_scope
56
59
  if strategies.empty?
57
- self[:default_strategies]
60
+ self[:default_strategies][scope]
58
61
  else
59
- self[:default_strategies] = strategies.flatten
62
+ self[:default_strategies][scope] = strategies.flatten
60
63
  end
61
64
  end
62
65
 
66
+ # Set the default options that are passed to set_user. This is configured
67
+ # during the setup phase and is used throughout.
68
+ def default_scope_options(scope = default_scope, opts = nil)
69
+ if opts.nil?
70
+ # We're reading the default options for this scope
71
+ self[:default_scope_options][scope] ||= {}
72
+ else
73
+ # We're setting the default options forthe scope
74
+ self[:default_scope_options][scope] = opts
75
+ end
76
+ end
77
+
78
+ # A short hand way to set up a particular scope
79
+ def scope_defaults(scope, opts = {})
80
+ strategies = opts.delete(:strategies) || []
81
+ default_strategies(strategies, :scope => scope)
82
+ default_scope_options(scope, opts)
83
+ true
84
+ end
85
+
63
86
  # Quick accessor to strategies from manager
64
87
  # :api: public
65
88
  def strategies
@@ -78,4 +101,4 @@ module Warden
78
101
  Warden::Manager.serialize_from_session(*args, &block)
79
102
  end
80
103
  end
81
- end
104
+ end
@@ -18,7 +18,10 @@ module Warden
18
18
  # configure the Warden::Manager.
19
19
  # :api: public
20
20
  def initialize(app, options={})
21
+ default_strategies = options.delete(:default_strategies)
22
+
21
23
  @app, @config = app, Warden::Config.new(options)
24
+ @config.default_strategies *default_strategies if default_strategies
22
25
  yield @config if block_given?
23
26
  self
24
27
  end
@@ -37,13 +40,14 @@ module Warden
37
40
  result ||= {}
38
41
  case result
39
42
  when Array
40
- if result.first == 401 && !env['warden'].custom_failure?
41
- process_unauthenticated(env)
43
+ if result.first == 401
44
+ process_unauthenticated({:original_response => result, :action => :unauthenticated}, env)
42
45
  else
43
46
  result
44
47
  end
45
48
  when Hash
46
- process_unauthenticated(env, result || {})
49
+ result[:action] ||= :unauthenticated
50
+ process_unauthenticated(result, env)
47
51
  end
48
52
  end
49
53
 
@@ -83,33 +87,30 @@ module Warden
83
87
  # When a request is unauthentiated, here's where the processing occurs.
84
88
  # It looks at the result of the proxy to see if it's been executed and what action to take.
85
89
  # :api: private
86
- def process_unauthenticated(env, options={})
87
- options[:action] ||= :unauthenticated
88
-
89
- proxy = env['warden']
90
- result = options[:result] || proxy.result
90
+ def process_unauthenticated(result, env)
91
+ action = result[:result] || env['warden'].result
91
92
 
92
- case result
93
- when :redirect
94
- body = proxy.message || "You are being redirected to #{proxy.headers['Location']}"
95
- [proxy.status, proxy.headers, [body]]
96
- when :custom
97
- proxy.custom_response
98
- else
99
- call_failure_app(env, options)
93
+ case action
94
+ when :redirect
95
+ [env['warden'].status, env['warden'].headers, [env['warden'].message || "You are being redirected to #{env['warden'].headers['Location']}"]]
96
+ when :custom
97
+ env['warden'].custom_response
98
+ else
99
+ call_failure_app(env, result)
100
100
  end
101
101
  end
102
102
 
103
103
  # Calls the failure app.
104
104
  # The before_failure hooks are run on each failure
105
105
  # :api: private
106
- def call_failure_app(env, options = {})
107
- if config.failure_app
108
- options.merge!(:attempted_path => ::Rack::Request.new(env).fullpath)
109
- env["PATH_INFO"] = "/#{options[:action]}"
110
- env["warden.options"] = options
106
+ def call_failure_app(env, opts = {})
107
+ if env['warden'].custom_failure?
108
+ opts[:original_response]
109
+ elsif config.failure_app
110
+ env["PATH_INFO"] = "/#{opts[:action]}"
111
+ env["warden.options"] = opts
111
112
 
112
- _run_callbacks(:before_failure, env, options)
113
+ _run_callbacks(:before_failure, env, opts)
113
114
  config.failure_app.call(env).to_a
114
115
  else
115
116
  raise "No Failure App provided"
@@ -1,8 +1,11 @@
1
1
  # encoding: utf-8
2
+ require 'warden/proxy_deprecation'
3
+
2
4
  module Warden
3
5
  class UserNotSet < RuntimeError; end
4
6
 
5
7
  class Proxy
8
+ include ProxyDeprecation
6
9
  # An accessor to the winning strategy
7
10
  # :api: private
8
11
  attr_accessor :winning_strategy
@@ -66,14 +69,23 @@ module Warden
66
69
  # By changing this value, you can change the default strategies for a downstream branch of you rack graph.
67
70
  #
68
71
  # @api public
69
- def default_strategies
70
- @default_strategies ||= config.default_strategies.dup
72
+ def default_strategies(*strategies)
73
+ scope, opts = _retrieve_scope_and_opts(strategies)
74
+ if strategies.empty?
75
+ _default_strategies[scope] ||= begin
76
+ (
77
+ @config.default_strategies(:scope => scope) ||
78
+ @config.default_strategies(:scope => @config.default_scope)
79
+ ).dup
80
+ end
81
+ else
82
+ _default_strategies[scope] = strategies.flatten
83
+ end
84
+ _default_strategies[scope]
71
85
  end
72
86
 
73
- # Allows you to set the default strategies for this request.
74
- # @api public
75
- def default_strategies=(*strategies)
76
- @default_strategies = strategies.flatten
87
+ def _default_strategies
88
+ @default_strategies ||= {}
77
89
  end
78
90
 
79
91
  # Run the authentiation strategies for the given strategies.
@@ -154,6 +166,12 @@ module Warden
154
166
  return unless user
155
167
  scope = (opts[:scope] ||= @config.default_scope)
156
168
 
169
+ # Get the default options from the master configuration for the given scope
170
+ opts = opts.dup
171
+ if @config.default_scope_options(scope)
172
+ opts = @config.default_scope_options(scope).merge(opts)
173
+ end
174
+
157
175
  @users[scope] = user
158
176
  session_serializer.store(user, scope) unless opts[:store] == false
159
177
 
@@ -281,7 +299,8 @@ module Warden
281
299
  # Run the strategies for a given scope
282
300
  def _run_strategies_for(scope, args) #:nodoc:
283
301
  self.winning_strategy = nil
284
- strategies = args.empty? ? default_strategies : args
302
+ strategies = args.empty? ? default_strategies(:scope => scope) : args
303
+ puts strategies.inspect
285
304
 
286
305
  strategies.each do |name|
287
306
  strategy = _fetch_strategy(name, scope)
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ module Warden
3
+ # Sets up a place for deprecation of methods from the main proxy
4
+ module ProxyDeprecation
5
+ def default_strategies=(*strategies)
6
+ warn "[DEPRECATION] warden.default_strateiges= is deprecated. Instead use warden.set_default_strategies(*strategies) with an optional :scope => :scope)"
7
+ strategies.push(:scope => @config.default_scope)
8
+ set_default_strategies(*strategies)
9
+ end
10
+ end
11
+ end
@@ -121,7 +121,6 @@ module Warden
121
121
  # You must throw an :warden symbol somewhere in the application to enforce this
122
122
  # :api: public
123
123
  def fail!(message = "Failed to Login")
124
- halt!
125
124
  @message = message
126
125
  @result = :failure
127
126
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Warden
3
- VERSION = "0.9.7".freeze
3
+ VERSION = "0.10.0".freeze
4
4
  end
@@ -38,4 +38,11 @@ describe Warden::Config do
38
38
  it "should merge given options on initialization" do
39
39
  Warden::Config.new(:foo => :bar)[:foo].should == :bar
40
40
  end
41
+
42
+ it "should setup defaults with the scope_defaults method" do
43
+ c = Warden::Config.new
44
+ c.scope_defaults :foo, :strategies => [:foo, :bar], :store => false
45
+ c.default_strategies(:scope => :foo).should == [:foo, :bar]
46
+ c.default_scope_options(:foo).should == {:store => false}
47
+ end
41
48
  end
@@ -69,17 +69,6 @@ describe Warden::Manager do
69
69
  result.first.should == 401
70
70
  result.last.should == ["You Fail!"]
71
71
  end
72
-
73
- it "should set the attempted url in warden.options hash" do
74
- env = env_with_params("/access/path", {})
75
- app = lambda do |env|
76
- env['warden'].authenticate(:pass)
77
- throw(:warden)
78
- end
79
- result = setup_rack(app, :failure_app => @fail_app).call(env)
80
- result.first.should == 401
81
- env["warden.options"][:attempted_path].should == "/access/path"
82
- end
83
72
  end # failure
84
73
 
85
74
  end
@@ -672,8 +672,8 @@ describe "dynamic default_strategies" do
672
672
  end
673
673
 
674
674
  before(:each) do
675
- $captures = []
676
675
  @app = lambda{|e| e['warden'].authenticate! }
676
+ $captures = []
677
677
  end
678
678
 
679
679
  def wrap_app(app, &blk)
@@ -688,7 +688,7 @@ describe "dynamic default_strategies" do
688
688
  app = wrap_app(@app) do |e|
689
689
  e['warden'].default_strategies.should == [:password]
690
690
  e['warden'].config.default_strategies.should == [:password]
691
- e['warden'].default_strategies = [:one]
691
+ e['warden'].default_strategies :one
692
692
  e['warden'].authenticate!
693
693
  Rack::Response.new("OK").finish
694
694
  end
@@ -708,6 +708,45 @@ describe "dynamic default_strategies" do
708
708
 
709
709
  $captures.should == [:one]
710
710
  end
711
+
712
+ it "should allow me to set the default strategies on a per scope basis" do
713
+ app = wrap_app(@app) do |e|
714
+ w = e['warden']
715
+ w.default_strategies(:two, :one, :scope => :foo)
716
+ w.default_strategies(:two, :scope => :default)
717
+ w.default_strategies(:scope => :foo).should == [:two, :one]
718
+ w.authenticate(:scope => :foo)
719
+ $captures.should == [:two, :one]
720
+ $captures.clear
721
+ w.authenticate
722
+ $captures.should == [:two]
723
+ end
724
+ setup_rack(app).call(env_with_params)
725
+ $captures.should == [:two]
726
+ end
727
+
728
+ it "should allow me to setup default strategies for each scope on the manager" do
729
+ builder = Rack::Builder.new do
730
+ use Warden::Spec::Helpers::Session
731
+ use Warden::Manager do |config|
732
+ config.default_strategies :one
733
+ config.default_strategies :two, :one, :scope => :foo
734
+ config.failure_app = Warden::Spec::Helpers::FAILURE_APP
735
+ end
736
+ run(lambda do |e|
737
+ w = e['warden']
738
+ w.authenticate
739
+ $captures.should == [:one]
740
+ $captures.clear
741
+ w.authenticate(:scope => :foo)
742
+ $captures.should == [:two, :one]
743
+ $captures << :complete
744
+ end)
745
+ end
746
+ builder.to_app.call(env_with_params)
747
+ $captures.should include(:complete)
748
+ end
749
+
711
750
  it "should not change the master configurations strategies when I change them" do
712
751
  app = wrap_app(@app) do |e|
713
752
  e['warden'].default_strategies << :one
@@ -720,4 +759,43 @@ describe "dynamic default_strategies" do
720
759
 
721
760
  $captures.should == [:one]
722
761
  end
762
+
763
+ describe "default scope options" do
764
+ it "should allow me to set store, false on a given scope" do
765
+ $captures = []
766
+ builder = Rack::Builder.new do
767
+ use Warden::Manager do |config|
768
+ config.default_strategies :one
769
+ config.default_strategies :two, :one, :scope => :foo
770
+ config.default_strategies :two, :one, :scope => :bar
771
+
772
+ config.default_scope_options :bar, :store => false
773
+ config.default_scope_options :baz, :store => false
774
+ config.failure_app = Warden::Spec::Helpers::FAILURE_APP
775
+ end
776
+ run(lambda do |e|
777
+ w = e['warden']
778
+ w.authenticate
779
+ w.authenticate(:scope => :foo)
780
+ w.authenticate(:one, :scope => :bar)
781
+ w.authenticate(:one, :scope => :baz, :store => true)
782
+ w.user.should == "User"
783
+ w.user(:foo).should == "User"
784
+ w.user(:bar).should == "User"
785
+ w.user(:baz).should == "User"
786
+ $captures << :complete
787
+ Rack::Response.new("OK").finish
788
+ end)
789
+ end
790
+ env = env_with_params
791
+ session = env["rack.session"] = {}
792
+ builder.to_app.call(env)
793
+ $captures.should include(:complete)
794
+ session['warden.user.default.key'].should == "User"
795
+ session['warden.user.foo.key'].should == "User"
796
+ session.key?('warden.user.bar.key').should be_false
797
+ session['warden.user.bar.key'].should be_nil
798
+ session['warden.user.baz.key'].should == "User"
799
+ end
800
+ end
723
801
  end
@@ -203,9 +203,9 @@ describe Warden::Strategies::Base do
203
203
  @str.user.should be_nil
204
204
  end
205
205
 
206
- it "should halt the strategies when failing" do
206
+ it "should not halt the strategies when failing" do
207
207
  @str._run!
208
- @str.should be_halted
208
+ @str.should_not be_halted
209
209
  end
210
210
 
211
211
  it "should allow you to set a message when failing" do
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{warden}
8
- s.version = "0.9.7"
8
+ s.version = "0.10.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Daniel Neighman"]
12
- s.date = %q{2010-03-11}
12
+ s.date = %q{2010-03-22}
13
13
  s.email = %q{has.sox@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE",
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
30
30
  "lib/warden/manager_deprecation.rb",
31
31
  "lib/warden/mixins/common.rb",
32
32
  "lib/warden/proxy.rb",
33
+ "lib/warden/proxy_deprecation.rb",
33
34
  "lib/warden/session_serializer.rb",
34
35
  "lib/warden/strategies.rb",
35
36
  "lib/warden/strategies/base.rb",
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 9
8
- - 7
9
- version: 0.9.7
7
+ - 10
8
+ - 0
9
+ version: 0.10.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Daniel Neighman
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-11 00:00:00 +01:00
17
+ date: 2010-03-22 00:00:00 +11:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -69,6 +69,7 @@ files:
69
69
  - lib/warden/manager_deprecation.rb
70
70
  - lib/warden/mixins/common.rb
71
71
  - lib/warden/proxy.rb
72
+ - lib/warden/proxy_deprecation.rb
72
73
  - lib/warden/session_serializer.rb
73
74
  - lib/warden/strategies.rb
74
75
  - lib/warden/strategies/base.rb