Class: Zxcvbn::TesterBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/zxcvbn/tester_builder.rb

Overview

Fluent builder for constructing a Tester with custom word lists and options.

Obtain a builder via tester_builder and call #build to get the Tester.

Example:

tester = Zxcvbn
  .tester_builder
  .add_word_list('company', %w[acme corp])
  .max_password_length(75)
  .build

Constant Summary collapse

DEFAULT_MAX_PASSWORD_LENGTH =

Default maximum password length used when neither #max_password_length nor the ZXCVBN_MAX_PASSWORD_LENGTH environment variable is set.

256

Instance Method Summary collapse

Constructor Details

#initializeTesterBuilder

Returns a new instance of TesterBuilder.



23
24
25
26
# File 'lib/zxcvbn/tester_builder.rb', line 23

def initialize
  @word_lists = {}
  @max_password_length = nil
end

Instance Method Details

#add_word_list(name, words) ⇒ self

Parameters:

  • name (String)

    identifier for the word list; calling with the same name twice replaces the earlier list

  • words (Array<String>, String)

    words to add; non-String elements are silently ignored during matching

Returns:

  • (self)

Raises:

  • (ArgumentError)

    if name collides with a built-in dictionary name or “user_inputs”



32
33
34
35
36
37
38
39
40
# File 'lib/zxcvbn/tester_builder.rb', line 32

def add_word_list(name, words)
  if Data::RESERVED_NAMES.include?(name)
    raise ArgumentError,
          "#{name.inspect} is a reserved dictionary name; use a different name for custom word lists"
  end

  @word_lists[name] = Array(words)
  self
end

#buildTester

Returns:

Raises:

  • (ArgumentError)

    if max_password_length or ZXCVBN_MAX_PASSWORD_LENGTH is not a positive integer



56
57
58
59
60
# File 'lib/zxcvbn/tester_builder.rb', line 56

def build
  data = Data.new
  @word_lists.each_pair { |name, words| data.add_word_list(name, words) }
  Tester.new(data: data.freeze, max_password_length: effective_max_password_length).freeze
end

#max_password_length(length) ⇒ self

Parameters:

  • length (Integer)

    maximum password length for the built Zxcvbn::Tester

Returns:

  • (self)

Raises:

  • (ArgumentError)

    if length is not a positive integer



45
46
47
48
49
50
51
52
# File 'lib/zxcvbn/tester_builder.rb', line 45

def max_password_length(length)
  unless length.is_a?(Integer) && length.positive?
    raise ArgumentError, "max_password_length must be a positive integer; got #{length.inspect}"
  end

  @max_password_length = length
  self
end