heartcombo--devise/lib/devise/models/database_authenticatable.rb

131 lines
4.1 KiB
Ruby
Raw Normal View History

require 'devise/strategies/database_authenticatable'
require 'bcrypt'
2009-10-12 11:37:28 +00:00
module Devise
module Models
# Authenticatable Module, responsible for encrypting password and validating
2009-10-09 12:27:44 +00:00
# authenticity of a user while signing in.
#
2010-07-15 11:01:31 +00:00
# == Options
2009-10-20 13:55:57 +00:00
#
2010-07-15 11:01:31 +00:00
# DatabaseAuthenticable adds the following options to devise_for:
2009-10-20 13:55:57 +00:00
#
# * +pepper+: a random string used to provide a more secure hash. Use
# `rake secret` to generate new keys.
#
# * +stretches+: the cost given to bcrypt.
2009-11-15 05:31:13 +00:00
#
2010-07-15 11:01:31 +00:00
# == Examples
2009-10-09 12:27:44 +00:00
#
# User.find(1).valid_password?('password123') # returns true/false
2009-10-20 13:55:57 +00:00
#
module DatabaseAuthenticatable
extend ActiveSupport::Concern
2010-02-17 11:35:38 +00:00
included do
attr_reader :password, :current_password
attr_accessor :password_confirmation
end
2012-02-17 16:37:44 +00:00
module ModuleMethods
extend self
def required_fields
[:encrypted_password] + Devise.authentication_keys
2012-02-17 16:37:44 +00:00
end
end
# Generates password encryption based on the given value.
2009-10-15 18:52:25 +00:00
def password=(new_password)
@password = new_password
self.encrypted_password = password_digest(@password) if @password.present?
2009-10-15 18:52:25 +00:00
end
2011-02-15 10:33:54 +00:00
# Verifies whether an password (ie from sign in) is the user password.
def valid_password?(password)
2011-04-16 10:43:43 +00: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)
end
2010-02-08 22:14:03 +00:00
# Set password and password confirmation to nil
def clean_up_passwords
self.password = self.password_confirmation = nil
2010-02-08 22:14:03 +00: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.
def update_with_password(params, *options)
current_password = params.delete(:current_password)
2010-02-08 19:38:47 +00:00
if params[:password].blank?
params.delete(:password)
params.delete(:password_confirmation) if params[:password_confirmation].blank?
end
2010-02-08 22:14:03 +00:00
result = if valid_password?(current_password)
update_attributes(params, *options)
else
self.attributes = params
self.valid?
self.errors.add(:current_password, current_password.blank? ? :blank : :invalid)
false
end
2010-02-08 22:14:03 +00:00
clean_up_passwords
2010-02-08 22:14:03 +00:00
result
end
2011-06-22 16:01:49 +00:00
# Updates record attributes without asking for the current password.
# Never allows to change the current password. If you are using this
# method, you should probably override this method to protect other
# attributes you would not like to be updated without a password.
#
# Example:
#
# def update_without_password(params={})
# params.delete(:email)
# super(params)
# end
#
def update_without_password(params, *options)
2011-05-05 07:24:21 +00:00
params.delete(:password)
params.delete(:password_confirmation)
result = update_attributes(params, *options)
2011-05-05 07:24:21 +00:00
clean_up_passwords
result
end
def after_database_authentication
end
# A reliable way to expose the salt regardless of the implementation.
def authenticatable_salt
self.encrypted_password[0,29] if self.encrypted_password
end
protected
# Digests the password using bcrypt.
def password_digest(password)
::BCrypt::Password.create("#{password}#{self.class.pepper}", :cost => self.class.stretches).to_s
end
module ClassMethods
2010-11-20 20:19:12 +00:00
Devise::Models.config(self, :pepper, :stretches)
# 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.
def find_for_database_authentication(conditions)
find_for_authentication(conditions)
end
end
2009-09-17 12:46:40 +00:00
end
end
end