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:



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

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?
  self
end

Instance Attribute Details

#configObject

Returns the value of attribute config



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

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



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

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



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

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



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

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
49
# 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
    @app.call(env)
  end

  result ||= {}
  case result
  when Array
    if result.first == 401 && intercept_401?(env)
      process_unauthenticated(env)
    else
      result
    end
  when Hash
    process_unauthenticated(env, result)
  end
end