Sha256: ac51750595112c8c88fd192c8dbfb1bb7664fb3de09e6d04c08a3fa683294b49

Contents?: true

Size: 1.06 KB

Versions: 9

Compression:

Stored size: 1.06 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?
          cost = if defined?(::Rails) && ::Rails.env.test?
                   ::BCrypt::Engine::MIN_COST
                 else
                   ::BCrypt::Engine::DEFAULT_COST
                 end

          self.encrypted_password = ::BCrypt::Password.create(
            new_password,
            cost: cost,
          )
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
clearance-2.0.0.beta2 lib/clearance/password_strategies/bcrypt.rb
clearance-2.0.0.beta1 lib/clearance/password_strategies/bcrypt.rb
clearance-1.17.0 lib/clearance/password_strategies/bcrypt.rb
clearance-1.16.2 lib/clearance/password_strategies/bcrypt.rb
clearance-1.16.1 lib/clearance/password_strategies/bcrypt.rb
clearance-1.16.0 lib/clearance/password_strategies/bcrypt.rb
clearance-1.15.1 lib/clearance/password_strategies/bcrypt.rb
clearance-1.15.0 lib/clearance/password_strategies/bcrypt.rb
clearance-1.14.2 lib/clearance/password_strategies/bcrypt.rb