module Warden::Hooks

def _after_failed_fetch

:api: private
Provides access to the callback array for after_failed_fetch
def _after_failed_fetch
  @_after_failed_fetch ||= []
end

def _after_set_user # :nodoc:

:nodoc:
:api: private
Provides access to the array of after_set_user blocks to run
def _after_set_user # :nodoc:
  @_after_set_user ||= []
end

def _before_failure

:api: private
Provides access to the callback array for before_failure
def _before_failure
  @_before_failure ||= []
end

def _before_logout

:api: private
Provides access to the callback array for before_logout
def _before_logout
  @_before_logout ||= []
end

def _on_request

:api: private
Provides access to the callback array for before_logout
def _on_request
  @_on_request ||= []
end

def _run_callbacks(kind, *args) #:nodoc:

:nodoc:
Hook to _run_callbacks asserting for conditions.
def _run_callbacks(kind, *args) #:nodoc:
  options = args.last # Last callback arg MUST be a Hash
  send("_#{kind}").each do |callback, conditions|
    invalid = conditions.find do |key, value|
      value.is_a?(Array) ? !value.include?(options[key]) : (value != options[key])
    end
    callback.call(*args) unless invalid
  end
end

def after_authentication(options = {}, method = :push, &block)

:api: public

are the same as in after_set_user.
when the user is set through the authentication path. The options and yielded arguments
after_authentication is just a wrapper to after_set_user, which is only invoked
def after_authentication(options = {}, method = :push, &block)
  after_set_user(options.merge(:event => :authentication), method, &block)
end

def after_failed_fetch(options = {}, method = :push, &block)

:api: public

end
I18n.locale = :en
Warden::Manager.after_failed_fetch do |user, auth, opts|
Example:

opts - any options passed into the authenticate call including :scope
auth - The warden proxy object
user - The authenticated user for the current scope
Block Parameters: |user, auth, scope|
A block to contain logic for the callback
scope - Executes the callback only if it matches the scope(s) given
Some options which specify when the callback should be executed
Parameters:

A callback that runs if no user could be fetched, meaning there is now no user logged in.
def after_failed_fetch(options = {}, method = :push, &block)
  raise BlockNotGiven unless block_given?
  _after_failed_fetch.send(method, [block, options])
end

def after_fetch(options = {}, method = :push, &block)

:api: public

are the same as in after_set_user.
when the user is fetched from session. The options and yielded arguments
after_fetch is just a wrapper to after_set_user, which is only invoked
def after_fetch(options = {}, method = :push, &block)
  after_set_user(options.merge(:event => :fetch), method, &block)
end

def after_set_user(options = {}, method = :push, &block)

:api: public

end
user.login_count += 1
Warden::Manager.after_set_user :except => :fetch do |user,auth,opts|

end
auth.session["#{scope}.last_access"] = Time.now
end
throw(:warden, :scope => scope, :reason => "Times Up")
auth.logout(scope)
if auth.session["#{scope}.last_access"].to_i > (Time.now - 5.minutes)
scope = opts[:scope]
Warden::Manager.after_set_user do |user,auth,opts|
Example:

opts - any options passed into the set_user call including :scope
auth - The raw authentication proxy object.
user - The user object that is being set
Block Parameters: |user, auth, opts|
A block where you can set arbitrary logic to run every time a user is set
except - Executes the callback except if it matches the event(s) given
only - Executes the callback only if it matches the event(s) given
scope - Executes the callback only if it matches the scope(s) given
Some options which specify when the callback should be executed
Parameters:

See parameters and example below.
If you want to run the callbacks for a given scope and/or event, you can specify them as options.

You can supply as many hooks as you like, and they will be run in order of declaration.
during a request: :authentication, :fetch (from session) and :set_user (when manually set).
This callback is triggered the first time one of those three events happens
A callback hook set to run every time after a user is set.
def after_set_user(options = {}, method = :push, &block)
  raise BlockNotGiven unless block_given?
  if options.key?(:only)
    options[:event] = options.delete(:only)
  elsif options.key?(:except)
    options[:event] = [:set_user, :authentication, :fetch] - Array(options.delete(:except))
  end
  _after_set_user.send(method, [block, options])
end

def before_failure(options = {}, method = :push, &block)

:api: public

end
params[:warden_failure] = opts
params[:action] = :unauthenticated
params = Rack::Request.new(env).params
Warden::Manager.before_failure do |env, opts|
Example:

opts - any options passed into the authenticate call including :scope
env - The rack env hash
Block Parameters: |env, opts|
A block to contain logic for the callback
scope - Executes the callback only if it matches the scope(s) given
Some options which specify when the callback should be executed
Parameters:

If a Rails controller were used for the failure_app for example, you would need to set request[:params][:action] = :unauthenticated
In this callback you can mutate the environment as required by the failure application
This callback occurs after PATH_INFO has been modified for the failure (default /unauthenticated)
A callback that runs just prior to the failure application being called.
def before_failure(options = {}, method = :push, &block)
  raise BlockNotGiven unless block_given?
  _before_failure.send(method, [block, options])
end

def before_logout(options = {}, method = :push, &block)

:api: public

end
user.forget_me!
Warden::Manager.before_logout do |user, auth, opts|
Example:

opts - any options passed into the authenticate call including :scope
auth - The warden proxy object
user - The authenticated user for the current scope
Block Parameters: |user, auth, scope|
A block to contain logic for the callback
scope - Executes the callback only if it matches the scope(s) given
Some options which specify when the callback should be executed
Parameters:

A callback that runs just prior to the logout of each scope.
def before_logout(options = {}, method = :push, &block)
  raise BlockNotGiven unless block_given?
  _before_logout.send(method, [block, options])
end

def on_request(options = {}, method = :push, &block)

:api: public

end
proxy.set_user = user
Warden::Manager.on_request do |proxy|
user = "A User"
Example:

proxy - The warden proxy object for the request
Block Parameters: |proxy|
A block to contain logic for the callback
Parameters:

A callback that runs on each request, just after the proxy is initialized
def on_request(options = {}, method = :push, &block)
  raise BlockNotGiven unless block_given?
  _on_request.send(method, [block, options])
end