我可以将devise encrypted_pa​​ssword与自定义身份验证一起使用吗?

时间:2015-09-06 04:27:26

标签: ruby-on-rails authentication devise

我有一个带有设计认证的rails 4应用程序。我正在从头开始重建,并希望编写自己的身份验证系统,但我将用户存储在数据库中,其密码存储为encrypted_password,这是设备用于存储哈希密码的用途。我了解使用bcrypt我应该有一个password_digest列。

我的问题有两个:bcrypt是否能够读取我在设计encrypted_password列中存储的内容,如果是,我可以简单地将该数据库列重命名为password_digest,否则会导致问题?

1 个答案:

答案 0 :(得分:1)

根据我的阅读,是的,您应该只需重命名该列并将其与自定义身份验证一起使用。

参考文献: https://github.com/plataformatec/devise/blob/master/lib/devise/models/database_authenticatable.rb#L149-L151

module Devise
  def self.bcrypt(klass, password)
    ActiveSupport::Deprecation.warn "Devise.bcrypt is deprecated; use Devise::Encryptor.digest instead"
    Devise::Encryptor.digest(klass, password)
  end

  module Models
    module DatabaseAuthenticatable

      # Digests the password using bcrypt. Custom encryption should override
      # this method to apply their own algorithm.
      #
      # See https://github.com/plataformatec/devise-encryptable for examples
      # of other encryption engines.
      def password_digest(password)
        Devise::Encryptor.digest(self.class, password)
      end

https://github.com/plataformatec/devise/blob/master/lib/devise/encryptor.rb#L5-L10

module Devise
  module Encryptor
    def self.digest(klass, password)
      if klass.pepper.present?
        password = "#{password}#{klass.pepper}"
      end
      ::BCrypt::Password.create(password, cost: klass.stretches).to_s
    end
相关问题