2010-06-19 40 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

就我个人而言,我会写一个迁移,将所有明文密码迁移到加密的密码中。在迁移中定义自己的裸骨模型以允许良好的低级访问可能会很有帮助。