我目前有一个需要迁移到rails的django系统。我正在使用Devise进行Rails授权。旧的Django系统有它自己的一组用户,我需要迁移到rails。我关心的是用户的密码。它使用sha1算法进行加密。所以,我如何修改设计,使其与旧用户的密码兼容。将django用户迁移到rails
1
A
回答
2
每个用户都得到自己的随机盐,这样如果密码表泄露,彩虹表不会帮助获取实际的密码。
结帐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))
1
我在我的用户模型下面的方法:
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逻辑运行。这样设计密码也是可以的,所以你在将来不会遇到兼容性问题。
相关问题
- 1. 迁移Rails restful认证用户到Django
- 2. Wordpress用户迁移到rails设计
- 3. 将用户帐户从Joomla迁移到Django
- 4. 将cakephp应用程序迁移到django
- 5. 迁移到Django 1.4
- 6. 将用户密码从Symfony迁移到Django
- 7. 将现有模型迁移到Django内置用户模型
- 8. 迁移到Rails 4.2
- 9. 迁移到Rails 3
- 10. 将phpBB3用户迁移到Drupal 7
- 11. 将旧用户迁移到symfony2
- 12. 将AspNetSqlMembershipProvider用户迁移到WebMatrix
- 13. 将Joomla 1.5用户迁移到Wordpress 3.2
- 14. Rails 4:将所有迁移重新迁移到Schema
- 15. 将django.dispatch.dispatcher从Django 0.96迁移到1.0.2
- 16. 将Django项目从MySQL迁移到Oracle
- 17. Django将模式从sqlite迁移到postgresql
- 18. 将Django从MySQL迁移到postgresql
- 19. 将Django迁移到生产服务器
- 20. Rails将列迁移到关联
- 21. Rails将日期迁移到字符串?
- 22. 将Rails ActiveRecord列迁移到新模型
- 23. Rails 3:将模型迁移到MongoDB
- 24. Django 1.7数据迁移和用户组
- 25. 如何迁移类似于Ruby on Rails迁移的Django模型?
- 26. 将Google App Engine应用程序从Django 0.96迁移到Django 1.2
- 27. Django迁移找不到GDALRaster
- 28. Django:从MEDIA_URL迁移到STATIC_URL
- 29. 在Django中迁移到mysql
- 30. Django标记迁移到GAE