Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class User < Sequel::Model
- validates do
- presence_of :email_address
- presence_of :password_salt
- presence_of :password_hash
- end
- def password_mismatch
- @password_mismatch ||= false
- end
- def password_mismatch=(val)
- @password_mismatch = val
- end
- validates_each :password_mismatch do |object,attribute,value|
- raise @password_mismatch.inspect
- if @password_mismatch
- object.errors[attribute] << "does not match"
- end
- end
- before_create do
- self.signed_up_on = Time.now
- end
- def password=(pass)
- @password = pass
- # This is for generating the keys
- salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
- self.password_salt, self.password_hash = salt, Digest::SHA256.hexdigest(pass+salt)
- end
- def self.authenticate(email_address, password)
- user = User.where{:email_address == email_address}.first
- if user.nil? || Digest::SHA256.hexdigest(password + user.password_salt) != user.password_hash
- raise "Username or password invalid"
- end
- user
- end
- end
- >> u = User.new(:email_address => "hey@hey.com", :password_mismatch => true); u.valid?
- RuntimeError: nil
Add Comment
Please, Sign In to add comment