2010-06-26 88 views
1

我試圖在我的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將密碼散列爲?還有什麼可能是錯的?

回答

0

我解決我的問題,我不得不寫一個自定義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