Module: Janus::Models::DatabaseAuthenticatable

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

Overview

DatabaseAuthenticatable

This is the initial part and is required for email + password registration and logins. Passwords are automatically encrypted following Devise’s default encryption logic, which relies on bcrypt.

Required columns:

  • email

  • encrypted_password

Configuration

  • stretches

  • pepper

  • authentication_keys - required keys for authenticating a user, defaults to [:email]

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#clean_up_passwordsObject



78
79
80
# File 'lib/janus/models/database_authenticatable.rb', line 78

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

#digest_password(password) ⇒ Object

Digests a password using either bcrypt or scrypt (as configured by ‘config.encryptor`).



65
66
67
68
69
70
71
72
# File 'lib/janus/models/database_authenticatable.rb', line 65

def digest_password(password)
  case self.class.encryptor
  when :bcrypt
    ::BCrypt::Password.create(salted_password(password), :cost => self.class.stretches).to_s
  when :scrypt
    ::SCrypt::Password.create(salted_password(password), self.class.scrypt_options).to_s
  end
end

#generate_reset_password_token!Object



82
83
84
85
86
# File 'lib/janus/models/database_authenticatable.rb', line 82

def generate_reset_password_token!
  self.reset_password_token = self.class.generate_token(:reset_password_token)
  self.reset_password_sent_at = Time.now
  save
end

#password=(password) ⇒ Object



49
50
51
52
# File 'lib/janus/models/database_authenticatable.rb', line 49

def password=(password)
  @password = password
  self.encrypted_password = digest_password(@password) unless @password.blank?
end

#reset_password!(params) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/janus/models/database_authenticatable.rb', line 88

def reset_password!(params)
  %w{password password_confirmation}.each do |attr|
    send("#{attr}=", params[attr]) if params.has_key?(attr)
  end

  self.reset_password_sent_at = self.reset_password_token = nil
  save
end

#salted_password(password) ⇒ Object



74
75
76
# File 'lib/janus/models/database_authenticatable.rb', line 74

def salted_password(password)
  "#{password}#{self.class.pepper}"
end

#valid_password?(password) ⇒ Boolean

Checks if a given password matches this user’s password.

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
# File 'lib/janus/models/database_authenticatable.rb', line 55

def valid_password?(password)
  case self.class.encryptor
  when :bcrypt
    ::BCrypt::Password.new(encrypted_password) == salted_password(password)
  when :scrypt
    ::SCrypt::Password.new(encrypted_password) == salted_password(password)
  end
end