Module: Clearance::PasswordStrategies::BCrypt

Defined in:
lib/clearance/password_strategies/bcrypt.rb

Overview

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.

Instance Method Summary collapse

Instance Method Details

#authenticated?(password) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
# File 'lib/clearance/password_strategies/bcrypt.rb', line 12

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

#password=(new_password) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/clearance/password_strategies/bcrypt.rb', line 18

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