Sha256: 98ccd35da2618766763741b4f77fbfc407f48e77394c8a8c0cbb6b908dd2c1b0

Contents?: true

Size: 1.29 KB

Versions: 25

Compression:

Stored size: 1.29 KB

Contents

# frozen-string-literal: true

module Rodauth
  Feature.define(:disallow_common_passwords, :DisallowCommonPasswords) do
    depends :login_password_requirements_base

    auth_value_method :most_common_passwords_file, File.expand_path('../../../../dict/top-10_000-passwords.txt', __FILE__)
    translatable_method :password_is_one_of_the_most_common_message, "is one of the most common passwords"
    auth_value_method :most_common_passwords, nil

    auth_methods :password_one_of_most_common?

    def password_meets_requirements?(password)
      super && password_not_one_of_the_most_common?(password)
    end

    def post_configure
      super

      return if most_common_passwords || !most_common_passwords_file

      require 'set'
      most_common = Set.new(File.read(most_common_passwords_file).split("\n").each(&:freeze)).freeze
      self.class.send(:define_method, :most_common_passwords){most_common}
    end

    def password_one_of_most_common?(password)
      most_common_passwords.include?(password)
    end

    private

    def password_not_one_of_the_most_common?(password)
      return true unless password_one_of_most_common?(password)
      set_password_requirement_error_message(:password_is_one_of_the_most_common, password_is_one_of_the_most_common_message)
      false
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
rodauth-2.37.0 lib/rodauth/features/disallow_common_passwords.rb
rodauth-2.36.0 lib/rodauth/features/disallow_common_passwords.rb
rodauth-2.34.0 lib/rodauth/features/disallow_common_passwords.rb
rodauth-2.33.0 lib/rodauth/features/disallow_common_passwords.rb
rodauth-2.32.0 lib/rodauth/features/disallow_common_passwords.rb
rodauth-2.31.0 lib/rodauth/features/disallow_common_passwords.rb
rodauth-2.30.0 lib/rodauth/features/disallow_common_passwords.rb
rodauth-2.29.0 lib/rodauth/features/disallow_common_passwords.rb
rodauth-2.28.0 lib/rodauth/features/disallow_common_passwords.rb
rodauth-2.27.0 lib/rodauth/features/disallow_common_passwords.rb
rodauth-2.26.1 lib/rodauth/features/disallow_common_passwords.rb
rodauth-2.26.0 lib/rodauth/features/disallow_common_passwords.rb
rodauth-2.25.0 lib/rodauth/features/disallow_common_passwords.rb
rodauth-2.24.0 lib/rodauth/features/disallow_common_passwords.rb
rodauth-2.23.0 lib/rodauth/features/disallow_common_passwords.rb
rodauth-2.22.0 lib/rodauth/features/disallow_common_passwords.rb
rodauth-2.21.0 lib/rodauth/features/disallow_common_passwords.rb
rodauth-2.20.0 lib/rodauth/features/disallow_common_passwords.rb
rodauth-2.19.0 lib/rodauth/features/disallow_common_passwords.rb
rodauth-2.18.0 lib/rodauth/features/disallow_common_passwords.rb