warden 0.7.0 → 0.8.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.
data/History.rdoc CHANGED
@@ -1,3 +1,14 @@
1
+ == Version 0.8.0 / 2010-01-06
2
+
3
+ * enhancements
4
+ * Add conditionals to callbacks (josevalim)
5
+ * Extract Warden::Config from Warden::Manager (josevalim)
6
+
7
+ == Version 0.7.0 / 2010-01-04
8
+
9
+ * enhancements
10
+ * Expose config in warden proxy (hassox)
11
+
1
12
  == Version 0.6.0 / 2009-11-16
2
13
 
3
14
  * enhancements
@@ -6,9 +17,9 @@
6
17
  * deprecation
7
18
  * serializer_into_session and serializer_from_session are deprecated, overwrite serialize and deserializer in Warden::Serializers::Session instead (josevalim)
8
19
 
9
- == Version 0.5.3 / 2009-11-20
20
+ == Version 0.5.3 / 2009-11-10
10
21
  * bug fixes
11
- * authenticated? and unauthenticated? should return true or false, not the user or false.
22
+ * authenticated? and unauthenticated? should return true or false, not the user or false. (hassox)
12
23
 
13
24
  == Version 0.5.2 / 2009-11-09
14
25
  * enhancements
@@ -0,0 +1,95 @@
1
+ module Warden
2
+ # This is a class which is yielded on use Warden::Manager. If you have a plugin
3
+ # and wants to add more configuration to warden, you just need to extend this
4
+ # class.
5
+ class Config < Hash
6
+ # Creates an accessor that simply sets and reads a key in the hash:
7
+ #
8
+ # class Config < Hash
9
+ # hash_accessor :failure_app
10
+ # end
11
+ #
12
+ # config = Config.new
13
+ # config.failure_app = Foo
14
+ # config[:failure_app] #=> Foo
15
+ #
16
+ # config[:failure_app] = Bar
17
+ # config.failure_app #=> Bar
18
+ #
19
+ def self.hash_accessor(*names) #:nodoc:
20
+ names.each do |name|
21
+ class_eval <<-METHOD, __FILE__, __LINE__ + 1
22
+ def #{name}
23
+ self[:#{name}]
24
+ end
25
+
26
+ def #{name}=(value)
27
+ self[:#{name}] = value
28
+ end
29
+ METHOD
30
+ end
31
+ end
32
+
33
+ hash_accessor :failure_app, :default_scope
34
+
35
+ def initialize(other={})
36
+ merge!(other)
37
+
38
+ self[:default_scope] ||= :default
39
+ self[:default_strategies] ||= []
40
+ self[:default_serializers] ||= [ :session ]
41
+ end
42
+
43
+ # Do not raise an error if a missing strategy is given by default.
44
+ # :api: plugin
45
+ def silence_missing_strategies!
46
+ self[:silence_missing_strategies] = true
47
+ end
48
+
49
+ def silence_missing_strategies? #:nodoc:
50
+ !!self[:silence_missing_strategies]
51
+ end
52
+
53
+ # Do not raise an error if a missing serializer is given by default.
54
+ # :api: plugin
55
+ def silence_missing_serializers!
56
+ self[:silence_missing_serializers] = true
57
+ end
58
+
59
+ def silence_missing_serializers? #:nodoc:
60
+ !!self[:silence_missing_serializers]
61
+ end
62
+
63
+ # Set the default strategies to use.
64
+ # :api: public
65
+ def default_strategies(*strategies)
66
+ if strategies.empty?
67
+ self[:default_strategies]
68
+ else
69
+ self[:default_strategies] = strategies.flatten
70
+ end
71
+ end
72
+
73
+ # Set the default serializers to use. By default, only session is enabled.
74
+ # :api: public
75
+ def default_serializers(*serializers)
76
+ if serializers.empty?
77
+ self[:default_serializers]
78
+ else
79
+ self[:default_serializers] = serializers.flatten
80
+ end
81
+ end
82
+
83
+ # Quick accessor to strategies from manager
84
+ # :api: public
85
+ def serializers
86
+ Warden::Serializers
87
+ end
88
+
89
+ # Quick accessor to strategies from manager
90
+ # :api: public
91
+ def strategies
92
+ Warden::Strategies
93
+ end
94
+ end
95
+ end
data/lib/warden/hooks.rb CHANGED
@@ -2,11 +2,32 @@
2
2
  module Warden
3
3
  module Hooks
4
4
 
5
+ # Hook to _run_callbacks asserting for conditions.
6
+ def _run_callbacks(kind, *args) #:nodoc:
7
+ options = args.last # Last callback args MUST be a Hash
8
+
9
+ send("_#{kind}").each do |callback, conditions|
10
+ invalid = conditions.find do |key, value|
11
+ value.is_a?(Array) ? !value.include?(options[key]) : (value != options[key])
12
+ end
13
+
14
+ callback.call(*args) unless invalid
15
+ end
16
+ end
17
+
5
18
  # A callback hook set to run every time after a user is set.
6
- # This will happen the first time the user is either authenticated, accessed or manually set
7
- # during a request. You can supply as many hooks as you like, and they will be run in order of decleration
19
+ # This callback is triggered the first time one of those three events happens during a request:
20
+ # :authentication, :fetch (from one of the serializers, like session) and :set_user (when manually set).
21
+ # You can supply as many hooks as you like, and they will be run in order of decleration.
22
+ #
23
+ # If you want to run the callbacks for a given scope and/or event, you can specify them as options.
24
+ # See parameters and example below.
8
25
  #
9
26
  # Parameters:
27
+ # <options> Some options which specify when the callback should be executed
28
+ # scope - Executes the callback only if it maches the scope(s) given
29
+ # only - Executes the callback only if it matches the event(s) given
30
+ # except - Executes the callback except if it matches the event(s) given
10
31
  # <block> A block where you can set arbitrary logic to run every time a user is set
11
32
  # Block Parameters: |user, auth, opts|
12
33
  # user - The user object that is being set
@@ -23,10 +44,21 @@ module Warden
23
44
  # auth.session["#{scope}.last_access"] = Time.now
24
45
  # end
25
46
  #
47
+ # Warden::Manager.after_set_user :except => :fetch do |user,auth,opts|
48
+ # user.login_count += 1
49
+ # end
50
+ #
26
51
  # :api: public
27
- def after_set_user(&block)
52
+ def after_set_user(options={}, &block)
28
53
  raise BlockNotGiven unless block_given?
29
- _after_set_user << block
54
+
55
+ if options.key?(:only)
56
+ options[:event] = options.delete(:only)
57
+ elsif options.key?(:except)
58
+ options[:event] = [:set_user, :authentication, :fetch] - Array(options.delete(:except))
59
+ end
60
+
61
+ _after_set_user << [block, options]
30
62
  end
31
63
 
32
64
  # Provides access to the array of after_set_user blocks to run
@@ -35,32 +67,22 @@ module Warden
35
67
  @_after_set_user ||= []
36
68
  end
37
69
 
38
- # A callback hook set to run after the first authentiation of a session.
39
- # This will only happenwhen the session is first authenticated
40
- #
41
- # Parameters:
42
- # <block> A block to contain logic for the callback
43
- # Block Parameters: |user, auth, opts|
44
- # user - The user object that is being set
45
- # auth - The raw authentication proxy object.
46
- # opts - any options passed into the authenticate call includeing :scope
47
- #
48
- # Example:
49
- #
50
- # Warden::Manager.after_authentication do |user, auth, opts|
51
- # throw(:warden, opts) unless user.active?
52
- # end
70
+ # after_authentication is just a wrapper to after_set_user, which is only invoked
71
+ # when the user is set through the authentication path. The options and yielded arguments
72
+ # are the same as in after_set_user.
53
73
  #
54
74
  # :api: public
55
- def after_authentication(&block)
56
- raise BlockNotGiven unless block_given?
57
- _after_authentication << block
75
+ def after_authentication(options={}, &block)
76
+ after_set_user(options.merge(:event => :authentication), &block)
58
77
  end
59
78
 
60
- # Provides access to the array of after_authentication blocks
61
- # :api: private
62
- def _after_authentication
63
- @_after_authentication ||= []
79
+ # after_fetch is just a wrapper to after_set_user, which is only invoked
80
+ # when the user is fetched from sesion. The options and yielded arguments
81
+ # are the same as in after_set_user.
82
+ #
83
+ # :api: public
84
+ def after_fetch(options={}, &block)
85
+ after_set_user(options.merge(:event => :fetch), &block)
64
86
  end
65
87
 
66
88
  # A callback that runs just prior to the failur application being called.
@@ -69,8 +91,10 @@ module Warden
69
91
  # If a Rails controller were used for the failure_app for example, you would need to set request[:params][:action] = :unauthenticated
70
92
  #
71
93
  # Parameters:
94
+ # <options> Some options which specify when the callback should be executed
95
+ # scope - Executes the callback only if it maches the scope(s) given
72
96
  # <block> A block to contain logic for the callback
73
- # Block Parameters: |user, auth, opts|
97
+ # Block Parameters: |env, opts|
74
98
  # env - The rack env hash
75
99
  # opts - any options passed into the authenticate call includeing :scope
76
100
  #
@@ -82,8 +106,9 @@ module Warden
82
106
  # end
83
107
  #
84
108
  # :api: public
85
- def before_failure(&block)
86
- _before_failure << block
109
+ def before_failure(options={}, &block)
110
+ raise BlockNotGiven unless block_given?
111
+ _before_failure << [block, options]
87
112
  end
88
113
 
89
114
  # Provides access to the callback array for before_failure
@@ -95,20 +120,23 @@ module Warden
95
120
  # A callback that runs just prior to the logout of each scope.
96
121
  #
97
122
  # Parameters:
123
+ # <options> Some options which specify when the callback should be executed
124
+ # scope - Executes the callback only if it maches the scope(s) given
98
125
  # <block> A block to contain logic for the callback
99
126
  # Block Parameters: |user, auth, scope|
100
127
  # user - The authenticated user for the current scope
101
128
  # auth - The warden proxy object
102
- # scope - current logout scope
129
+ # opts - any options passed into the authenticate call including :scope
103
130
  #
104
131
  # Example:
105
- # Warden::Manager.before_logout do |user, auth, scope|
132
+ # Warden::Manager.before_logout do |user, auth, opts|
106
133
  # user.forget_me!
107
134
  # end
108
135
  #
109
136
  # :api: public
110
- def before_logout(&block)
111
- _before_logout << block
137
+ def before_logout(options={}, &block)
138
+ raise BlockNotGiven unless block_given?
139
+ _before_logout << [block, options]
112
140
  end
113
141
 
114
142
  # Provides access to the callback array for before_logout
@@ -1,5 +1,7 @@
1
1
  # encoding: utf-8
2
2
  require 'warden/hooks'
3
+ require 'warden/config'
4
+ require 'warden/manager_deprecation'
3
5
 
4
6
  module Warden
5
7
  # The middleware for Rack Authentication
@@ -8,90 +10,26 @@ module Warden
8
10
  # the rack environment hash
9
11
  class Manager
10
12
  extend Warden::Hooks
13
+ extend Warden::ManagerDeprecation
11
14
 
12
- attr_accessor :config, :failure_app
15
+ attr_accessor :config
13
16
 
14
- # initialize the middleware.
15
- # Provide a :failure_app in the options to setup an application to run when there is a failure
16
- # The manager is yielded when initialized with a block. This is useful when declaring it in Rack::Builder
17
+ # Initialize the middleware. If a block is given, a Warden::Config is yielded so you can properly
18
+ # configure the Warden::Manager.
17
19
  # :api: public
18
- def initialize(app, config = {})
19
- @app = app
20
- @config = config
21
- yield self if block_given?
22
-
23
- # Should ensure there is a failure application defined.
24
- @failure_app = config[:failure_app] if config[:failure_app]
25
-
26
- # Set default configuration values.
27
- @config[:default_strategies] ||= []
28
- @config[:default_serializers] ||= [ :session ]
29
-
20
+ def initialize(app, options={})
21
+ @app, @config = app, Warden::Config.new(options)
22
+ yield @config if block_given?
30
23
  self
31
24
  end
32
-
33
- # Get the default scope for Warden. By default this is :default
34
- # @api public
35
- def self.default_scope
36
- @default_scope
37
- end
38
-
39
- # Set the default scope for Warden.
40
- def self.default_scope=(scope)
41
- @default_scope = scope
42
- end
43
- @default_scope = :default
44
-
45
- # Do not raise an error if a missing strategy is given by default.
46
- # :api: plugin
47
- def silence_missing_strategies!
48
- @config[:silence_missing_strategies] = true
49
- end
50
-
51
- # Do not raise an error if a missing serializer is given by default.
52
- # :api: plugin
53
- def silence_missing_serializers!
54
- @config[:silence_missing_serializers] = true
55
- end
56
-
57
- # Set the default strategies to use.
58
- # :api: public
59
- def default_strategies(*strategies)
60
- if strategies.empty?
61
- @config[:default_strategies]
62
- else
63
- @config[:default_strategies] = strategies.flatten
64
- end
65
- end
66
-
67
- # Set the default serializers to use. By default, only session is enabled.
68
- # :api: public
69
- def default_serializers(*serializers)
70
- if serializers.empty?
71
- @config[:default_serializers]
72
- else
73
- @config[:default_serializers] = serializers.flatten
74
- end
75
- end
76
-
77
- # Quick accessor to strategies from manager
78
- # :api: public
79
- def serializers
80
- Warden::Serializers
81
- end
82
-
83
- # Quick accessor to strategies from manager
84
- # :api: public
85
- def strategies
86
- Warden::Strategies
87
- end
88
25
 
26
+ # Invoke the application guarding for throw :warden.
27
+ # If this is downstream from another warden instance, don't do anything.
89
28
  # :api: private
90
29
  def call(env) # :nodoc:
91
- # if this is downstream from another warden instance, don't do anything.
92
30
  return @app.call(env) unless env['warden'].nil?
93
31
 
94
- env['warden'] = Proxy.new(env, @config)
32
+ env['warden'] = Proxy.new(env, self)
95
33
  result = catch(:warden) do
96
34
  @app.call(env)
97
35
  end
@@ -110,58 +48,13 @@ module Warden
110
48
  end
111
49
  end
112
50
 
113
- class << self
114
-
115
- # Prepares the user to serialize into the session.
116
- # Any object that can be serialized into the session in some way can be used as a "user" object
117
- # Generally however complex object should not be stored in the session.
118
- # If possible store only a "key" of the user object that will allow you to reconstitute it.
119
- #
120
- # Example:
121
- # Warden::Manager.serialize_into_session{ |user| user.id }
122
- #
123
- # Deprecation:
124
- # This method was deprecated in favor of serializer in Session. You can set it while setting the middleware:
125
- #
126
- # use Warden::Manager do |manager|
127
- # manager.serializers.update(:session) do
128
- # def serialize(user)
129
- # user.id
130
- # end
131
- # end
132
- # end
133
- #
134
- # :api: public
135
- def serialize_into_session(&block)
136
- warn "[DEPRECATION] serialize_into_session is deprecated. Please overwrite the serialize method in Warden::Serializers::Session."
137
- Warden::Serializers::Session.send :define_method, :serialize, &block
138
- end
139
-
140
- # Reconstitues the user from the session.
141
- # Use the results of user_session_key to reconstitue the user from the session on requests after the initial login
142
- #
143
- # Example:
144
- # Warden::Manager.serialize_from_session{ |id| User.get(id) }
145
- #
146
- # Deprecation:
147
- # This method was deprecated in favor of serializer in Session. You can set it while setting the middleware:
148
- #
149
- # use Warden::Manager do |manager|
150
- # manager.serializers.update(:session) do
151
- # def deserialize(id)
152
- # User.get(id)
153
- # end
154
- # end
155
- # end
156
- #
157
- # :api: public
158
- def serialize_from_session(&block)
159
- warn "[DEPRECATION] serialize_from_session is deprecated. Please overwrite the deserialize method in Warden::Serializers::Session."
160
- Warden::Serializers::Session.send :define_method, :deserialize, &block
161
- end
51
+ # :api: private
52
+ def _run_callbacks(*args) #:nodoc:
53
+ self.class._run_callbacks(*args)
162
54
  end
163
55
 
164
- private
56
+ private
57
+
165
58
  # When a request is unauthentiated, here's where the processing occurs.
166
59
  # It looks at the result of the proxy to see if it's been executed and what action to take.
167
60
  # :api: private
@@ -184,14 +77,14 @@ module Warden
184
77
  def call_failure_app(env, opts = {})
185
78
  if env['warden'].custom_failure?
186
79
  opts[:original_response]
187
- else
80
+ elsif config.failure_app
188
81
  env["PATH_INFO"] = "/#{opts[:action]}"
189
82
  env["warden.options"] = opts
190
83
 
191
- # Call the before failure callbacks
192
- Warden::Manager._before_failure.each{|hook| hook.call(env,opts)}
193
- raise "No Failure App provided" unless @failure_app
194
- @failure_app.call(env).to_a
84
+ _run_callbacks(:before_failure, env, opts)
85
+ config.failure_app.call(env).to_a
86
+ else
87
+ raise "No Failure App provided"
195
88
  end
196
89
  end # call_failure_app
197
90
  end
@@ -0,0 +1,62 @@
1
+ module Warden
2
+ module ManagerDeprecation
3
+ # Read the default scope from Warden
4
+ def default_scope
5
+ warn "[DEPRECATION] Warden::Manager.default_scope is deprecated. It's now accessible in the Warden::Manager instance."
6
+ end
7
+
8
+ # Set the default scope for Warden.
9
+ def default_scope=(scope)
10
+ warn "[DEPRECATION] Warden::Manager.default_scope= is deprecated. Please set it in the Warden::Manager instance."
11
+ end
12
+
13
+ # Prepares the user to serialize into the session.
14
+ # Any object that can be serialized into the session in some way can be used as a "user" object
15
+ # Generally however complex object should not be stored in the session.
16
+ # If possible store only a "key" of the user object that will allow you to reconstitute it.
17
+ #
18
+ # Example:
19
+ # Warden::Manager.serialize_into_session{ |user| user.id }
20
+ #
21
+ # Deprecation:
22
+ # This method was deprecated in favor of serializer in Session. You can set it while setting the middleware:
23
+ #
24
+ # use Warden::Manager do |manager|
25
+ # manager.serializers.update(:session) do
26
+ # def serialize(user)
27
+ # user.id
28
+ # end
29
+ # end
30
+ # end
31
+ #
32
+ # :api: public
33
+ def serialize_into_session(&block)
34
+ warn "[DEPRECATION] serialize_into_session is deprecated. Please overwrite the serialize method in Warden::Serializers::Session."
35
+ Warden::Serializers::Session.send :define_method, :serialize, &block
36
+ end
37
+
38
+ # Reconstitues the user from the session.
39
+ # Use the results of user_session_key to reconstitue the user from the session on requests after the initial login
40
+ #
41
+ # Example:
42
+ # Warden::Manager.serialize_from_session{ |id| User.get(id) }
43
+ #
44
+ # Deprecation:
45
+ # This method was deprecated in favor of serializer in Session. You can set it while setting the middleware:
46
+ #
47
+ # use Warden::Manager do |manager|
48
+ # manager.serializers.update(:session) do
49
+ # def deserialize(id)
50
+ # User.get(id)
51
+ # end
52
+ # end
53
+ # end
54
+ #
55
+ # :api: public
56
+ def serialize_from_session(&block)
57
+ warn "[DEPRECATION] serialize_from_session is deprecated. Please overwrite the deserialize method in Warden::Serializers::Session."
58
+ Warden::Serializers::Session.send :define_method, :deserialize, &block
59
+ end
60
+
61
+ end
62
+ end
data/lib/warden/proxy.rb CHANGED
@@ -7,13 +7,9 @@ module Warden
7
7
  # :api: private
8
8
  attr_accessor :winning_strategy
9
9
 
10
- # An accessor to the rack env hash
10
+ # An accessor to the rack env hash, the proxy owner and its config
11
11
  # :api: public
12
- attr_reader :env
13
-
14
- # A reader for the config of warden that created this proxy.
15
- # :api: public
16
- attr_reader :config
12
+ attr_reader :env, :manager, :config
17
13
 
18
14
  extend ::Forwardable
19
15
  include ::Warden::Mixins::Common
@@ -21,11 +17,9 @@ module Warden
21
17
  # :api: private
22
18
  def_delegators :winning_strategy, :headers, :_status, :custom_response
23
19
 
24
- def initialize(env, config = {}) #:nodoc:
25
- @env = env
26
- @config = config
27
- @strategies = @config.fetch(:default_strategies, [])
28
- @users = {}
20
+ def initialize(env, manager) #:nodoc:
21
+ @env, @users = env, {}
22
+ @manager, @config = manager, manager.config
29
23
  errors # setup the error object in the session
30
24
  end
31
25
 
@@ -41,7 +35,7 @@ module Warden
41
35
  # env['warden'].authenticated?(:admin)
42
36
  #
43
37
  # :api: public
44
- def authenticated?(scope = Warden::Manager.default_scope)
38
+ def authenticated?(scope = @config.default_scope)
45
39
  result = !!user(scope)
46
40
  yield if block_given? && result
47
41
  result
@@ -49,7 +43,7 @@ module Warden
49
43
 
50
44
  # Same API as authenticated, but returns false when authenticated.
51
45
  # :api: public
52
- def unauthenticated?(scope = Warden::Manager.default_scope)
46
+ def unauthenticated?(scope = @config.default_scope)
53
47
  result = !authenticated?(scope)
54
48
  yield if block_given? && result
55
49
  result
@@ -82,7 +76,7 @@ module Warden
82
76
  # :api: public
83
77
  def authenticate!(*args)
84
78
  scope, opts = _perform_authentication(*args)
85
- throw(:warden, opts.merge(:action => :unauthenticated)) if !user(scope)
79
+ throw(:warden, opts) if !user(scope)
86
80
  user(scope)
87
81
  end
88
82
 
@@ -97,9 +91,9 @@ module Warden
97
91
  # env['warden'].stored?(:default, :cookie) #=> false
98
92
  #
99
93
  # :api: public
100
- def stored?(scope = Warden::Manager.default_scope, serializer = nil)
94
+ def stored?(scope = @config.default_scope, serializer = nil)
101
95
  if serializer
102
- find_serializer(serializer).stored?(scope)
96
+ _find_serializer(serializer).stored?(scope)
103
97
  else
104
98
  serializers.any? { |s| s.stored?(scope) }
105
99
  end
@@ -113,12 +107,13 @@ module Warden
113
107
  #
114
108
  # :api: public
115
109
  def set_user(user, opts = {})
116
- scope = (opts[:scope] ||= Warden::Manager.default_scope)
110
+ return unless user
111
+ scope = (opts[:scope] ||= @config.default_scope)
117
112
  _store_user(user, scope) unless opts[:store] == false
118
113
  @users[scope] = user
119
114
 
120
- # Run the after hooks for setting the user
121
- Warden::Manager._after_set_user.each{ |hook| hook.call(user, self, opts) }
115
+ opts[:event] ||= :set_user
116
+ manager._run_callbacks(:after_set_user, user, self, opts)
122
117
  user
123
118
  end
124
119
 
@@ -133,8 +128,8 @@ module Warden
133
128
  # env['warden'].user(:admin)
134
129
  #
135
130
  # :api: public
136
- def user(scope = Warden::Manager.default_scope)
137
- @users[scope] ||= set_user(_fetch_user(scope), :scope => scope)
131
+ def user(scope = @config.default_scope)
132
+ @users[scope] ||= set_user(_fetch_user(scope), :scope => scope, :event => :fetch)
138
133
  end
139
134
 
140
135
  # Provides a scoped session data for authenticated users.
@@ -148,7 +143,7 @@ module Warden
148
143
  # env['warden'].session(:sudo)[:foo] = "bar"
149
144
  #
150
145
  # :api: public
151
- def session(scope = Warden::Manager.default_scope)
146
+ def session(scope = @config.default_scope)
152
147
  raise NotAuthenticated, "#{scope.inspect} user is not logged in" unless authenticated?(scope)
153
148
  raw_session["warden.user.#{scope}.session"] ||= {}
154
149
  end
@@ -178,7 +173,7 @@ module Warden
178
173
 
179
174
  scopes.each do |scope|
180
175
  user = @users.delete(scope)
181
- Warden::Manager._before_logout.each { |hook| hook.call(user, self, scope) }
176
+ manager._run_callbacks(:before_logout, user, self, :scope => scope)
182
177
 
183
178
  raw_session.delete("warden.user.#{scope}.session")
184
179
  _delete_user(user, scope)
@@ -216,15 +211,13 @@ module Warden
216
211
  # :api: private
217
212
  def serializers # :nodoc:
218
213
  @serializers ||= begin
219
- array = []
220
- @config[:default_serializers].each do |s|
221
- unless Warden::Serializers[s]
222
- raise "Invalid serializer #{s}" unless silence_missing_serializers?
214
+ @config.default_serializers.inject([]) do |array, s|
215
+ unless klass = Warden::Serializers[s]
216
+ raise "Invalid serializer #{s}" unless @config.silence_missing_serializers?
223
217
  next
224
218
  end
225
- array << Warden::Serializers[s].new(@env)
219
+ array << klass.new(@env)
226
220
  end
227
- array
228
221
  end
229
222
  end
230
223
 
@@ -233,34 +226,16 @@ module Warden
233
226
  # :api: private
234
227
  def _perform_authentication(*args)
235
228
  scope = scope_from_args(args)
236
- opts = opts_from_args(args)
237
-
238
- # Look for an existing user in the session for this scope
239
- # If there was no user in the session. See if we can get one from the request
240
- return scope, opts if the_user = user(scope)
229
+ opts = opts_from_args(args)
241
230
 
242
- strategies = args.empty? ? @strategies : args
243
- raise "No Strategies Found" if strategies.empty?
231
+ # Look for an existing user in the session for this scope.
232
+ # If there was no user in the session. See if we can get one from the request
233
+ return scope, opts if user(scope)
244
234
 
245
- strategies.each do |s|
246
- unless Warden::Strategies[s]
247
- raise "Invalid strategy #{s}" unless args.empty? && silence_missing_strategies?
248
- next
249
- end
250
-
251
- strategy = Warden::Strategies[s].new(@env, scope)
252
- self.winning_strategy = strategy
253
- next unless strategy.valid?
254
-
255
- strategy._run!
256
- break if strategy.halted?
257
- end
235
+ _run_strategies_for(scope, args)
258
236
 
259
237
  if winning_strategy && winning_strategy.user
260
- set_user(winning_strategy.user, opts)
261
-
262
- # Run the after_authentication hooks
263
- Warden::Manager._after_authentication.each{|hook| hook.call(winning_strategy.user, self, opts)}
238
+ set_user(winning_strategy.user, opts.merge!(:event => :authentication))
264
239
  end
265
240
 
266
241
  [scope, opts]
@@ -268,7 +243,7 @@ module Warden
268
243
 
269
244
  # :api: private
270
245
  def scope_from_args(args) # :nodoc:
271
- Hash === args.last ? args.last.fetch(:scope, Warden::Manager.default_scope) : Warden::Manager.default_scope
246
+ Hash === args.last ? args.last.fetch(:scope, @config.default_scope) : @config.default_scope
272
247
  end
273
248
 
274
249
  # :api: private
@@ -277,25 +252,35 @@ module Warden
277
252
  end
278
253
 
279
254
  # :api: private
280
- def silence_missing_strategies? # :nodoc:
281
- @config[:silence_missing_strategies]
282
- end
255
+ def _run_strategies_for(scope, args) #:nodoc:
256
+ strategies = args.empty? ? @config.default_strategies : args
257
+ raise "No Strategies Found" if strategies.empty?
283
258
 
284
- # :api: private
285
- def silence_missing_serializers? # :nodoc:
286
- @config[:silence_missing_serializers]
259
+ strategies.each do |s|
260
+ unless klass = Warden::Strategies[s]
261
+ raise "Invalid strategy #{s}" unless args.empty? && @config.silence_missing_strategies?
262
+ next
263
+ end
264
+
265
+ strategy = klass.new(@env, scope)
266
+ self.winning_strategy = strategy
267
+ next unless strategy.valid?
268
+
269
+ strategy._run!
270
+ break if strategy.halted?
271
+ end
287
272
  end
288
273
 
289
274
  # Does the work of storing the user in stores.
290
275
  # :api: private
291
- def _store_user(user, scope = Warden::Manager.default_scope) # :nodoc:
276
+ def _store_user(user, scope) # :nodoc:
292
277
  return unless user
293
278
  serializers.each { |s| s.store(user, scope) }
294
279
  end
295
280
 
296
281
  # Does the work of fetching the user from the first store.
297
282
  # :api: private
298
- def _fetch_user(scope = Warden::Manager.default_scope) # :nodoc:
283
+ def _fetch_user(scope) # :nodoc:
299
284
  serializers.each do |s|
300
285
  user = s.fetch(scope)
301
286
  return user if user
@@ -305,12 +290,12 @@ module Warden
305
290
 
306
291
  # Does the work of deleteing the user in all stores.
307
292
  # :api: private
308
- def _delete_user(user, scope = Warden::Manager.default_scope) # :nodoc:
293
+ def _delete_user(user, scope) # :nodoc:
309
294
  serializers.each { |s| s.delete(scope, user) }
310
295
  end
311
296
 
312
297
  # :api: private
313
- def find_serializer(name) # :nodoc:
298
+ def _find_serializer(name) # :nodoc:
314
299
  serializers.find { |s| s.class == ::Warden::Serializers[name] }
315
300
  end
316
301
  end # Proxy
@@ -1,3 +1,3 @@
1
1
  module Warden
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.0"
3
3
  end
@@ -12,16 +12,14 @@ module Warden::Spec
12
12
 
13
13
  def setup_rack(app = nil, opts = {}, &block)
14
14
  app ||= block if block_given?
15
+
16
+ opts[:failure_app] ||= failure_app
15
17
  opts[:default_strategies] ||= [:password]
16
18
  opts[:default_serializers] ||= [:session]
17
- opts[:failure_app] ||= Warden::Spec::Helpers::FAILURE_APP
19
+
18
20
  Rack::Builder.new do
19
21
  use Warden::Spec::Helpers::Session
20
- use Warden::Manager, opts do |manager|
21
- manager.failure_app = Warden::Spec::Helpers::FAILURE_APP
22
- manager.default_strategies *opts[:default_strategies]
23
- manager.default_serializers *opts[:default_serializers]
24
- end
22
+ use Warden::Manager, opts
25
23
  run app
26
24
  end
27
25
  end
@@ -0,0 +1,55 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Warden::Config do
4
+
5
+ before(:each) do
6
+ @config = Warden::Config.new
7
+ end
8
+
9
+ it "should behave like a hash" do
10
+ @config[:foo] = :bar
11
+ @config[:foo].should == :bar
12
+ end
13
+
14
+ it "should provide hash accessors" do
15
+ @config.failure_app = :foo
16
+ @config[:failure_app].should == :foo
17
+ @config[:failure_app] = :bar
18
+ @config.failure_app.should == :bar
19
+ end
20
+
21
+ it "should allow to read and set default strategies" do
22
+ @config.default_strategies :foo, :bar
23
+ @config.default_strategies.should == [:foo, :bar]
24
+ end
25
+
26
+ it "should allow to read and set default serializers" do
27
+ @config.default_serializers :foo, :bar
28
+ @config.default_serializers.should == [:foo, :bar]
29
+ end
30
+
31
+ it "should ship with default serializers and strategies" do
32
+ @config.default_strategies.should == []
33
+ @config.default_serializers.should == [:session]
34
+ end
35
+
36
+ it "should allow to silence missing strategies" do
37
+ @config.silence_missing_strategies!
38
+ @config.silence_missing_strategies?.should be_true
39
+ end
40
+
41
+ it "should allow to silence missing serializers" do
42
+ @config.silence_missing_serializers!
43
+ @config.silence_missing_serializers?.should be_true
44
+ end
45
+
46
+ it "should set the default_scope" do
47
+ @config.default_scope.should == :default
48
+ @config.default_scope = :foo
49
+ @config.default_scope.should == :foo
50
+ end
51
+
52
+ it "should merge given options on initialization" do
53
+ Warden::Config.new(:foo => :bar)[:foo].should == :bar
54
+ end
55
+ end
@@ -43,38 +43,82 @@ describe "standard authentication hooks" do
43
43
  env['warden.spec.hook.foo'].should == "run foo"
44
44
  env['warden.spec.hook.bar'].should == "run bar"
45
45
  end
46
- end
47
46
 
48
- describe "after_authentication" do
49
- before(:each) do
50
- RAM = Warden::Manager unless defined?(RAM)
51
- RAM._after_authentication.clear
47
+ it "should not run the event specified with except" do
48
+ RAM.after_set_user(:except => :set_user){|u,a,o| fail}
49
+ app = lambda do |e|
50
+ e['warden'].set_user("foo")
51
+ valid_response
52
+ end
53
+ env = env_with_params
54
+ setup_rack(app).call(env)
52
55
  end
53
56
 
54
- after(:each) do
55
- RAM._after_authentication.clear
57
+ it "should only run the event specified with only" do
58
+ RAM.after_set_user(:only => :set_user){|u,a,o| fail}
59
+ app = lambda do |e|
60
+ e['warden'].authenticate(:pass)
61
+ valid_response
62
+ end
63
+ env = env_with_params
64
+ setup_rack(app).call(env)
56
65
  end
57
66
 
58
- it "should allow me to add an after_authentication hook" do
59
- RAM.after_authentication{|user, auth, opts| "foo"}
60
- RAM._after_authentication.should have(1).item
61
- end
67
+ context "after_authentication" do
68
+ it "should be a wrapper to after_set_user behavior" do
69
+ RAM.after_authentication{|u,a,o| a.env['warden.spec.hook.baz'] = "run baz"}
70
+ RAM.after_authentication{|u,a,o| a.env['warden.spec.hook.paz'] = "run paz"}
71
+ RAM.after_authentication{|u,a,o| o[:event].should == :authentication }
72
+ app = lambda{|e| e['warden'].authenticate(:pass); valid_response}
73
+ env = env_with_params
74
+ setup_rack(app).call(env)
75
+ env['warden.spec.hook.baz'].should == 'run baz'
76
+ env['warden.spec.hook.paz'].should == 'run paz'
77
+ end
62
78
 
63
- it "should allow me to add multiple after_authentication hooks" do
64
- RAM.after_authentication{|u,a,o| "bar"}
65
- RAM.after_authentication{|u,a,o| "baz"}
66
- RAM._after_authentication.should have(2).items
79
+ it "should not be invoked on default after_set_user scenario" do
80
+ RAM.after_authentication{|u,a,o| fail}
81
+ app = lambda do |e|
82
+ e['warden'].set_user("foo")
83
+ valid_response
84
+ end
85
+ env = env_with_params
86
+ setup_rack(app).call(env)
87
+ end
67
88
  end
68
89
 
69
- it "should run each after_authentication hook after authentication is run" do
70
- RAM.after_authentication{|u,a,o| a.env['warden.spec.hook.baz'] = "run baz"}
71
- RAM.after_authentication{|u,a,o| a.env['warden.spec.hook.paz'] = "run paz"}
72
- app = lambda{|e| e['warden'].authenticate(:pass); valid_response}
73
- env = env_with_params
74
- setup_rack(app).call(env)
75
- env['warden.spec.hook.baz'].should == 'run baz'
76
- env['warden.spec.hook.paz'].should == 'run paz'
90
+ context "after_fetch" do
91
+ it "should be a wrapper to after_set_user behavior" do
92
+ RAM.after_fetch{|u,a,o| a.env['warden.spec.hook.baz'] = "run baz"}
93
+ RAM.after_fetch{|u,a,o| a.env['warden.spec.hook.paz'] = "run paz"}
94
+ RAM.after_fetch{|u,a,o| o[:event].should == :fetch }
95
+ env = env_with_params
96
+ setup_rack(lambda { valid_response }).call(env)
97
+ env['rack.session']['warden.user.default.key'] = "Foo"
98
+ env['warden'].user.should == "Foo"
99
+ env['warden.spec.hook.baz'].should == 'run baz'
100
+ env['warden.spec.hook.paz'].should == 'run paz'
101
+ end
102
+
103
+ it "should not be invoked on default after_set_user scenario" do
104
+ RAM.after_fetch{|u,a,o| fail}
105
+ app = lambda do |e|
106
+ e['warden'].set_user("foo")
107
+ valid_response
108
+ end
109
+ env = env_with_params
110
+ setup_rack(app).call(env)
111
+ end
112
+
113
+ it "should not be invoked if fetched user is nil" do
114
+ RAM.after_fetch{|u,a,o| fail}
115
+ env = env_with_params
116
+ setup_rack(lambda { valid_response }).call(env)
117
+ env['rack.session']['warden.user.default.key'] = nil
118
+ env['warden'].user.should be_nil
119
+ end
77
120
  end
121
+
78
122
  end
79
123
 
80
124
  describe "before_failure" do
@@ -125,14 +169,14 @@ describe "standard authentication hooks" do
125
169
  end
126
170
 
127
171
  it "should allow me to add multiple after_authetnication hooks" do
128
- RAM.before_logout{|u,a,s| "bar"}
129
- RAM.before_logout{|u,a,s| "baz"}
172
+ RAM.before_logout{|u,a,o| "bar"}
173
+ RAM.before_logout{|u,a,o| "baz"}
130
174
  RAM._before_logout.should have(2).items
131
175
  end
132
176
 
133
177
  it "should run each before_logout hook before logout is run" do
134
- RAM.before_logout{|u,a,s| a.env['warden.spec.hook.lorem'] = "run lorem"}
135
- RAM.before_logout{|u,a,s| a.env['warden.spec.hook.ipsum'] = "run ipsum"}
178
+ RAM.before_logout{|u,a,o| a.env['warden.spec.hook.lorem'] = "run lorem"}
179
+ RAM.before_logout{|u,a,o| a.env['warden.spec.hook.ipsum'] = "run ipsum"}
136
180
  app = lambda{|e| e['warden'].authenticate(:pass); valid_response}
137
181
  env = env_with_params
138
182
  setup_rack(app).call(env)
@@ -141,21 +185,27 @@ describe "standard authentication hooks" do
141
185
  env['warden.spec.hook.ipsum'].should == 'run ipsum'
142
186
  end
143
187
 
144
- it "should run before_logout hook on different scopes" do
145
- RAM.before_logout{|u,a,s| a.env["warden.spec.hook.scope1"] = "run scope1" if s == :scope1}
146
- RAM.before_logout{|u,a,s| a.env["warden.spec.hook.scope2"] = "run scope2" if s == :scope2}
188
+ it "should run before_logout hook for a socufued scope" do
189
+ RAM.before_logout(:scope => :scope1){|u,a,o| a.env["warden.spec.hook.a"] << :scope1 }
190
+ RAM.before_logout(:scope => [:scope2]){|u,a,o| a.env["warden.spec.hook.b"] << :scope2 }
191
+
147
192
  app = lambda do |e|
148
193
  e['warden'].authenticate(:pass, :scope => :scope1)
149
194
  e['warden'].authenticate(:pass, :scope => :scope2)
150
195
  valid_response
151
196
  end
152
197
  env = env_with_params
198
+ env["warden.spec.hook.a"] ||= []
199
+ env["warden.spec.hook.b"] ||= []
153
200
  setup_rack(app).call(env)
201
+
154
202
  env['warden'].logout(:scope1)
155
- env['warden.spec.hook.scope1'].should == 'run scope1'
156
- env['warden.spec.hook.scope2'].should == nil
203
+ env['warden.spec.hook.a'].should == [:scope1]
204
+ env['warden.spec.hook.b'].should == []
205
+
157
206
  env['warden'].logout(:scope2)
158
- env['warden.spec.hook.scope2'].should == 'run scope2'
207
+ env['warden.spec.hook.a'].should == [:scope1]
208
+ env['warden.spec.hook.b'].should == [:scope2]
159
209
  end
160
210
  end
161
211
 
@@ -209,25 +209,28 @@ describe Warden::Manager do
209
209
  end # integrated strategies
210
210
 
211
211
  it "should allow me to set a different default scope for warden" do
212
- Warden::Manager.default_scope.should == :default
213
- Warden::Manager.default_scope = :other_scope
214
- Warden::Manager.default_scope.should == :other_scope
215
- Warden::Manager.default_scope = :default
212
+ Rack::Builder.new do
213
+ use Warden::Manager, :default_scope => :default do |manager|
214
+ manager.default_scope.should == :default
215
+ manager.default_scope = :other
216
+ manager.default_scope.should == :other
217
+ end
218
+ end
216
219
  end
217
220
 
218
221
  it "should allow me to access serializers through manager" do
219
222
  Rack::Builder.new do
220
- use Warden::Manager do |manager|
221
- manager.serializers.should == Warden::Serializers
222
- end
223
- end
223
+ use Warden::Manager do |manager|
224
+ manager.serializers.should == Warden::Serializers
225
+ end
226
+ end
224
227
  end
225
228
 
226
229
  it "should allow me to access serializers through manager" do
227
230
  Rack::Builder.new do
228
- use Warden::Manager do |manager|
229
- manager.strategies.should == Warden::Strategies
230
- end
231
- end
231
+ use Warden::Manager do |manager|
232
+ manager.strategies.should == Warden::Strategies
233
+ end
234
+ end
232
235
  end
233
236
  end
@@ -98,7 +98,7 @@ describe Warden::Proxy do
98
98
  valid_response
99
99
  end
100
100
  lambda {
101
- setup_rack(app, :silence_missing_strategies => true, :default_strategies => :unknown).call(env)
101
+ setup_rack(app, :silence_missing_strategies => true, :default_strategies => [:unknown]).call(env)
102
102
  }.should_not raise_error
103
103
  end
104
104
 
data/warden.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{warden}
8
- s.version = "0.7.0"
8
+ s.version = "0.8.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-01-04}
12
+ s.date = %q{2010-01-07}
13
13
  s.email = %q{has.sox@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE",
@@ -23,10 +23,12 @@ Gem::Specification.new do |s|
23
23
  "Rakefile",
24
24
  "TODO.textile",
25
25
  "lib/warden.rb",
26
+ "lib/warden/config.rb",
26
27
  "lib/warden/declarable.rb",
27
28
  "lib/warden/errors.rb",
28
29
  "lib/warden/hooks.rb",
29
30
  "lib/warden/manager.rb",
31
+ "lib/warden/manager_deprecation.rb",
30
32
  "lib/warden/mixins/common.rb",
31
33
  "lib/warden/proxy.rb",
32
34
  "lib/warden/serializers.rb",
@@ -42,10 +44,10 @@ Gem::Specification.new do |s|
42
44
  "spec/helpers/strategies/failz.rb",
43
45
  "spec/helpers/strategies/invalid.rb",
44
46
  "spec/helpers/strategies/pass.rb",
45
- "spec/helpers/strategies/pass_without_user.rb",
46
47
  "spec/helpers/strategies/password.rb",
47
48
  "spec/spec_helper.rb",
48
49
  "spec/warden/authenticated_data_store_spec.rb",
50
+ "spec/warden/config_spec.rb",
49
51
  "spec/warden/errors_spec.rb",
50
52
  "spec/warden/hooks_spec.rb",
51
53
  "spec/warden/manager_spec.rb",
@@ -69,10 +71,10 @@ Gem::Specification.new do |s|
69
71
  "spec/helpers/strategies/failz.rb",
70
72
  "spec/helpers/strategies/invalid.rb",
71
73
  "spec/helpers/strategies/pass.rb",
72
- "spec/helpers/strategies/pass_without_user.rb",
73
74
  "spec/helpers/strategies/password.rb",
74
75
  "spec/spec_helper.rb",
75
76
  "spec/warden/authenticated_data_store_spec.rb",
77
+ "spec/warden/config_spec.rb",
76
78
  "spec/warden/errors_spec.rb",
77
79
  "spec/warden/hooks_spec.rb",
78
80
  "spec/warden/manager_spec.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: warden
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Neighman
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-04 00:00:00 +11:00
12
+ date: 2010-01-07 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -39,10 +39,12 @@ files:
39
39
  - Rakefile
40
40
  - TODO.textile
41
41
  - lib/warden.rb
42
+ - lib/warden/config.rb
42
43
  - lib/warden/declarable.rb
43
44
  - lib/warden/errors.rb
44
45
  - lib/warden/hooks.rb
45
46
  - lib/warden/manager.rb
47
+ - lib/warden/manager_deprecation.rb
46
48
  - lib/warden/mixins/common.rb
47
49
  - lib/warden/proxy.rb
48
50
  - lib/warden/serializers.rb
@@ -58,10 +60,10 @@ files:
58
60
  - spec/helpers/strategies/failz.rb
59
61
  - spec/helpers/strategies/invalid.rb
60
62
  - spec/helpers/strategies/pass.rb
61
- - spec/helpers/strategies/pass_without_user.rb
62
63
  - spec/helpers/strategies/password.rb
63
64
  - spec/spec_helper.rb
64
65
  - spec/warden/authenticated_data_store_spec.rb
66
+ - spec/warden/config_spec.rb
65
67
  - spec/warden/errors_spec.rb
66
68
  - spec/warden/hooks_spec.rb
67
69
  - spec/warden/manager_spec.rb
@@ -106,10 +108,10 @@ test_files:
106
108
  - spec/helpers/strategies/failz.rb
107
109
  - spec/helpers/strategies/invalid.rb
108
110
  - spec/helpers/strategies/pass.rb
109
- - spec/helpers/strategies/pass_without_user.rb
110
111
  - spec/helpers/strategies/password.rb
111
112
  - spec/spec_helper.rb
112
113
  - spec/warden/authenticated_data_store_spec.rb
114
+ - spec/warden/config_spec.rb
113
115
  - spec/warden/errors_spec.rb
114
116
  - spec/warden/hooks_spec.rb
115
117
  - spec/warden/manager_spec.rb
@@ -1,7 +0,0 @@
1
- Warden::Strategies.add(:pass_without_user) do
2
- def authenticate!
3
- request.env['warden.spec.strategies'] ||= []
4
- request.env['warden.spec.strategies'] << :pass_without_user
5
- success!(nil)
6
- end
7
- end