【问题标题】:Migrate django users to rails将 django 用户迁移到 rails
【发布时间】:2012-09-04 22:32:26
【问题描述】:

我目前有一个带有 django 的系统,我需要将其迁移到 rails。我在 Rails 中使用 Devise 进行授权。旧的 django 系统有自己的一组用户,我需要将它们迁移到 rails。我关心的是用户的密码。它使用sha1算法加密。那么,我该如何修改设计,使其也与旧用户的密码兼容。

【问题讨论】:

    标签: ruby-on-rails django devise


    【解决方案1】:

    每个用户都有自己的随机盐,这样如果带有密码的表被泄露,彩虹表将无助于获取实际密码。

    Checkout django/contrib/auth.models.py, check_password(raw_password, enc_password) 是您需要在 Rails 身份验证系统中实现的内容:

    def get_hexdigest(algorithm, salt, raw_password):
        """
        Returns a string of the hexdigest of the given plaintext password and salt
        using the given algorithm ('md5', 'sha1' or 'crypt').
        """
        raw_password, salt = smart_str(raw_password), smart_str(salt)
        if algorithm == 'crypt':
            try:
                import crypt
            except ImportError:
                raise ValueError('"crypt" password algorithm not supported in this environment')
            return crypt.crypt(raw_password, salt)
    
        if algorithm == 'md5':
            return md5_constructor(salt + raw_password).hexdigest()
        elif algorithm == 'sha1':
            return sha_constructor(salt + raw_password).hexdigest()
        raise ValueError("Got unknown password algorithm type in password.")
    
    def check_password(raw_password, enc_password):
        """
        Returns a boolean of whether the raw_password was correct. Handles
        encryption formats behind the scenes.
        """
        algo, salt, hsh = enc_password.split('$')
        return constant_time_compare(hsh, get_hexdigest(algo, salt, raw_password))
    

    【讨论】:

      【解决方案2】:

      我的用户模型中有以下方法:

      def valid_password?(pwd)
          begin
              super(pwd)
          rescue
              my_pwds = self.encrypted_password.split '$'
              Digest::SHA1.hexdigest( my_pwds[1] + pwd ) == my_pwds[2] rescue false
          end
      end
      

      这扩展了 default_password? Devise 用来查看用户是否提交了正确密码的方法。首先,使用正常的设计逻辑检查用户,如果这不起作用,则运行 Django sha1 逻辑。这种方式也支持设计密码,因此您将来不会遇到兼容性问题。

      【讨论】:

        猜你喜欢
        • 2011-04-11
        • 1970-01-01
        • 2019-06-02
        • 2014-11-21
        • 2015-05-02
        • 2021-02-18
        • 1970-01-01
        • 2020-05-08
        • 2013-06-08
        相关资源
        最近更新 更多