Skip to content

Instantly share code, notes, and snippets.

@oleganza
Created December 9, 2008 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oleganza/33865 to your computer and use it in GitHub Desktop.
Save oleganza/33865 to your computer and use it in GitHub Desktop.
#
# Helps to authenticate person using password and email
#
class Person
Authentication = DeferredModule.new do
property :crypted_password, String, :length => 50, :accessor => :protected # DM bug #663, :lazy => [:auth_data]
property :salt, String, :length => 50, :accessor => :protected # DM bug #663, :lazy => [:auth_data]
attr_accessor :password, :password_confirmation
validates_length :password, :min => 4, :if => :password_required?
validates_is_confirmed :password, :if => :password_required?, :message => "Password and confirmation do not match"
before :save, :encrypt_password
# Encrypts the password with the user salt
def encrypt(password)
self.class.encrypt(password, salt)
end
def encrypt_password
return if password.blank?
self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{email}--") if new_record?
self.crypted_password = encrypt(password)
end
def authenticated?(password)
crypted_password == encrypt(password)
end
def password_required?
crypted_password.blank? || !password.blank?
end
def with_random_password
self.password = rand(2**64).to_s(16)
self.password_confirmation = self.password
self
end
end
module Authentication
def self.included(base)
super(base)
base.extend(ClassMethods)
end
module ClassMethods
# Encrypts some data with the salt.
def encrypt(password, salt)
Digest::SHA1.hexdigest("--#{salt}--#{password}--")
end
# Authenticates a user by their login field and unencrypted password. Returns the user or nil.
def authenticate(given_email, given_password)
person = first(:email => given_email)
person = person && person.authenticated?(given_password) && person.activated? ? person : nil
end
end
end
end
#
# Helps to authenticate person using password and email
#
class Person
SimplisticAuthentication = DeferredModule.new do
property :password, String, :length => 255
attr_accessor :password_confirmation
validates_length :password, :min => 4
validates_is_confirmed :password, :if => :new_record?, :message => "Password and confirmation do not match"
def authenticated?(password)
self.password == password
end
def with_random_password
self.password = rand(2**64).to_s(16)
self.password_confirmation = self.password
self
end
end
module SimplisticAuthentication
def self.included(base)
super(base)
base.extend(ClassMethods)
end
module ClassMethods
# Authenticates a user by their login field and unencrypted password. Returns the user or nil.
def authenticate(given_email, given_password)
person = first(:email => given_email)
person = person && person.authenticated?(given_password) && person.activated? ? person : nil
end
end
end # SimplisticAuthentication
end # Person
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment