Skip to content

Instantly share code, notes, and snippets.

@bdmac
Created January 17, 2014 20:07
Show Gist options
  • Save bdmac/8480492 to your computer and use it in GitHub Desktop.
Save bdmac/8480492 to your computer and use it in GitHub Desktop.
Using ClientSideValidations middleware with StrongPassword.
module ClientSideValidations::Middleware
class PasswordStrength < ClientSideValidations::Middleware::Base
require 'strong_password'
def response
min_entropy = StrongPassword::StrengthChecker::BASE_ENTROPY
if request.params['min_entropy']
min_entropy = request.params['min_entropy'].to_f
end
entropy = StrongPassword::StrengthChecker.new(request.params['password']).calculate_entropy(use_dictionary: true)
is_strong = entropy >= min_entropy
# Body contains percent of min entropy
percent_entropy = (entropy / min_entropy) * 100
percent_entropy = 100 if percent_entropy > 100
percent_entropy = "#{percent_entropy.to_i}%"
self.body = percent_entropy
if request.params['password'] && is_strong
self.status = 200
else
self.status = 404
end
super
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment