从清除密码存储迁移到authlogic

时间:2010-06-19 07:14:29

标签: ruby-on-rails ruby passwords authlogic

我目前正在开发一个存储简单明确密码的Rails应用程序(...)。所以我正在使用“标准”SHA512加密迁移到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核心选项?

谢谢!

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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
相关问题