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
- #clean_up_passwords ⇒ Object
-
#digest_password(password) ⇒ Object
Digests a password using either bcrypt or scrypt (as configured by ‘config.encryptor`).
- #generate_reset_password_token! ⇒ Object
- #password=(password) ⇒ Object
- #reset_password!(params) ⇒ Object
- #salted_password(password) ⇒ Object
-
#valid_password?(password) ⇒ Boolean
Checks if a given password matches this user’s password.
Instance Method Details
#clean_up_passwords ⇒ Object
80 81 82 |
# File 'lib/janus/models/database_authenticatable.rb', line 80 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`).
67 68 69 70 71 72 73 74 |
# File 'lib/janus/models/database_authenticatable.rb', line 67 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.).to_s end end |
#generate_reset_password_token! ⇒ Object
84 85 86 87 88 |
# File 'lib/janus/models/database_authenticatable.rb', line 84 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
90 91 92 93 94 95 96 97 |
# File 'lib/janus/models/database_authenticatable.rb', line 90 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
76 77 78 |
# File 'lib/janus/models/database_authenticatable.rb', line 76 def salted_password(password) "#{password}#{self.class.pepper}" end |
#valid_password?(password) ⇒ Boolean
Checks if a given password matches this user’s password.
55 56 57 58 59 60 61 62 63 64 |
# 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 rescue BCrypt::Errors::InvalidHash, SCrypt::Errors::InvalidHash false end |