将自定义身份验证转换为身份验证逻辑
问题描述:
我试图在我的Rails应用中将我的自定义简单身份验证系统转换为使用身份验证逻辑。我设法使所有的工作都相当容易,但现在当我尝试登录时,它不会正确验证我的凭据。相关代码如下:将自定义身份验证转换为身份验证逻辑
# 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将密码散列为?还有什么可能是错的?
答
我解决我的问题,我不得不写一个自定义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