【发布时间】:2012-09-04 22:32:26
【问题描述】:
我目前有一个带有 django 的系统,我需要将其迁移到 rails。我在 Rails 中使用 Devise 进行授权。旧的 django 系统有自己的一组用户,我需要将它们迁移到 rails。我关心的是用户的密码。它使用sha1算法加密。那么,我该如何修改设计,使其也与旧用户的密码兼容。
【问题讨论】:
标签: ruby-on-rails django devise
我目前有一个带有 django 的系统,我需要将其迁移到 rails。我在 Rails 中使用 Devise 进行授权。旧的 django 系统有自己的一组用户,我需要将它们迁移到 rails。我关心的是用户的密码。它使用sha1算法加密。那么,我该如何修改设计,使其也与旧用户的密码兼容。
【问题讨论】:
标签: ruby-on-rails django devise
每个用户都有自己的随机盐,这样如果带有密码的表被泄露,彩虹表将无助于获取实际密码。
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))
【讨论】:
我的用户模型中有以下方法:
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 逻辑。这种方式也支持设计密码,因此您将来不会遇到兼容性问题。
【讨论】: