Class: Warden::Manager

Inherits:
Object
  • Object
show all
Extended by:
Hooks
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_authentication, _after_set_user, _before_failure, _before_logout, after_authentication, after_set_user, before_failure, before_logout

Constructor Details

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

initialize the middleware. Provide a :failure_app in the options to setup an application to run when there is a failure The manager is yielded when initialized with a block. This is useful when declaring it in Rack::Builder :api: public

Yields:

  • (_self)

Yield Parameters:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/warden/manager.rb', line 18

def initialize(app, config = {})
  @app = app
  @config = config
  yield self if block_given?

  # Should ensure there is a failure application defined.
  @failure_app = config[:failure_app] if config[:failure_app]

  # Set default configuration values.
  @config[:default_strategies]  ||= []
  @config[:default_serializers] ||= [ :session ]

  self
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#failure_appObject

Returns the value of attribute failure_app.



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

def failure_app
  @failure_app
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) }

Deprecation:

This method was deprecated in favor of serializer in Session. You can set it while setting the middleware:

use Warden::Manager do |manager|
  manager.update(:session) do
    def deserialize(user)
      User.get(id)
    end
  end
end

:api: public



134
135
136
137
# File 'lib/warden/manager.rb', line 134

def serialize_from_session(&block)
  warn "[DEPRECATION] serialize_from_session is deprecated. Please overwrite the deserialize method in Warden::Serializers::Session."
  Warden::Serializers::Session.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 }

Deprecation:

This method was deprecated in favor of serializer in Session. You can set it while setting the middleware:

use Warden::Manager do |manager|
  manager.update(:session) do
    def serialize(user)
      user.id
    end
  end
end

:api: public



111
112
113
114
# File 'lib/warden/manager.rb', line 111

def serialize_into_session(&block)
  warn "[DEPRECATION] serialize_into_session is deprecated. Please overwrite the serialize method in Warden::Serializers::Session."
  Warden::Serializers::Session.send :define_method, :serialize, &block
end

Instance Method Details

#call(env) ⇒ Object

:api: private



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/warden/manager.rb', line 66

def call(env) # :nodoc:
  # if this is downstream from another warden instance, don't do anything.
  return @app.call(env) unless env['warden'].nil?

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

  result ||= {}
  case result
  when Array
    if result.first != 401
      return result
    else
      process_unauthenticated({:original_response => result, :action => :unauthenticated}, env)
    end
  when Hash
    result[:action] ||= :unauthenticated
    process_unauthenticated(result, env)
  end
end

#default_serializers(*serializers) ⇒ Object

Set the default serializers to use. By default, only session is enabled. :api: public



57
58
59
60
61
62
63
# File 'lib/warden/manager.rb', line 57

def default_serializers(*serializers)
  if serializers.empty?
    @config[:default_serializers]
  else
    @config[:default_serializers] = serializers.flatten
  end
end

#default_strategies(*strategies) ⇒ Object

Set the default strategies to use. :api: public



47
48
49
50
51
52
53
# File 'lib/warden/manager.rb', line 47

def default_strategies(*strategies)
  if strategies.empty?
    @config[:default_strategies]
  else
    @config[:default_strategies] = strategies.flatten
  end
end

#silence_missing_serializers!Object

Do not raise an error if a missing serializer is given by default. :api: plugin



41
42
43
# File 'lib/warden/manager.rb', line 41

def silence_missing_serializers!
  @config[:silence_missing_serializers] = true
end

#silence_missing_strategies!Object

Do not raise an error if a missing strategy is given by default. :api: plugin



35
36
37
# File 'lib/warden/manager.rb', line 35

def silence_missing_strategies!
  @config[:silence_missing_strategies] = true
end