2009-09-17 08:24:33 -04:00
|
|
|
module Devise
|
|
|
|
module Authenticable
|
|
|
|
require 'digest/sha1'
|
|
|
|
|
2009-10-07 20:59:21 -04:00
|
|
|
# Pepper for encrypting password
|
|
|
|
mattr_accessor :pepper
|
|
|
|
self.pepper = '23c64df433d9b08e464db5c05d1e6202dd2823f0'
|
2009-09-17 08:24:33 -04:00
|
|
|
|
|
|
|
def self.included(base)
|
|
|
|
base.class_eval do
|
2009-09-17 10:06:46 -04:00
|
|
|
extend ClassMethods
|
|
|
|
|
2009-09-17 08:24:33 -04:00
|
|
|
attr_reader :password
|
|
|
|
attr_accessor :password_confirmation
|
|
|
|
attr_accessible :email, :password, :password_confirmation
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Defines the new password, generating a salt and encrypting it.
|
|
|
|
#
|
|
|
|
def password=(new_password)
|
|
|
|
if new_password != @password
|
|
|
|
@password = new_password
|
|
|
|
if @password.present?
|
|
|
|
generate_salt
|
|
|
|
encrypt_password
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-17 08:46:40 -04:00
|
|
|
# Verifies whether an incoming_password (ie from login) is the user password
|
|
|
|
#
|
|
|
|
def valid_password?(incoming_password)
|
|
|
|
password_digest(incoming_password) == encrypted_password
|
|
|
|
end
|
|
|
|
|
2009-09-17 08:24:33 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
# Generate password salt using SHA1 based on password and Time.now
|
|
|
|
#
|
|
|
|
def generate_salt
|
2009-09-17 10:06:46 -04:00
|
|
|
self.password_salt = secure_digest(Time.now.utc, random_string, password) if password_salt.blank?
|
2009-09-17 08:24:33 -04:00
|
|
|
end
|
|
|
|
|
2009-09-17 08:46:40 -04:00
|
|
|
# Encrypt password using SHA1
|
2009-09-17 08:24:33 -04:00
|
|
|
#
|
|
|
|
def encrypt_password
|
2009-09-17 08:46:40 -04:00
|
|
|
self.encrypted_password = password_digest(password)
|
|
|
|
end
|
|
|
|
|
2009-10-07 20:59:21 -04:00
|
|
|
# Gererates a default password digest based on salt, pepper and the
|
|
|
|
# incoming password
|
2009-09-17 08:46:40 -04:00
|
|
|
#
|
|
|
|
def password_digest(password_to_digest)
|
2009-10-07 20:59:21 -04:00
|
|
|
secure_digest(password_salt, @@pepper, password_to_digest)
|
2009-09-17 08:24:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Generate a SHA1 digest joining args. Generated token is something like
|
|
|
|
#
|
|
|
|
# --arg1--arg2--arg3--argN--
|
|
|
|
#
|
|
|
|
def secure_digest(*tokens)
|
|
|
|
::Digest::SHA1.hexdigest('--' << tokens.flatten.join('--') << '--')
|
|
|
|
end
|
2009-09-17 08:46:40 -04:00
|
|
|
|
2009-09-17 10:06:46 -04:00
|
|
|
# Generate a string randomically based on rand method
|
|
|
|
#
|
|
|
|
def random_string
|
|
|
|
(1..10).map{ rand.to_s }
|
|
|
|
end
|
|
|
|
|
2009-09-17 08:46:40 -04:00
|
|
|
module ClassMethods
|
|
|
|
|
|
|
|
# Authenticate a user based on email and password. Returns the
|
|
|
|
# authenticated user if it's valid or nil
|
|
|
|
#
|
|
|
|
def authenticate(email, password)
|
2009-09-17 10:27:45 -04:00
|
|
|
authenticable = self.find_by_email(email)
|
|
|
|
authenticable if authenticable.valid_password?(password) unless authenticable.nil?
|
2009-09-17 08:46:40 -04:00
|
|
|
end
|
|
|
|
end
|
2009-09-17 08:24:33 -04:00
|
|
|
end
|
|
|
|
end
|