Sha256: bca9f18f06531344ac4c31b6b1c9cd3adbc8c3c59b626188084e4654a16bf64b

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

require 'devise_zxcvbn/email_tokeniser'

module Devise
  module Models
    module Zxcvbnable
      extend ActiveSupport::Concern

      delegate :min_password_score, to: "self.class"

      included do
        validate :not_weak_password, if: :password_required?
      end

      def password_score
        self.class.password_score(self)
      end

      private

      def not_weak_password
        if password_score < min_password_score
          self.errors.add :password, :weak_password, score: password_score, min_password_score: min_password_score
          return false
        end
      end

      module ClassMethods
        Devise::Models.config(self, :min_password_score)

        def password_score(user, email=nil)
          password = nil
          weak_words = []

          if user.is_a? String
            password = user
          else
            password = user.password
            email = user.email unless email
          end

          weak_words = [email, *DeviseZxcvbn::EmailTokeniser.split(email)] if email

          ::Zxcvbn.test(password, weak_words).score
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
devise_zxcvbn-1.1.2 lib/devise_zxcvbn/model.rb