Sha256: e3bc137facd2f918ad7d7a08d725b6398d0710f2b83f2bb70b0e338fe563769e

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module Passweird
  # 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?
  class Checker
    attr_reader :password

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

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

      @password = password
    end

    # Checks if the password is blacklisted
    #
    # @return [Boolean] true if the password is blacklisted, false otherwise
    def blacklisted?
      @blacklisted ||= BlacklistedTerm.exists?(term: possible_terms)
    end

    # Retrieves the blacklisted terms that match the possible terms
    #
    # @return [ActiveRecord::Relation] a collection of blacklisted terms
    def blacklisted_terms
      @blacklisted_terms ||= BlacklistedTerm.where("LOWER(term) IN ?", possible_terms)
    end

    # Generates all possible terms from substrings and leet speak equivalents
    #
    # @return [Array<String>] an array of unique possible terms
    def possible_terms
      @possible_terms ||= (substrings + unleeted_substrings).uniq
    end

    private

    def unleeted_substrings
      @unleeted_substrings ||= LeetSpeak.unleet_all(substrings)
    end

    def substrings
      @substrings ||= Substringer.substrings(password)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
passweird-0.1.0 lib/passweird/checker.rb