I have researched a lot on this topic but due to some reason I am unable to perform password complexity implementation on my Ruby on Rails Web Application. I have installed the devise gem and followed Best flexible rails password security implementation and How to validate password strength with Devise in Ruby on Rails?.
My regex seems to be working when I check it online
/\A(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[[:^alnum:]])/x
but once I implement it inside my user.rb it just does not work.
My user.rb file:
#Active Record for Users
class User < ActiveRecord::Base
belongs_to :entity
has_and_belongs_to_many :groups, :join_table => "users_groups"
has_many :surveys, inverse_of: :user
has_many :results, inverse_of: :user
validates :password, :firstName, :email, :salt, :role, :timezone, presence: true
validates :email, :uniqueness => {:scope => :entity_id}
validates_format_of :email, :with => /.+@.+\..+/i
devise :database_authenticatable, :validatable
validate :password_complexity
#User Authentication
def self.authenticate(email="", lpassword="")
users = User.where(email: email)
results = []
users.each do |user|
if user && user.match_password(lpassword)
results.push(user)
end
end
if(results.length == 0)
return false
else
return results
end
end
#Password Check
def match_password(lpassword="")
return (BCrypt::Password.new(password).is_password?(lpassword+salt))
end
#Password Authentication
def password_complexity
if password.present? and not password.match(/\A(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[[:^alnum:]])/x)
errors.add :password, "must include at least one lowercase letter, one uppercase letter, and one digit"
end
end
end
prybreakpoint into#password_complexitymethod and try match manually as:/\A(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[[:^alnum:]])/x =~ password