Class: Warden::Manager

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

Overview

The middleware for Rack Authentication The middlware 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_set_user, _before_failure, _before_logout, _on_request, after_authentication, after_fetch, after_set_user, before_failure, before_logout, on_request

Methods included from ManagerDeprecation

default_scope, default_scope=, serializers

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:



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

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.



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

def config
  @config
end

Class Method Details

.serialize_from_session(&block) ⇒ Object

Reconstitues the user from the session. Use the results of user_session_key to reconstitue the user from the session on requests after the initial login

Example:

Warden::Manager.serialize_from_session{ |id| User.get(id) }

:api: public



79
80
81
# File 'lib/warden/manager.rb', line 79

def serialize_from_session(&block)
  Warden::SessionSerializer.send :define_method, :deserialize, &block
end

.serialize_into_session(&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.

Example:

Warden::Manager.serialize_into_session{ |user| user.id }

:api: public



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

def serialize_into_session(&block)
  Warden::SessionSerializer.send :define_method, :serialize, &block
end

Instance Method Details

#_run_callbacks(*args) ⇒ Object

:api: private



54
55
56
# File 'lib/warden/manager.rb', line 54

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



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

def call(env) # :nodoc:
  return @app.call(env) unless env['warden'].nil? || 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 && !env['warden'].custom_failure?
      process_unauthenticated(env)
    else
      result
    end
  when Hash
    process_unauthenticated(env, result || {})
  end
end