Sha256: 7a00fa6c079d2f68a64841ef85849e7fe29ebc4588d9bad77a1089850253ad40

Contents?: true

Size: 1.21 KB

Versions: 5

Compression:

Stored size: 1.21 KB

Contents

module Clearance
  module PasswordStrategies
    # Uses BCrypt to authenticate users and store encrypted passwords.
    #
    # The BCrypt cost (the measure of how many key expansion iterations BCrypt
    # will perform) is automatically set to the minimum allowed value when
    # Rails is operating in the test environment and the default cost in all
    # other envionments. This provides a speed boost in tests.
    module BCrypt
      require 'bcrypt'

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

      def password=(new_password)
        @password = new_password

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

      private

      # @api private
      def encrypt(password)
        ::BCrypt::Password.create(password, cost: cost)
      end

      # @api private
      def cost
        if test_environment?
          ::BCrypt::Engine::MIN_COST
        else
          ::BCrypt::Engine::DEFAULT_COST
        end
      end

      # @api private
      def test_environment?
        defined?(::Rails) && ::Rails.env.test?
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
clearance-1.14.1 lib/clearance/password_strategies/bcrypt.rb
clearance-1.14.0 lib/clearance/password_strategies/bcrypt.rb
clearance-1.13.0 lib/clearance/password_strategies/bcrypt.rb
clearance-1.12.1 lib/clearance/password_strategies/bcrypt.rb
clearance-1.12.0 lib/clearance/password_strategies/bcrypt.rb