Sha256: c45883607c0f9537f89e27a6b122c66aa50779f5a00bd724683552894f9ff9d2

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

require 'active_record'
require 'unix_crypt'

class CASinoCore::Authenticator::ActiveRecord

  # @param [Hash] options
  def initialize(options)
    @options = options
    ::ActiveRecord::Base.establish_connection @options[:connection]

    eval <<-END
      class #{@options[:table].classify} < ActiveRecord::Base
      end
    END

    @model = "#{self.class.to_s}::#{@options[:table].classify}".constantize
  end

  def validate(username, password)
    user = @model.send("find_by_#{@options[:username_column]}!", username)
    password_from_database = user.send(@options[:password_column])

    if valid_password?(password, password_from_database)
      { username: user.send(@options[:username_column]) }.merge(extra_attributes(user))
    else
      false
    end

  rescue ActiveRecord::RecordNotFound
    false
  end

  private
  def valid_password?(password, password_from_database)
    UnixCrypt.valid?(password, password_from_database)
  end

  def extra_attributes(user)
    attributes = {}
    @options[:extra_attributes].each do |attribute_name, database_column|
      attributes[attribute_name] = user.send(database_column)
    end
    attributes
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
casino_core-authenticator-activerecord-0.0.1 lib/casino_core/authenticator/activerecord.rb