【问题标题】:Can I use devise encrypted_password with custom authentication?我可以将设计 encrypted_pa​​ssword 与自定义身份验证一起使用吗?
【发布时间】:2015-09-06 04:27:25
【问题描述】:

我有一个带有设计身份验证的 rails 4 应用程序。我正在从头开始重建并想编写自己的身份验证系统,但我将用户存储在数据库中,其密码存储为encrypted_password,这是设计用来存储散列密码的。我知道使用bcrypt 我应该有一个password_digestcolumn。

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

【问题讨论】:

    标签: ruby-on-rails authentication devise


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2017-04-02
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 2012-01-29
      • 1970-01-01
      • 2015-05-21
      • 2016-10-18
      • 2021-01-03
      相关资源
      最近更新 更多