Sha256: 5e853a38a7f80aa7058705471c16fe4206975872ffcb2d022168123fdce33815

Contents?: true

Size: 1.28 KB

Versions: 13

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true
module ThinkFeelDoEngine
  # Users Participant and User password validations
  class PasswordValidator
    WEAK_PASSWORD_MESSAGE = "is too weak"
    VALID_ENTROPY = 10

    attr_reader :password,
                :password_token,
                :person,
                :token

    def initialize(password:, password_token:)
      @password = password
      @password_token = password_token
      find_or_intialize_person
    end

    def entropy_value
      valid_entropy? ? calculate_entroy : 0
    end

    private

    def calculate_entroy
      StrongPassword::StrengthChecker
        .new(password)
        .calculate_entropy(use_dictionary: true)
    end

    def find_or_intialize_person
      @person ||= Participant.find_by_reset_password_token(token) ||
                  User.find_by_reset_password_token(token) ||
                  Participant.new
    end

    def set_password
      person.password = password
      person.password_confirmation = password
    end

    def token
      @token ||= Devise.token_generator.digest(
        Devise, :reset_password_token,
        password_token
      )
    end

    def valid_entropy?
      set_password
      !person
        .tap(&:valid?)
        .errors[:password]
        .include?(WEAK_PASSWORD_MESSAGE)
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
think_feel_do_engine-3.22.9 app/models/think_feel_do_engine/password_validator.rb
think_feel_do_engine-3.22.8 app/models/think_feel_do_engine/password_validator.rb
think_feel_do_engine-3.22.7 app/models/think_feel_do_engine/password_validator.rb
think_feel_do_engine-3.22.6 app/models/think_feel_do_engine/password_validator.rb
think_feel_do_engine-3.22.5 app/models/think_feel_do_engine/password_validator.rb
think_feel_do_engine-3.22.4 app/models/think_feel_do_engine/password_validator.rb
think_feel_do_engine-3.22.2 app/models/think_feel_do_engine/password_validator.rb
think_feel_do_engine-3.22.1 app/models/think_feel_do_engine/password_validator.rb
think_feel_do_engine-3.22.0 app/models/think_feel_do_engine/password_validator.rb
think_feel_do_engine-3.21.2 app/models/think_feel_do_engine/password_validator.rb
think_feel_do_engine-3.21.1 app/models/think_feel_do_engine/password_validator.rb
think_feel_do_engine-3.21.0 app/models/think_feel_do_engine/password_validator.rb
think_feel_do_engine-3.20.1 app/models/think_feel_do_engine/password_validator.rb