【问题标题】:Migrating from clear password storage to authlogic从清除密码存储迁移到 authlogic
【发布时间】:2011-03-05 17:16:33
【问题描述】:

我目前正在开发一个存储明文密码 (...) 的 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 核心选项?

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby passwords 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
    

    【讨论】:

    • 哇!超好!非常感谢。
    【解决方案2】:

    我个人会编写一个迁移来将所有明文密码迁移到加密密码中。您可能适合在迁移中定义自己的准系统模型以允许良好的低级访问。

    【讨论】:

      猜你喜欢
      • 2013-07-26
      • 1970-01-01
      • 2015-03-21
      • 2018-08-18
      • 2012-10-18
      • 2017-02-03
      • 1970-01-01
      • 2012-04-10
      • 1970-01-01
      相关资源
      最近更新 更多