Sha256: d148c560acce9ee3eafee1310ad68b93dc4cc96786cc709ae989f0801328f5f4

Contents?: true

Size: 1.81 KB

Versions: 13

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true
require "strong_password"

module ThinkFeelDoEngine
  module Concerns
    # validates passwords.
    module ValidatePassword
      WEAK_PASSWORD_MESSAGE = "is too weak"
      extend ActiveSupport::Concern

      included do
        before_validation :contains_integer,
                          :contains_lowercase_letter,
                          :contains_uppercase_letter,
                          :repeating_characters,
                          on: [:create, :update],
                          if: :password_exists_without_message?

        validates :password,
                  password_strength: {
                    extra_dictionary_words: :extra_words,
                    use_dictionary: true,
                    min_entropy: ThinkFeelDoEngine::
                        PasswordValidator::VALID_ENTROPY,
                    message: WEAK_PASSWORD_MESSAGE
                  },
                  if: :password_exists_without_message?
      end

      private

      def contains_integer
        set_error_on_password unless password =~ /[0-9]/
      end

      def contains_lowercase_letter
        set_error_on_password unless password =~ /[a-z]/
      end

      def contains_uppercase_letter
        set_error_on_password unless password =~ /[A-Z]/
      end

      def email_prefix
        @email_prefix ||= email[/[^@]+/]
      end

      def extra_words
        [email_prefix, email_prefix.reverse] if email.present?
      end

      def password_exists_without_message?
        password.present? &&
          !errors.full_messages.include?("Password #{WEAK_PASSWORD_MESSAGE}")
      end

      def repeating_characters
        set_error_on_password if password =~ /(.)\1\1/
      end

      def set_error_on_password
        errors.add(:password, WEAK_PASSWORD_MESSAGE)
      end
    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/concerns/validate_password.rb
think_feel_do_engine-3.22.8 app/models/think_feel_do_engine/concerns/validate_password.rb
think_feel_do_engine-3.22.7 app/models/think_feel_do_engine/concerns/validate_password.rb
think_feel_do_engine-3.22.6 app/models/think_feel_do_engine/concerns/validate_password.rb
think_feel_do_engine-3.22.5 app/models/think_feel_do_engine/concerns/validate_password.rb
think_feel_do_engine-3.22.4 app/models/think_feel_do_engine/concerns/validate_password.rb
think_feel_do_engine-3.22.2 app/models/think_feel_do_engine/concerns/validate_password.rb
think_feel_do_engine-3.22.1 app/models/think_feel_do_engine/concerns/validate_password.rb
think_feel_do_engine-3.22.0 app/models/think_feel_do_engine/concerns/validate_password.rb
think_feel_do_engine-3.21.2 app/models/think_feel_do_engine/concerns/validate_password.rb
think_feel_do_engine-3.21.1 app/models/think_feel_do_engine/concerns/validate_password.rb
think_feel_do_engine-3.21.0 app/models/think_feel_do_engine/concerns/validate_password.rb
think_feel_do_engine-3.20.1 app/models/think_feel_do_engine/concerns/validate_password.rb