Class: Warden::Manager

Inherits:
Object
  • Object
show all
Extended by:
Hooks
Defined in:
lib/warden/manager.rb

Overview

The middleware for Rack Authentication The middleware requires that there is a session upstream The middleware injects an authentication object into the rack environment hash

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hooks

_after_failed_fetch, _after_set_user, _before_failure, _before_logout, _on_request, after_authentication, after_failed_fetch, after_fetch, after_set_user, before_failure, before_logout, on_request

Constructor Details

#initialize(app, options = {}) {|@config| ... } ⇒ Manager

Initialize the middleware. If a block is given, a Warden::Config is yielded so you can properly configure the Warden::Manager. :api: public

Yields:



19
20
21
22
23
24
25
# File 'lib/warden/manager.rb', line 19

def initialize(app, options={})
  default_strategies = options.delete(:default_strategies)

  @app, @config = app, Warden::Config.new(options)
  @config.default_strategies(*default_strategies) if default_strategies
  yield @config if block_given?
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



14
15
16
# File 'lib/warden/manager.rb', line 14

def config
  @config
end

Class Method Details

.serialize_from_session(scope = nil, &block) ⇒ Object

Reconstitutes the user from the session. Use the results of user_session_key to reconstitute the user from the session on requests after the initial login You can supply different methods of de-serialization for different scopes by passing a scope symbol

Example:

Warden::Manager.serialize_from_session{ |id| User.get(id) }
# With Scope:
Warden::Manager.serialize_from_session(:admin) { |id| AdminUser.get(id) }

:api: public



84
85
86
87
88
89
90
91
92
# File 'lib/warden/manager.rb', line 84

def serialize_from_session(scope = nil, &block)
  method_name = scope.nil? ? :deserialize : "#{scope}_deserialize"

  if Warden::SessionSerializer.method_defined? method_name
    Warden::SessionSerializer.send :remove_method, method_name
  end

  Warden::SessionSerializer.send :define_method, method_name, &block
end

.serialize_into_session(scope = nil, &block) ⇒ Object

Prepares the user to serialize into the session. Any object that can be serialized into the session in some way can be used as a “user” object Generally however complex object should not be stored in the session. If possible store only a “key” of the user object that will allow you to reconstitute it.

You can supply different methods of serialization for different scopes by passing a scope symbol

Example:

Warden::Manager.serialize_into_session{ |user| user.id }
# With Scope:
Warden::Manager.serialize_into_session(:admin) { |user| user.id }

:api: public



69
70
71
72
# File 'lib/warden/manager.rb', line 69

def serialize_into_session(scope = nil, &block)
  method_name = scope.nil? ? :serialize : "#{scope}_serialize"
  Warden::SessionSerializer.send :define_method, method_name, &block
end

Instance Method Details

#_run_callbacks(*args) ⇒ Object

:api: private



51
52
53
# File 'lib/warden/manager.rb', line 51

def _run_callbacks(*args) #:nodoc:
  self.class._run_callbacks(*args)
end

#call(env) ⇒ Object

Invoke the application guarding for throw :warden. If this is downstream from another warden instance, don’t do anything. :api: private



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/warden/manager.rb', line 30

def call(env) # :nodoc:
  return @app.call(env) if env['warden'] && env['warden'].manager != self

  env['warden'] = Proxy.new(env, self)
  result = catch(:warden) do
    env['warden'].on_request
    @app.call(env)
  end

  result ||= {}
  case result
  when Array
    handle_chain_result(result.first, result, env)
  when Hash
    process_unauthenticated(env, result)
  when Rack::Response
    handle_chain_result(result.status, result, env)
  end
end