Sha256: 6d5b089c6f18067d1c5bd60e31b518b6d6e4273be8ea95977d66e448f1e91ebd

Contents?: true

Size: 1.39 KB

Versions: 14

Compression:

Stored size: 1.39 KB

Contents

module MinimalistAuthentication
  class Password
    class << self
      # Create a bcrypt password hash with a calibrated cost factor.
      def create(secret)
        new ::BCrypt::Engine.hash_secret(secret, BCrypt::Engine.generate_salt(cost))
      end

      # Cache the calibrated bcrypt cost factor.
      def cost
        @bcrypt_cost ||= calibrate_cost
      end

      private

      # Calibrates cost so that new user passwords can automatically take
      # advantage of faster server hardware in the future.
      # Sets cost to BCrypt::Engine::MIN_COST in the test environment
      def calibrate_cost
        ::Rails.env.test? ? ::BCrypt::Engine::MIN_COST : ::BCrypt::Engine.calibrate(750)
      end
    end

    attr_accessor :bcrypt_password

    # Returns a password object wrapping a valid BCrypt password or a NullPassword
    def initialize(password_hash)
      begin
        self.bcrypt_password = ::BCrypt::Password.new(password_hash)
      rescue ::BCrypt::Errors::InvalidHash
        self.bcrypt_password = NullPassword.new
      end
    end

    # Delegate methods to bcrypt_password
    delegate :==, :to_s, :cost, to: :bcrypt_password

    # Temporary access to checksum and salt for backwards compatibility
    delegate :checksum, :salt,  to: :bcrypt_password

    # Checks if the password_hash cost factor is less than the current cost.
    def stale?
      cost < self.class.cost
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
minimalist_authentication-2.4.0 lib/minimalist_authentication/password.rb
minimalist_authentication-2.3.0 lib/minimalist_authentication/password.rb
minimalist_authentication-2.2.4 lib/minimalist_authentication/password.rb
minimalist_authentication-2.2.3 lib/minimalist_authentication/password.rb
minimalist_authentication-2.2.2 lib/minimalist_authentication/password.rb
minimalist_authentication-2.2.1 lib/minimalist_authentication/password.rb
minimalist_authentication-2.2.0 lib/minimalist_authentication/password.rb
minimalist_authentication-2.1.5 lib/minimalist_authentication/password.rb
minimalist_authentication-2.1.4 lib/minimalist_authentication/password.rb
minimalist_authentication-2.1.3 lib/minimalist_authentication/password.rb
minimalist_authentication-2.1.2 lib/minimalist_authentication/password.rb
minimalist_authentication-2.1.1 lib/minimalist_authentication/password.rb
minimalist_authentication-2.1.0 lib/minimalist_authentication/password.rb
minimalist_authentication-2.0.0 lib/minimalist_authentication/password.rb