Sha256: 2d437ae1cd0074e7bb02bced68ebcf5b329d56d42f151cb057490ca6c4c0a66d

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

# dependencies
require "geocoder"
require "warden"

# modules
require "auth_trail/engine"
require "auth_trail/manager"
require "auth_trail/version"

module AuthTrail
  class << self
    attr_accessor :exclude_method, :geocode, :track_method, :identity_method, :job_queue
  end
  self.geocode = true
  self.identity_method = lambda do |request, opts, user|
    if user
      user.try(:email)
    else
      scope = opts[:scope]
      request.params[scope] && request.params[scope][:email] rescue nil
    end
  end

  def self.track(strategy:, scope:, identity:, success:, request:, user: nil, failure_reason: nil)
    info = {
      strategy: strategy,
      scope: scope,
      identity: identity,
      success: success,
      failure_reason: failure_reason,
      user: user,
      ip: request.remote_ip,
      user_agent: request.user_agent,
      referrer: request.referrer
    }

    if request.params[:controller]
      info[:context] = "#{request.params[:controller]}##{request.params[:action]}"
    end

    # if exclude_method throws an exception, default to not excluding
    exclude = AuthTrail.exclude_method && AuthTrail.safely(default: false) { AuthTrail.exclude_method.call(info) }

    unless exclude
      if AuthTrail.track_method
        AuthTrail.track_method.call(info)
      else
        login_activity = LoginActivity.new
        info.each do |k, v|
          login_activity.try("#{k}=", v)
        end
        login_activity.save!
        AuthTrail::GeocodeJob.perform_later(login_activity) if AuthTrail.geocode
      end
    end
  end

  def self.safely(default: nil)
    begin
      yield
    rescue => e
      warn "[authtrail] #{e.class.name}: #{e.message}"
      default
    end
  end
end

Warden::Manager.after_set_user except: :fetch do |user, auth, opts|
  AuthTrail::Manager.after_set_user(user, auth, opts)
end

Warden::Manager.before_failure do |env, opts|
  AuthTrail::Manager.before_failure(env, opts) if opts[:message]
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
authtrail-0.2.1 lib/authtrail.rb