2010-03-29 16:13:19 +02:00
|
|
|
require 'devise/strategies/database_authenticatable'
|
2010-09-25 16:08:46 +02:00
|
|
|
require 'bcrypt'
|
2009-10-12 08:37:28 -03:00
|
|
|
|
2009-09-17 09:24:33 -03:00
|
|
|
module Devise
|
2009-10-09 08:30:25 -03:00
|
|
|
module Models
|
2011-01-12 02:53:17 +08:00
|
|
|
# Authenticatable Module, responsible for encrypting password and validating
|
2009-10-09 09:27:44 -03:00
|
|
|
# authenticity of a user while signing in.
|
|
|
|
#
|
2010-07-15 13:01:31 +02:00
|
|
|
# == Options
|
2009-10-20 11:55:57 -02:00
|
|
|
#
|
2010-07-15 13:01:31 +02:00
|
|
|
# DatabaseAuthenticable adds the following options to devise_for:
|
2009-10-20 11:55:57 -02:00
|
|
|
#
|
2010-09-25 16:08:46 +02:00
|
|
|
# * +stretches+: the cost given to bcrypt.
|
2009-11-15 03:31:13 -02:00
|
|
|
#
|
2010-07-15 13:01:31 +02:00
|
|
|
# == Examples
|
2009-10-09 09:27:44 -03:00
|
|
|
#
|
|
|
|
# User.find(1).valid_password?('password123') # returns true/false
|
2009-10-20 11:55:57 -02:00
|
|
|
#
|
2010-03-29 16:13:19 +02:00
|
|
|
module DatabaseAuthenticatable
|
2010-04-06 16:34:22 +02:00
|
|
|
extend ActiveSupport::Concern
|
2009-09-17 11:06:46 -03:00
|
|
|
|
2010-02-17 12:35:38 +01:00
|
|
|
included do
|
|
|
|
attr_reader :password, :current_password
|
|
|
|
attr_accessor :password_confirmation
|
2011-04-16 12:52:59 +02:00
|
|
|
before_validation :downcase_keys
|
2011-06-10 01:37:43 -07:00
|
|
|
before_validation :strip_whitespace
|
2009-09-17 09:24:33 -03:00
|
|
|
end
|
|
|
|
|
2010-09-26 11:47:56 -03:00
|
|
|
# Generates password encryption based on the given value.
|
2009-10-15 15:52:25 -03:00
|
|
|
def password=(new_password)
|
|
|
|
@password = new_password
|
2010-09-25 16:08:46 +02:00
|
|
|
self.encrypted_password = password_digest(@password) if @password.present?
|
2009-10-15 15:52:25 -03:00
|
|
|
end
|
|
|
|
|
2011-02-15 11:33:54 +01:00
|
|
|
# Verifies whether an password (ie from sign in) is the user password.
|
2010-09-28 17:45:06 +02:00
|
|
|
def valid_password?(password)
|
2011-04-16 12:43:43 +02:00
|
|
|
return false if encrypted_password.blank?
|
|
|
|
bcrypt = ::BCrypt::Password.new(self.encrypted_password)
|
|
|
|
password = ::BCrypt::Engine.hash_secret("#{password}#{self.class.pepper}", bcrypt.salt)
|
|
|
|
Devise.secure_compare(password, self.encrypted_password)
|
2010-01-24 03:38:52 +01:00
|
|
|
end
|
|
|
|
|
2010-02-08 23:14:03 +01:00
|
|
|
# Set password and password confirmation to nil
|
|
|
|
def clean_up_passwords
|
2011-04-18 09:56:24 +02:00
|
|
|
self.password = self.password_confirmation = ""
|
2010-02-08 23:14:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Update record attributes when :current_password matches, otherwise returns
|
|
|
|
# error on :current_password. It also automatically rejects :password and
|
|
|
|
# :password_confirmation if they are blank.
|
2009-12-14 22:55:55 -02:00
|
|
|
def update_with_password(params={})
|
2010-02-16 14:31:49 +01:00
|
|
|
current_password = params.delete(:current_password)
|
2010-02-08 20:38:47 +01:00
|
|
|
|
2010-04-25 09:38:56 +02:00
|
|
|
if params[:password].blank?
|
|
|
|
params.delete(:password)
|
|
|
|
params.delete(:password_confirmation) if params[:password_confirmation].blank?
|
|
|
|
end
|
2010-02-08 23:14:03 +01:00
|
|
|
|
2010-02-15 14:15:24 +01:00
|
|
|
result = if valid_password?(current_password)
|
2009-12-14 22:55:55 -02:00
|
|
|
update_attributes(params)
|
|
|
|
else
|
2010-02-16 17:00:36 +01:00
|
|
|
self.errors.add(:current_password, current_password.blank? ? :blank : :invalid)
|
2010-04-01 14:00:21 +02:00
|
|
|
self.attributes = params
|
2009-12-14 22:55:55 -02:00
|
|
|
false
|
|
|
|
end
|
2010-02-08 23:14:03 +01:00
|
|
|
|
2010-04-01 14:00:21 +02:00
|
|
|
clean_up_passwords
|
2010-02-08 23:14:03 +01:00
|
|
|
result
|
2009-12-14 22:55:55 -02:00
|
|
|
end
|
|
|
|
|
2011-05-05 09:24:21 +02:00
|
|
|
# Update record attributes without asking for the current password. Never allow to
|
|
|
|
# change the current password
|
|
|
|
def update_without_password(params={})
|
|
|
|
params.delete(:password)
|
|
|
|
params.delete(:password_confirmation)
|
|
|
|
|
|
|
|
result = update_attributes(params)
|
|
|
|
clean_up_passwords
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2010-04-06 13:26:56 +02:00
|
|
|
def after_database_authentication
|
|
|
|
end
|
|
|
|
|
2010-09-25 16:08:46 +02:00
|
|
|
# A reliable way to expose the salt regardless of the implementation.
|
|
|
|
def authenticatable_salt
|
2010-11-11 22:51:19 +01:00
|
|
|
self.encrypted_password[0,29] if self.encrypted_password
|
2010-09-25 16:08:46 +02:00
|
|
|
end
|
|
|
|
|
2010-03-29 23:44:47 +02:00
|
|
|
protected
|
2009-11-18 09:26:47 -02:00
|
|
|
|
2010-11-18 23:29:53 +01:00
|
|
|
# Downcase case-insensitive keys
|
|
|
|
def downcase_keys
|
2011-03-15 12:52:53 +01:00
|
|
|
(self.class.case_insensitive_keys || []).each { |k| self[k].try(:downcase!) }
|
2010-11-18 23:29:53 +01:00
|
|
|
end
|
2011-06-10 01:37:43 -07:00
|
|
|
|
|
|
|
def strip_whitespace
|
|
|
|
(self.class.strip_whitespace_keys || []).each { |k| self[k].try(:strip!) }
|
|
|
|
end
|
2010-11-18 23:29:53 +01:00
|
|
|
|
2010-09-25 16:08:46 +02:00
|
|
|
# Digests the password using bcrypt.
|
2010-03-29 23:44:47 +02:00
|
|
|
def password_digest(password)
|
2010-09-28 17:45:06 +02:00
|
|
|
::BCrypt::Password.create("#{password}#{self.class.pepper}", :cost => self.class.stretches).to_s
|
2010-03-29 23:44:47 +02:00
|
|
|
end
|
2009-10-09 08:30:25 -03:00
|
|
|
|
|
|
|
module ClassMethods
|
2010-11-20 21:19:12 +01:00
|
|
|
Devise::Models.config(self, :pepper, :stretches)
|
2010-07-12 06:59:49 +02:00
|
|
|
|
2010-07-05 19:11:37 +02:00
|
|
|
# We assume this method already gets the sanitized values from the
|
|
|
|
# DatabaseAuthenticatable strategy. If you are using this method on
|
|
|
|
# your own, be sure to sanitize the conditions hash to only include
|
|
|
|
# the proper fields.
|
2010-04-06 13:26:56 +02:00
|
|
|
def find_for_database_authentication(conditions)
|
|
|
|
find_for_authentication(conditions)
|
2009-11-19 13:53:57 -02:00
|
|
|
end
|
2009-11-22 22:32:54 -02:00
|
|
|
end
|
2009-09-17 09:46:40 -03:00
|
|
|
end
|
2009-09-17 09:24:33 -03:00
|
|
|
end
|
|
|
|
end
|