Skip to content
This repository
Rich Rines March 03, 2014
file 40 lines (31 sloc) 0.786 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
module Clearance
  module PasswordStrategies
    module BCrypt
      require 'bcrypt'

      extend ActiveSupport::Concern

      def authenticated?(password)
        ::BCrypt::Password.new(encrypted_password) == password
      end

      def password=(new_password)
        @password = new_password

        if new_password.present?
          self.encrypted_password = encrypt(new_password)
        end
      end

      private

      def encrypt(password)
        ::BCrypt::Password.create(password, cost: cost)
      end

      def cost
        if test_environment?
          ::BCrypt::Engine::MIN_COST
        else
          ::BCrypt::Engine::DEFAULT_COST
        end
      end

      def test_environment?
        defined?(::Rails) && ::Rails.env.test?
      end
    end
  end
end
Something went wrong with that request. Please try again.