Class: Passweird::Checker

Inherits:
Object
  • Object
show all
Defined in:
lib/passweird/checker.rb

Overview

The Checker class is responsible for checking if a password is blacklisted by generating possible terms from substrings and leet speak conversions.

Example usage:

checker = Passweird::Checker.new("password")
checker.blacklisted?

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(password) ⇒ Checker

Returns a new instance of Checker.

Raises:

  • (ArgumentError)


17
18
19
20
21
# File 'lib/passweird/checker.rb', line 17

def initialize(password)
  raise ArgumentError, "password must be a String" unless password.is_a?(String)

  @password = password
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



11
12
13
# File 'lib/passweird/checker.rb', line 11

def password
  @password
end

Class Method Details

.blacklisted?(password) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/passweird/checker.rb', line 13

def self.blacklisted?(password)
  new(password).blacklisted?
end

Instance Method Details

#blacklisted?Boolean

Checks if the password is blacklisted

Returns:

  • (Boolean)

    true if the password is blacklisted, false otherwise



26
27
28
# File 'lib/passweird/checker.rb', line 26

def blacklisted?
  @blacklisted ||= BlacklistedTerm.exists?(term: possible_terms)
end

#blacklisted_termsActiveRecord::Relation

Retrieves the blacklisted terms that match the possible terms

Returns:

  • (ActiveRecord::Relation)

    a collection of blacklisted terms



33
34
35
# File 'lib/passweird/checker.rb', line 33

def blacklisted_terms
  @blacklisted_terms ||= BlacklistedTerm.where(term: possible_terms)
end

#possible_termsArray<String>

Generates all possible terms from substrings and leet speak equivalents

Returns:

  • (Array<String>)

    an array of unique possible terms



40
41
42
# File 'lib/passweird/checker.rb', line 40

def possible_terms
  @possible_terms ||= all_substring_case_permutations.uniq
end