module NOBSPW::ValidationMethods

Constants

DEFAULT_VALIDATION_METHODS
INTERRUPT_VALIDATION_FOR
STDIN_GREP_COMMAND

Private Instance Methods

domain_included_in_password?() click to toggle source
# File lib/nobspw/validation_methods.rb, line 45
def domain_included_in_password?
  domain = NOBSPW.configuration.domain_name
  return nil unless domain
  domain = strip_extension_from_domain(domain)
  domain_without_separator = remove_word_separators(domain).gsub(' ', '')
  words_included_in_password?([domain, domain_without_separator])
end
email_included_in_password?() click to toggle source
# File lib/nobspw/validation_methods.rb, line 39
def email_included_in_password?
  return nil unless @email
  words = remove_word_separators(email_without_extension(@email)).split(' ')
  words_included_in_password?(words)
end
email_without_extension(email) click to toggle source
# File lib/nobspw/validation_methods.rb, line 110
def email_without_extension(email)
  name, domain, whatev = email&.split("@", 3)
  "#{name}@#{strip_extension_from_domain(domain)}"
end
escaped_password(password = @password) click to toggle source
# File lib/nobspw/validation_methods.rb, line 123
def escaped_password(password = @password)
  Shellwords.escape(password)
end
name_included_in_password?() click to toggle source
# File lib/nobspw/validation_methods.rb, line 27
def name_included_in_password?
  return nil unless @name
  words = remove_word_separators(@name).split(' ')
  words_included_in_password?(words)
end
not_enough_unique_characters?() click to toggle source
# File lib/nobspw/validation_methods.rb, line 75
def not_enough_unique_characters?
  unique  = @password.split(//).uniq.size
  minimum = NOBSPW.configuration.min_unique_characters
  !(unique >= minimum)
end
password_empty?() click to toggle source
# File lib/nobspw/validation_methods.rb, line 23
def password_empty?
  @password.nil? || @password.strip == ''
end
password_not_allowed?() click to toggle source
# File lib/nobspw/validation_methods.rb, line 53
def password_not_allowed?
  return nil unless NOBSPW.configuration.blacklist

  NOBSPW.configuration.blacklist.each do |expression|
    if expression.is_a?(Regexp)
      return true if @password.match?(expression)
    else
      return true if expression.to_s == @password
    end
  end

  false
end
password_too_common?() click to toggle source
# File lib/nobspw/validation_methods.rb, line 88
def password_too_common?
  NOBSPW.configuration.use_ruby_grep ? ruby_grep : shell_grep
end
password_too_long?() click to toggle source
# File lib/nobspw/validation_methods.rb, line 71
def password_too_long?
  @password.length > NOBSPW.configuration.max_password_length
end
password_too_short?() click to toggle source
# File lib/nobspw/validation_methods.rb, line 67
def password_too_short?
  @password.length < NOBSPW.configuration.min_password_length
end
remove_word_separators(str) click to toggle source
# File lib/nobspw/validation_methods.rb, line 119
def remove_word_separators(str)
  str&.gsub(/-|_|\.|\'|\"|\@/, ' ')
end
ruby_grep() click to toggle source
# File lib/nobspw/validation_methods.rb, line 106
def ruby_grep
  File.open(NOBSPW.configuration.dictionary_path).grep(/^#{escaped_password}$/).any?
end
shell_grep() click to toggle source

Helper methods

# File lib/nobspw/validation_methods.rb, line 94
def shell_grep
  raise StandardError.new("Grep not found at: #{NOBSPW.configuration.grep_path}") \
    if !File.exist?(NOBSPW.configuration.grep_path)

  output = Open3.popen3(STDIN_GREP_COMMAND.join(" "), out: '/dev/null') { |stdin, stdout, stderr, wait_thr|
    stdin.puts "^#{escaped_password}$"
    stdin.close
    wait_thr.value
  }
  output.success?
end
strip_extension_from_domain(domain) click to toggle source
# File lib/nobspw/validation_methods.rb, line 115
def strip_extension_from_domain(domain)
  domain&.split(".")&.first
end
username_included_in_password?() click to toggle source
# File lib/nobspw/validation_methods.rb, line 33
def username_included_in_password?
  return nil unless @username
  words = remove_word_separators(@username).split(' ')
  words_included_in_password?(words)
end
words_included_in_password?(words) click to toggle source
# File lib/nobspw/validation_methods.rb, line 81
def words_included_in_password?(words)
  downcased_pw = @password.downcase
  words.each do |word|
    return true if word.length > 2 && downcased_pw.index(word.downcase)
  end; false
end