class Authenticate::Lifecycle

Lifecycle stores and runs callbacks for authorization events.

Heavily borrowed from warden (github.com/hassox/warden).

Events:

Callbacks are added via after_set_user or after_authentication.

Callbacks can throw(:failure,message) to signal an authentication/authorization failure, or perform actions on the user or session.

Options

The callback options may optionally specify when to run the callback:

The callback may also specify a 'name' key in options. This is for debugging purposes only.

Callback block parameters

Callbacks are invoked with the following block parameters: |user, session, opts|

Example

# A callback to track the users successful logins:
Authenticate.lifecycle.after_set_user do |user, session, opts|
  user.sign_in_count += 1
end

Public Class Methods

new() click to toggle source
# File lib/authenticate/lifecycle.rb, line 42
def initialize
  @conditions = [:only, :except, :event].freeze
end

Public Instance Methods

after_authentication(options = {}, method = :push, &block) click to toggle source

A callback to run after the user successfully authenticates, during the login process. Mechanically identical to [#after_set_user].

# File lib/authenticate/lifecycle.rb, line 53
def after_authentication(options = {}, method = :push, &block)
  add_callback(after_authentication_callbacks, options, method, &block)
end
after_set_user(options = {}, method = :push, &block) click to toggle source

This callback is triggered after the first time a user is set during per-hit authorization, or during login.

# File lib/authenticate/lifecycle.rb, line 47
def after_set_user(options = {}, method = :push, &block)
  add_callback(after_set_user_callbacks, options, method, &block)
end
prepend_after_authentication(options = {}, &block) click to toggle source
# File lib/authenticate/lifecycle.rb, line 78
def prepend_after_authentication(options = {}, &block)
  after_authentication(options, :unshift, &block)
end
run_callbacks(kind, user, session, *args) click to toggle source

Run callbacks of the given kind.

  • kind - :authenticate or :after_set_user

  • args - user, session, opts hash. Opts is an optional event, e.g. { event: :authentication }

Example:

Authenticate.lifecycle.run_callbacks(:after_set_user, @current_user, self, { event: :authentication })
# File lib/authenticate/lifecycle.rb, line 65
def run_callbacks(kind, user, session, *args) # args - |user, session, opts|
  # Last callback arg MUST be a Hash
  options = args.last
  send("#{kind}_callbacks").each do |callback, conditions| # each callback has 'conditions' stored with it
    conditions = conditions.dup.delete_if { |key, _val| !@conditions.include? key }
    invalid = conditions.find do |key, value|
      value.is_a?(Array) ? !value.include?(options[key]) : (value != options[key])
    end
    callback.call(user, session, *args) unless invalid
  end
  nil
end

Private Instance Methods

add_callback(callbacks, options = {}, method = :push, &block) click to toggle source
# File lib/authenticate/lifecycle.rb, line 84
def add_callback(callbacks, options = {}, method = :push, &block)
  raise BlockNotGiven unless block_given?
  options = process_opts(options)
  callbacks.send(method, [block, options])
end
after_authentication_callbacks() click to toggle source
# File lib/authenticate/lifecycle.rb, line 104
def after_authentication_callbacks
  @after_authentication_callbacks ||= []
end
after_set_user_callbacks() click to toggle source
# File lib/authenticate/lifecycle.rb, line 100
def after_set_user_callbacks
  @after_set_user_callbacks ||= []
end
process_opts(options) click to toggle source

set event: to run callback on based on options

# File lib/authenticate/lifecycle.rb, line 91
def process_opts(options)
  if options.key?(:only)
    options[:event] = options.delete(:only)
  elsif options.key?(:except)
    options[:event] = [:set_user, :authentication] - Array(options.delete(:except))
  end
  options
end