【问题标题】:Convert Custom Auth to AuthLogic将自定义身份验证转换为 AuthLogic
【发布时间】:2010-06-26 19:47:04
【问题描述】:

我正在尝试将我的 rails 应用程序中的自定义简单身份验证系统转换为使用 AuthLogic。我已经设法让一切工作变得相当容易,但是现在当我尝试登录时,它不会正确验证我的凭据。相关代码如下:

# app/models/profile.rb
class Profile < ActiveRecord::Base
  acts_as_authentic do |c|
    c.transition_from_crypto_providers = Authlogic::CryptoProviders::Sha1,
    c.crypto_provider = Authlogic::CryptoProviders::Sha512
  end
end

我曾经在创建时使用它来散列我的密码:

# app/models/profile.rb
def hash_password
  self.salt = ActiveSupport::SecureRandom.base64(8)
  self.hashed_password = Digest:SHA1.hexdigest(self.salt + @password)
end

我已经将所有必要的表列转换为与 AuthLogic 兼容。 有没有办法记录 AuthLogic 将密码散列为什么?还有什么问题?

【问题讨论】:

    标签: ruby-on-rails authentication authlogic


    【解决方案1】:

    我解决了我的问题,我必须编写一个自定义的 crypto_provider,对于任何好奇的人来说都是这样的:

    class MyCryptoProvider
      # Turns your raw password into a Sha1 hash.
      def self.encrypt(*tokens)
        tokens = tokens.flatten
        tokens = tokens.reverse
        digest = Digest::SHA1.hexdigest([*tokens].join)
        digest
      end
    
      # Does the crypted password match the tokens? Uses the same tokens that were used to encrypt.
      def self.matches?(crypted, *tokens)
        encrypt(*tokens) == crypted
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2020-04-02
      • 2020-04-01
      • 2022-12-10
      • 1970-01-01
      • 2018-09-26
      • 2010-12-17
      • 1970-01-01
      • 2010-11-11
      • 2016-07-09
      相关资源
      最近更新 更多