Sha256: 80b1289bb90b0b648135d9219f6d73d9856c3af96a6066ec3e22ec80caba658a

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

module Passwd::Rails
  module ActiveRecordExt
    def with_authenticate(passwd: nil, user_id: :email, salt: :salt, password: :password)
      passwd ||= Passwd.current
      define_singleton_auth_with_passwd(user_id)
      define_instance_auth_with_passwd(passwd, salt, password)
      define_instance_set_password(passwd, salt, password)
    end

    private

      def define_singleton_auth_with_passwd(user_id_col)
        define_singleton_method :authenticate do |user_id, plain|
          user = find_by(user_id_col => user_id)
          return nil unless user

          user.authenticate(plain) ? user : nil
        end
      end

      def define_instance_auth_with_passwd(passwd, salt_col, password_col)
        define_method :authenticate do |plain|
          passwd.hashed_password(plain, send(salt_col)) == send(password_col)
        end
      end

      def define_instance_set_password(passwd, salt_col, password_col)
        define_method :set_password do |plain = nil|
          plain ||= passwd.random
          random_salt = Rails.application.config.passwd.random_salt || proc { SecureRandom.uuid }
          send("#{salt_col}=", random_salt.call(self)) unless send(salt_col)
          send("#{password_col}=", passwd.hashed_password(plain, send(salt_col)))
          plain
        end
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
passwd-0.3.0 lib/passwd/rails/active_record_ext.rb