Module: Devise::Models::DatabaseAuthenticatable

Extended by:
ActiveSupport::Concern
Defined in:
lib/devise/models/database_authenticatable.rb

Overview

Authenticatable Module, responsible for encrypting password and validating authenticity of a user while signing in.

Options

DatabaseAuthenticable adds the following options to devise_for:

* +pepper+: a random string used to provide a more secure hash. Use
  `rake secret` to generate new keys.

* +stretches+: the cost given to bcrypt.

Examples

User.find(1).valid_password?('password123')         # returns true/false

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#after_database_authenticationObject



86
87
# File 'lib/devise/models/database_authenticatable.rb', line 86

def after_database_authentication
end

#authenticatable_saltObject

A reliable way to expose the salt regardless of the implementation.



90
91
92
# File 'lib/devise/models/database_authenticatable.rb', line 90

def authenticatable_salt
  self.encrypted_password[0,29] if self.encrypted_password
end

#clean_up_passwordsObject

Set password and password confirmation to nil



47
48
49
# File 'lib/devise/models/database_authenticatable.rb', line 47

def clean_up_passwords
  self.password = self.password_confirmation = nil
end

#password=(new_password) ⇒ Object

Generates password encryption based on the given value.



33
34
35
36
# File 'lib/devise/models/database_authenticatable.rb', line 33

def password=(new_password)
  @password = new_password
  self.encrypted_password = password_digest(@password) if @password.present?
end

#update_with_password(params = {}) ⇒ Object

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.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/devise/models/database_authenticatable.rb', line 54

def update_with_password(params={})
  current_password = params.delete(:current_password)

  if params[:password].blank?
    params.delete(:password)
    params.delete(:password_confirmation) if params[:password_confirmation].blank?
  end

  result = if valid_password?(current_password)
    update_attributes(params)
  else
    self.attributes = params
    self.valid?
    self.errors.add(:current_password, current_password.blank? ? :blank : :invalid)
    false
  end

  clean_up_passwords
  result
end

#update_without_password(params = {}) ⇒ Object

Updates record attributes without asking for the current password. Never allows to change the current password



77
78
79
80
81
82
83
84
# File 'lib/devise/models/database_authenticatable.rb', line 77

def update_without_password(params={})
  params.delete(:password)
  params.delete(:password_confirmation)

  result = update_attributes(params)
  clean_up_passwords
  result
end

#valid_password?(password) ⇒ Boolean

Verifies whether an password (ie from sign in) is the user password.

Returns:

  • (Boolean)


39
40
41
42
43
44
# File 'lib/devise/models/database_authenticatable.rb', line 39

def valid_password?(password)
  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