2010-06-19 38 views
2

我目前正在研究存儲純明確密碼(...)的Rails應用程序。因此,我正在使用「標準」SHA512加密遷移到Authlogic身份驗證。從明確的密碼存儲遷移到authlogic

我這樣做的正常工作:

#file /models/user.rb 
class User < ActiveRecord::Base 

    acts_as_authentic { |c| 
    c.transition_from_crypto_providers = [MyOwnNoCrypto, Authlogic::CryptoProviders::Sha512] 
    } 
end 

#file /lib/my_own_no_crypto.rb 
class MyOwnNoCrypto 
    def self.encrypt(*tokens) 
    return tokens[0] # or tokens.join I guess 
    end 

    def self.matches?(crypted_password, *tokens) 
    return crypted_password == tokens.join 
    end 
end 

這是很好的 - 和工作得很好 - 但我不知道是否有做一個性感的方式,也許有Authlogic核心選項?

謝謝!

回答

1

我同意thomasfedb's answer的部分建議一次性轉換,而不是使用AuthLogic的轉換模型。在這種情況下,你想盡快這些密碼進行加密成爲可能,而不是下一次在用戶登錄而不是一個Rake任務,不過,我可能會建議遷移:

# in db/migrate/nnnnnnnn_encrypt_passwords.rb: 

class EncryptPasswords < ActiveRecord::Migration 
    def self.up 
    add_column :users, :crypted_password 
    User.each do |u| 
     u.encrypt_password! 
    end 
    remove_column :users, :password 
    end 

    def self.down 
    raise IrreversibleMigration.new('Cannot decrypt user passwords') 
    end 
end 
+1

哇!超好!非常感謝。 – 2010-06-21 10:00:40

1

就我個人而言,我會寫一個遷移,將所有明文密碼遷移到加密的密碼中。在遷移中定義自己的裸骨模型以允許良好的低級訪問可能會很有幫助。