Initial commit
This commit is contained in:
23
app/services/password_strength_checker.rb
Normal file
23
app/services/password_strength_checker.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# password entropy calculation
|
||||
class PasswordStrengthChecker
|
||||
class <<self
|
||||
# this method typically called by validate! from model and from API /pass/validate controller
|
||||
def calculate_entropy(password)
|
||||
@checker ||= StrongPassword::StrengthChecker.new(min_entropy: Barong::App.config.password_min_entropy,
|
||||
use_dictionary: Barong::App.config.password_use_dictionary)
|
||||
@checker.calculate_entropy(password)
|
||||
end
|
||||
|
||||
# User model invokes this method while validating password on create and update
|
||||
def validate!(password)
|
||||
password_regex = Barong::App.config.password_regexp
|
||||
return 'requirements.short' unless password_regex.match(password)
|
||||
|
||||
return 'weak' if calculate_entropy(password) < Barong::App.config.password_min_entropy
|
||||
|
||||
'strong'
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user